0

Is it any way to known when an iOS app is launched, if Push notifications were sent to the device ? ( I want to access to the payload to retrieve information from the notification )

Thanks,

Eran
  • 387,369
  • 54
  • 702
  • 768
CZ54
  • 5,488
  • 1
  • 24
  • 39

2 Answers2

1

There are methods from UIApplicationDelegate from where you see if there is notification received

You could see in your AppDelegate didFinishLaunchingWithOptions method if user has launced the app with the notifications e.g

UILocalNotification *notif =
    [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
    if (localNotif) {
        irLog(@"Recieved Notification");
    }

for local notification you have posted you can have a look at this method

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif

for remote notifications you can have a look at this method

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
nsgulliver
  • 12,655
  • 23
  • 43
  • 64
1

You should add something like this to your code :

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

    NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];

    //Accept push notification when app is not open
    if (remoteNotif) {      
        [self handleRemoteNotification:application userInfo:remoteNotif];
        return YES;
    }

    return YES;
}

Note that you will only get the push notification payload if the app was launched by tapping the notification. If it was launched by tapping the app icon, you won't get the payload.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • to be picky, the suggested handler method should probably have the signature -handleRemoteNotification:remoteNotif application:application – jwilkey Dec 18 '14 at 00:01