iOS Push Notification - How to get the notification data when you click on the app icon instead of notification

18,116

Solution 1

You can't get remote push payload by launching app from homescreen.

If the push data is important for app use, load it from your server after app launched.

Solution 2

@fannheyward answer is absolutely correct. You cannot get payload when application is launched by tapping app icon.

I have an idea, what if you get to know that some notification is pending when app is launched by tapping app icon. With this knowledge your app can fetch payload from your server.

You can set "Badge" in every such notification and on applicationDidBecomeActive you can check [application applicationIconBadgeNumber] > 0 to know that some notification is active. After fetching payload from your server you can set it to 0 like below

[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];

Please note: This means your app will have badge displayed over it when notification is received. I am not sure of the behaviour when badge is disabled by user from settings.

Solution 3

If your application target is over iOS7, you can do only if application is alive in backgroud.

In the capabilities settings at Xcode, you should enable Background Modes>Remote notifications, and write below code.

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{ 
    // save userInfo in NSUserDefaults
    completionHandler( UIBackgroundFetchResultNoData );
}

If you want to test it, it will be helpful to use https://github.com/acoomans/SimulatorRemoteNotifications

  • From the server side, make sure to set content-available property with a value of 1

For this to work I also had to check the background fetch box.

Share:
18,116
Howard
Author by

Howard

(your about me is currently blank)

Updated on June 08, 2022

Comments

  • Howard
    Howard almost 2 years

    Similar to this question: How do I access remote push notification data on applicationDidBecomeActive?

    But the different is how can you access the notification data when you are inapplicationDidBecomeActive and if you have clicked on the app icon instead of the push notification.

    The flow is: If you click on the push notification then didReceiveRemoteNotification will be triggered, but if you click on the original app icon, only applicationDidBecomeActive will be triggered and didReceiveRemoteNotification will not be called.

    I am looking for the later case so how can I access the push notification data.

    (Both case assuming the app is in background and not killed yet.)

  • Howard
    Howard over 11 years
    The app is currently in background, and I click on the app from homescreen instead from notification.
  • Kyle Clegg
    Kyle Clegg over 11 years
    To continue on this trajectory, you could send a small payload to your server whenever the app is launched, then have your server listen for that payload and push data immediately. This would be the closest workaround I can think of to accomplish your goal.
  • Timo
    Timo about 11 years
    I am using the same approach. Take note that the user could have notification alerts enabled but badges disabled for your app. Practically, though... who does that? ;)
  • wyu
    wyu over 7 years
    I tried this on a iphone 6 with iOS8, content-available=1, background modes remote notification, app in background. didReceieveRemoteNotification is called when the the notification is received. it is NOT CALLED when the app is opened. This does not answer the question.
  • Madhu S. Kapoor
    Madhu S. Kapoor over 6 years
    Regarding >>I am not sure of the behaviour when badge is disabled by user from settings - Application can still get the correct count event if badge is not displayed on the app icon