1

I am using firebase push notification, everything is running fine when app is in background and foreground state. But when app is in killed state, tap on notification leads to home page. I have used all possible ways- by using launch option check

if let remoteNotification = launchOptions?[.remoteNotification] as?  [AnyHashable : Any] {

        if let userInfo = remoteNotification as? [String : Any]
        {
            DispatchQueue.main.asyncAfter(deadline: .now()+1.0, execute: {
            UIApplication.shared.applicationIconBadgeNumber = 0
            let notificationManager = PushNotificationManager()
            notificationManager.handleNotificationAction(data: userInfo)
        })
        }
    }

I don't know what I am missing. Please help.

1 Answers1

0
   In your appdelegate add this extension.

   extension AppDelegate: UNUserNotificationCenterDelegate {
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                            didReceive response: UNNotificationResponse, 
                            withCompletionHandler
                            completionHandler: @escaping () -> Void) {
    if UserDefaults.standard.bool(forKey: "login_session") {
        let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", 
        bundle: nil)
        let initialViewControlleripad : UIViewController = 
         mainStoryboardIpad.instantiateViewController(withIdentifier: "Notifs_Main") as UIViewController
        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window?.rootViewController = initialViewControlleripad
        self.window?.makeKeyAndVisible()
    } else {
        UserDefaults.standard.set(false, forKey: "isLoggedin")
        print("failed")
    }
    return completionHandler()
    }
   func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent
    notification: UNNotification, withCompletionHandler completionHandler:
    @escaping (UNNotificationPresentationOptions) -> Void) {
    return completionHandler(UNNotificationPresentationOptions.alert)
   }
 }
  • I have already used these methods but they are not working when app is in killed state -func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { if let userInfo = response.notification.request.content.userInfo as? [String : Any] { UIApplication.shared.applicationIconBadgeNumber = 0 let notificationManager = PushNotificationManager() notificationManager.handleNotificationAction(data: userInfo) } completionHandler() } – Nikita Srivastava Dec 06 '19 at 07:54
  • but are you getting a notification when the app is running on background? – Nathaniel Gonzales Dec 06 '19 at 08:05
  • yeah everything is working fine when app in running or background state, these delegate methods are getting called in that scenario but not in killed state so its opening the home page – Nikita Srivastava Dec 06 '19 at 08:08
  • may I see your didFinishLaunchingWithOptions? – Nathaniel Gonzales Dec 06 '19 at 08:13