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()
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With