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

4

38

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

Reprint answered 23/8, 2012 at 2:37 Comment(2)
That's not possible. The notification data is only passed to the app if it's launched in response to the notification and not when tapping on the app icon.Reduced
What information do you pass on with your push notifications? To me it sounds like you have made a design flaw if you cannot open your app conveniently without getting a push notification first.Jewell
B
39

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.

Barron answered 23/8, 2012 at 2:55 Comment(1)
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.Lenticularis
C
6

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

Christcrossrow answered 30/8, 2012 at 17:18 Comment(2)
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? ;)Hubbard
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 iconContemporary
O
4

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.

Osculate answered 23/10, 2015 at 9:20 Comment(1)
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.Unclench
B
-7

You should get the notification in the launchWithOptions method in your appDelegate something like this:

NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];   

    if(remoteNotif != nil){  
        //Handle your notification   
    }
Bagpipes answered 24/8, 2012 at 18:58 Comment(1)
The app is currently in background, and I click on the app from homescreen instead from notification.Reprint

© 2022 - 2024 — McMap. All rights reserved.