Is it possible to detect if a push notification was received while the app was offline (not in background or foreground ? given the case that the user open the app directly from the spring board and not from the push notification alert.
Pull down your Notification Shade once and then scroll down to the bottom of your notifications. You should now see a History button (Figure 3). The History button has been added to your Notification Shade. Tap History to access your past 24 hours of notifications (Figure 4).
Apple does not offer a way to handle a notification that arrives when your app is closed (i.e. when the user has fully quit the application or the OS had decided to kill it while it is in the background). If this happens, the only way to handle the notification is to wait until it is opened by the user.
From documentation of
- application:didReceiveRemoteNotification:fetchCompletionHandler:
the system calls this method when your app is running in the foreground or background.
But, as you may have found out by your question
However, the system does not automatically launch your app if the user has force-quit it. In that situation, the user must relaunch your app or restart the device before the system attempts to launch your app automatically again.
In your AppDelegate
do this (sorry for swift implementation):
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// We see if we have a recent push notification date stored in the user defaults.
// If there hasn't been a recent one sent this if block will not be entered.
if let mostRecentPushNotificationDate = NSUserDefaults.standardUserDefaults().objectForKey("mostRecentPushNotification") as? NSDate {
// We find the difference between when the notification was sent and when the app was opened
let interval = NSDate().timeIntervalSinceDate(mostRecentPushNotificationDate)
// Check to see if we opened from the push notification
if let notification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? [String: AnyObject] {
print("we opened from the push notifcation and the time difference was \(interval) seconds.")
}
// We did not open from a push notification
else {
print("we opened from the spring board and the time difference was \(interval) seconds.")
}
// We remove the object from the store so if they open the app without a notification being sent we don't check this if block
NSUserDefaults.standardUserDefaults().removeObjectForKey("mostRecentPushNotification")
}
return true
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
// Set a flag when a push notification was sent
NSUserDefaults.standardUserDefaults().setObject(NSDate(), forKey: "mostRecentPushNotification")
}
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