Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect If push notifications were received when app is not launched and user didn't open the app through them

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.

like image 514
M_Waly Avatar asked Sep 21 '16 14:09

M_Waly


People also ask

Can you see past push notifications?

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).

Do push notifications work when app is closed iOS?

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.


2 Answers

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.

like image 52
ohr Avatar answered Oct 16 '22 23:10

ohr


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")
}
like image 26
random Avatar answered Oct 17 '22 01:10

random