How to handle remote notification with background mode enabled

3.4k Views Asked by At

I build and app that has Background Modes enabled, and the push notification payload that the app gets has "content-available" key.

This setup results in didReceiveRemoteNotification being called EVERY TIME the app gets a push notification, which means that if i get 3 push notifications while the app is in the background - the function will fire 3 times and the code inside it will be executed when the app will applicationDidBecomeActive

My biggest problem is that there is NO way to know if a user tapped the Push System Alert or tapped the app icon to bring the app from background, since regardless of the user's action, the didReceiveRemoteNotification will fire.

Is there a way to know for sure that the user tapped on the Sys alert?

and this: http://samwize.com/2015/08/07/how-to-handle-remote-notification-with-background-mode-enabled/ and other answers don't seem to be helpful

1

There are 1 best solutions below

1
On
For app is background push 
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    if ( application.applicationState == UIApplicationStateInactive || application.applicationState == UIApplicationStateBackground  )
    {
         //opened from a push notification when the app was on background
    }
}

For app is terminate state

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    if (launchOptions != nil) {
         // Launched from push notification
         NSDictionary *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

    }
}