Open view controller when remote notification pres

2019-07-22 20:53发布

问题:

My app allows users to send a message to their friends. They then get a push notification. How can I get the view controller with the incoming messages to be opened when the app is opened via the notification?

回答1:

To do that you will need to override app delegate didFinishLaunchingWithOptions function and check if launchOptions contains any notification key:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    if let options = launchOptions {

        if let notification = options[UIApplicationLaunchOptionsRemoteNotificationKey] as? [NSObject : AnyObject] {

            //notification found mean that you app is opened from notification
        }
    }


    return true
}