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

前端 未结 3 1807
眼角桃花
眼角桃花 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.

提交回复
热议问题