0

I am able to navigate to ChatMessageVC after tapping notification when app is in active state or in background, but when app is in killed state, clicking notification takes me to ContactListVC instead of chatMessageVC. Any help would be highly appreciable.

enter image description here

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
 if let option = launchOptions {
         let info = option[UIApplicationLaunchOptionsKey.remoteNotification]
                if (info != nil) {
                    self.application(application, didReceiveRemoteNotification: info as! [AnyHashable : Any])
                }
         }
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
    if state == .background {
        let navigation = storyboard.instantiateViewController(withIdentifier: "navigationContactPage")
        let navigationController = UINavigationController.init(rootViewController: navigation)
        self.window?.rootViewController = navigationController
        self.window?.makeKeyAndVisible()
        let viewController = storyboard.instantiateViewController(withIdentifier: "chatMessageVC") as! ChatMessagesVC

        navigationController.pushViewController(viewController, animated: true)
    }
}
kunal kushwaha
  • 111
  • 1
  • 10
  • As soon as the app goes into the killed state, an applicationWillTerminate function of AppDelegate is called, store Bool value and chatID in NSUserDefault and when function applicationDidBecomeActive is called load required view controller. Hope so this would help you. – Prakash Tripathi Jul 18 '18 at 11:21
  • @PrakashTripathi I get a sender_id in notification payload, I store it in NSUserDefaults while receiving the remote notification and navigate to ChatMessageVC and fetch sender_id from NSUserDefaults and load chats from firebase. But here i'm unable to navigate to ChatMessageVC after tapping on notification when app is not running – kunal kushwaha Jul 18 '18 at 12:06
  • Yes U can't navigate to chatView controller because After app goes into killed state, stacks of view Controller is cleared, so in this case load view controller with this [URL](https://stackoverflow.com/questions/25891444/launching-viewcontroller-from-appdelegate) – Prakash Tripathi Jul 18 '18 at 12:15
  • @PrakashTripathi I doing same as your shared URL says. i'm navigating to chatMessageVC from rootViewController(Navigation controller) – kunal kushwaha Jul 18 '18 at 12:31
  • have u added "aps" : { "content-available" : 1 }, in push notification and debugg console whether didReceiveNotificationResponse function is called or not in killed state. – Prakash Tripathi Jul 18 '18 at 12:46
  • @PrakashTripathi: Yes, content-available : 1 is already added, didReceiveNotificationResponse function is called after tapping on notification in killed state. I have already called manually in didFinishLaunchingWithOptions function – kunal kushwaha Jul 18 '18 at 12:53
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/176249/discussion-between-kunal-kushwaha-and-prakash-tripathi). – kunal kushwaha Jul 18 '18 at 12:56

1 Answers1

0

If you tap the notification while your app is terminated this method will be called inside your app.

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { let userInfo = response.notification.request.content.userInfo //Ask your backend developer to send chat id in notification user info. Some how you are succesfully navigating to contacts vc. In the same way open the chatVC using this chat id from contacts vc. }

Naga Syam
  • 126
  • 4
  • I am using FCM for push notifications.. I get contact_id and all the details i need on ChatMessageVC, Issue i'm facing is after notification is tapped, I navigate to contactPage instead of chatMessageVC – kunal kushwaha Jul 19 '18 at 06:15