How do I open a Viewcontroller via Local Notification?

Asked

Viewed 174 times

2

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

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

I am using Local Notification and not Remote Notification(Push).

I would like a simple example that doesn’t necessarily need an internet connection.

1 answer

1


In the iOS you don’t open a view directly (as can be done in Android) and yes the application is opened to then open the screen you want. This is all done in your Appdelegate, sort of like this:

In the application:didFinishLaunchingWithOptions:, which is where the lifecycle of your application starts as soon as it is opened by pressing 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 on launchOptions.

The method handleLocalNotification is what will perform the functions you want with the notification information:

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

Here, you have the variable userInfo with information from your notification (if anything) and viewController is your view, that its startup will depend on how you are doing your project, is something similar to what you should have already implemented.

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

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
    handleLocalNotification(application, userInfo: notification.userInfo)
}
  • This is all done inside the right Appdelegate?

  • 1

    That, the first section within the method didFinishLaunchingWithOptions: and the second is the I created, just to be apart, but you can put everything there in that condition from within.

  • I tested and didn’t open the View... what could I be doing wrong? Inside the handleLocalNotification() I called a println() to see if it was really calling the method, but apparently not.

  • I put a println inside Handle to see if it was calling but apparently not...

  • Oops, sorry, forgot the implementation of delegate didReceiveLocalNotification. There is the situation of when the application opens after it is terminated (for when it enters by didFinishLaunchingWithOptions) and when it is opened without being closed. When I say closed, it is when the user presses twice the button home and terminates the application, or if your application does not allow running on background.

  • Oh yes! I think it now works, but what I need to implement within the didReceiveLocalNotification?

  • 1

    I put it in the edit of my answer. It will only submit to the method we already created above. I also changed the parameter userInfo for optional, since it may be that you did not want to pass anything in the notification, just open a screen.

  • Thank you so much! Now it works!

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.