简体   繁体   中英

Push notification when app is terminated

My app works fine with push notifications if the app was in the background and/or if the app is in the foreground.

The problem I have is if the app is terminated (which I force by double-click on the home button, find the app, and swipe up).

I am using ios 9 and swift 2.

In app delegate, didFinishLaunchingWithOptions, I do:

let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)

application.registerUserNotificationSettings(settings)

application.registerForRemoteNotifications()

Then:

func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {        
        application.registerForRemoteNotifications()
}

Followed by didRegisterForRemoteNotificationsWithDeviceToken & didFailToRegisterForRemoteNotificationsWithError.

Then, I am using the relatively new method:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {...}

According to the documentation and this link , as oppose to the old version of didReceiveRemoteNotification , this method is called if the app was terminated (as oppose to calling will/did finishLaunchingWithOptions).

However, if there was a push (which was received - I can see it on the screen) and I launch the app after it has been terminated, this method does not seem to be called as the code that handles the push (simply post a notification so it is picked up by the respective ViewController) does not get called.

What am I missing? Is there an additional check I need to do in didFinishLaunchingWithOptions? Somewhere else?

Managed to solve the problem of intercepting a Remote Push when the app is terminated for ios 9.1 with the following but it failed on 9.2 (random failure?):

Register for remote:

if #available(iOS 9, *) {

            let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)

            //            UIApplication.sharedApplication().registerUserNotificationSettings(settings)
            //
            //            UIApplication.sharedApplication().registerForRemoteNotifications()

            application.registerUserNotificationSettings(settings)

            application.registerForRemoteNotifications()

} else if #available(iOS 8.0, *){
  register for 8...
} else { //ios 7
  register for 7...
}


if let _ = launchOptions {

       if let _ = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary {

                handleRemotePush()

            } else if let _ = launchOptions?[UIApplicationLaunchOptionsLocalNotificationKey] as? UILocalNotification {

                handleLocalNots()

            } else {

                handleElse()

        }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM