Search code examples
iosswiftlocalnotificationunusernotificationcenter

How to open VC on local notification when app is closed


Hy I am new to iOS but somehow I manged to complete some tasks. I am working on app that sets reminders for user. So for this I am using Local notifications (UNUserNotificationCenterDelegate).

Everything is working good and fine. I have written some code, I am receiving notification at scheduled time and I have handled following cases.

  • When app is in background
  • When app is in forground.

My app handles these both cases perfectly or you can say as I needed. but I am helpless in following case

when the App is removed from recent, or not even running in background at all,and a that time if the scheduled notification pops up, and user taps the notification, It opens the splash view controller then opens my app main view controller, where as I need to go to same view controller from where user set the scheduled time for reminder.

I think I am quite clear in what I want and what is happening. Is there any changes to do that. I know it can be possible as Whats App and other apps are also doing this. Please help me in doing this. ...

Note: I am using UserNotifications (Local notification) and Deployment target is 10.3

Update: I saw this link has same need as mine but I do not know what the selected answer suggest as I am new to iOS so I am not sure what and how to do:


Solution

  • So, your problem is when the app is killed or inactive and then when user tap the notification the reminder screen will show up, right?

    Here's the case: Notification shows (inactive/killed) -> tap notification -> splash -> reminder screen.

    You should save your data that you want to show in notification. iOS will save any notification data in remoteNotification.

    So, when user opens the app from inactive, the first thing that will be called is launchOption in AppDelegate.

    Here's the example:

        if launchOptions != nil {
        // get data from notificaioton when app is killed or incative
               if let userInfo = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? NSDictionary  {
                       // Do what you want, you can set set the trigger to move it the screen as you want, for your case is to reminder screen
                    }
                }