How do I open a ViewController via Local Notification?

2

I have an application that sends notifications to the user from time to time, but how do I open a View other than the main View when the user clicks the notification?

Example, Whatsapp conversations, Messenger notifications, ie open specific screens via notification.

I'm using Local Notification and not Push Notification.

I would like a simple example and do not necessarily need an internet connection.

    
asked by anonymous 23.06.2015 / 19:22

1 answer

1

iOS you do not open a view directly (as can be done in Android ), the screen you want. This is all done in your AppDelegate , something like this:

No% of%, which is where the life cycle of your app starts as soon as it opens when you press the notification, you will have this:

if let options: NSDictionary = launchOptions {
    var localNotification = options.objectForKey(UIApplicationLaunchOptionsLocalNotificationKey) as? NSDictionary

    if localNotification != nil {
        handleLocalNotification(application, userInfo: localNotification!)
    }
}

This indicates that your application is being opened through notification and has been terminated before, so you will receive this key that exists in application:didFinishLaunchingWithOptions: .

The launchOptions method will perform the functions you want with the notification information:

func handleLocalNotification(application: UIApplication, userInfo: NSDictionary?) {
    window?.rootViewController = viewController
}

Here you have the variable handleLocalNotification with your notification information (if there is something) and userInfo is your view , that initializing it will depend on how you are doing it your project, is something similar to what you have already implemented.

And the implementation of delegate viewController , if it has not been terminated and submits to the same method we created above:

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
    handleLocalNotification(application, userInfo: notification.userInfo)
}
    
23.06.2015 / 19:46