How to Open a Specific View Controller On didReceiveRemoteNotification when application is in back ground

前端 未结 3 1799
眼角桃花
眼角桃花 2021-02-10 21:00

I am implementing a alarm where i am getting pushNotification from server, i am receiving perfect push notification and it is working fine in foreground mode but when applicatio

相关标签:
3条回答
  • 2021-02-10 21:13

    First you need to set like this in your Xcode

    There are three states : Foreground,Background,Killed which you need to know clear details about your notification.

    1. Foreground : As soon as your notification come in when your app is at foreground, it will lead to the specific screen when you tapped at banner or alert base on your configuration in normal mode. But,at background modes, it can even do silently navigate as soon as your notification arrive without tapping it.

    2. Background : If your app is not at foreground,using background mode can help you open the specific screen if you handle it inside didReceiveRemoteNotification and as soon as the app is open when you tapped it,it will lead to it's specific screen.

    3. Killed : If your app is not in memory, only tapping push notification work for navigating specific screen because it wasn't in device memory so didReceiveRemoteNotification will not trigger if user neglect the notification.

    So,how do we handle the specific screen navigation? That's base on how you develop your application design and I can't provide correct answer until I see your code.

    But,yeah you can navigate to specific when the app is in background as soon as you open the app by tapping notification which appear on banner or lock screen.Everything can be done inside your didReceiveRemoteNotification under one condition when you want to do it in background.

    Take a look at this :

    https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

    At your push notification json,you have to include the following

    content-available : 1 
    

    when that is included,it can do background execution even the app is in background base on the additional data which you send from the server.

    At your additional data :

    {apns : [],
    data : [
       "Navigate" : "Home"
       ]}
    

    Then at your didReceiveRemoteNotification,

    if let aps = userInfo["data"] as? NSDictionary{
          // get the "Home" data and do navigation here
          // You might need to instantiate storyboard of the specific view controller which you want to go as RootViewController
    }
    

    So, when your notification comes,it will lead to specific screen in background if you do as soon as you tap the push notification from banner or alert.The application gonna open and it will lead to specific screen.

    0 讨论(0)
  • 2021-02-10 21:21

    1.Firstly you should Turn On Background Fetch in app "Capabilities" 2. Then use following code in app delegate

    In AppDelegate class add following code:

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
           // print(userInfo)
    let vc = mainStoryBoard.instantiateViewController(withIdentifier: "destinationVC") as! destinationVC
                                self.visibleNavController.pushViewController(vc, animated: true)
        }
    

    For iOS 10 use following code: 1.Import

     import UserNotifications
    

    For foreground fetch

         @available(iOS 10.0, *)
            func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (_ options: UNNotificationPresentationOptions) -> Void) {
                var userInfo = NSDictionary()
                userInfo = notification.request.content.userInfo as NSDictionary
                let pay = userInfo as NSDictionary
       let driverLocationVC = mainStoryBoard.instantiateViewController(withIdentifier: "destinationVC") as! destinationVC
                                    self.visibleNavController.pushViewController(driverLocationVC, animated: true)
    
    
        }
    

    For the background

     @available(iOS 10.0, *)
        func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
            print("Userinfo \(response.notification.request.content.userInfo)")
    
    
            var userInfo = NSDictionary()
            userInfo = response.notification.request.content.userInfo as NSDictionary
            print(userInfo)
        let driverLocationVC = mainStoryBoard.instantiateViewController(withIdentifier: "DriverLocationVC") as! DriverLocationVC
                                self.visibleNavController.pushViewController(driverLocationVC, animated: true)
    }
    

    For device token fetch

     func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
            let tokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
            print("Got token data! \(tokenString)")
    
            UserDefaults.standard.set(tokenString, forKey: "device_token")
            UserDefaults.standard.synchronize()
        }
    
    0 讨论(0)
  • 2021-02-10 21:30

    If you app is suspended check the UIApplicationLaunchOptionsRemoteNotificationKey in the dictionary from application:didFinishLaunchingWithOptions

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        ...
    
        // Check if launched from notification
        if let userInfo = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? [String: AnyObject] {
    
            // handle your notification like in application:didReceiveRemoteNotificatioUserInfo:
        }
        ...
    }
    
    0 讨论(0)
提交回复
热议问题