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.
Asked
Active
Viewed 6,030 times
8
-
you can open your app by tapping on notification ..its already in build – vaibhav Sep 21 '16 at 14:16
-
Possible duplicate of [How to know Push Notification delivery status](http://stackoverflow.com/questions/25830597/how-to-know-push-notification-delivery-status) – makadev Sep 21 '16 at 14:24
3 Answers
2
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.

ohr
- 1,717
- 14
- 15
1
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")
}

random
- 8,568
- 12
- 50
- 85
-
How is this detecting if you received a push notification and did not launch from it? – ohr Sep 21 '16 at 15:03
-
1I am interested in detecting when a notification was received and the user launched the app directly from the spring board after that and not from the notification alert itself – M_Waly Sep 21 '16 at 15:14
-
I tried and unfortuantely doesn't work , as the method doesn't get called unless the app is working in background – M_Waly Sep 22 '16 at 10:47
-
1@M_Waly you don't have your push notification setup correctly then. You have to have the ` content-available` flag set in it. See this post http://stackoverflow.com/questions/19390868/apple-push-notification-in-background-issue/19403462#19403462 – random Sep 22 '16 at 13:43
-
Changing `content -available` changes how notifications are delivered, so not always suitable. IMO this answer doesn't answer original question: 1 - the app is not launched at all (not on background); 2 - notification is received (could be notification with top priority, so cannot have `content -available` set to 1 as suggested); 3 - user launches the app from icon, not notification --> how to detect outstanding notifications? – timbre timbre Apr 08 '19 at 16:44
0
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// ...
if (launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]) {
[self application:application didReceiveRemoteNotification:launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]];
}
}
This should work.
The moment you open the app, the notifications you have received in notification center that you will get as a payload in didFinishLaunchingWithOptions
and then you can perform the specific operation.

Bista
- 7,869
- 3
- 27
- 55

Hitesh Arora
- 9
- 2
-
3only if the app was in background or foreground or launched from a notification alert , what about the case when it is neither in background or foreground but received a push notification ,then launched normally from spring board? – M_Waly Sep 22 '16 at 09:27