When my App is not running and receives a Push Notification, if I click on that notification, the App is launched - but then it doesn\'t prompt the
Although this has been answered, none of the provided answer actually worked correctly. Here is my implementation that I was able to get working.
This code goes in application:didFinishLaunchingWithOptions:
Swift:
if let options = launchOptions, notification = options[UIApplicationLaunchOptionsRemoteNotificationKey] as? [NSObject : AnyObject] {
// NOTE: (Kyle Begeman) May 19, 2016 - There is an issue with iOS instantiating the UIWindow object. Making this call without
// a delay will cause window to be nil, thus preventing us from constructing the application and assigning it to the window.
Quark.runAfterDelay(seconds: 0.001, after: {
self.application(application, didReceiveRemoteNotification: notification)
})
}
Objective-C:
if (launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]) {
[self application:application didReceiveRemoteNotification:launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]];
}
A couple notes:
Possibly due to the way iOS queues allocations, the root UIApplication.sharedApplication().window
object is nil when launching the application after being killed. Adding a 1 millisecond delay fixed this issue. Did not test this in Objective-C
Casting to [NSObject: AnyObject] was required to keep from having type conflicts but I did not experiment with this much; keep it in your mind when implementing in your own project. Objective-C does not require this.
Quark is just a fun little library I use in all my projects that contains helpful and commonly used extensions and methods. Simply use any form of delay that fits your application needs.
Swift 3 and xCode 8.3.2:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// your code here
// Check if app was not running in background and user taps on Push Notification
// This will perform your code in didReceiveRemoteNotification where you should handle different application states
if let options = launchOptions, let notification = options[UIApplicationLaunchOptionsKey.remoteNotification] as? [NSObject : AnyObject] {
self.application(application, didReceiveRemoteNotification: notification)
}
return true
}