Reputation: 37581
If a push notification is displayed to the user and the user taps it and the app is brought to the foreground from a background state then how can the app get the payload of the notification?
Because the app is already running didFinishLaunchingWithOptions
: won't get called and because the app was in the background when the push arrived didReceiveRemoteNotification
: won't have been called.
Upvotes: 0
Views: 950
Reputation: 2877
There are two places so I usually make a method that handles both something like this:
- (void)handleMessageFromRemoteNotification:(NSDictionary *)userInfo
Then in: application:didFinishLaunchingWithOptions:
if ([launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]) {
[self handleMessageFromRemoteNotification:launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]];
}
also in: application:didReceiveRemoteNotification:
[self handleMessageFromRemoteNotification:userInfo
If you want to do something different if the app is running check application.applicationState == UIApplicationStateActive in didReceiveRemoteNotification
Upvotes: 1
Reputation: 37581
"because the app was in the background when the push arrived didReceiveRemoteNotification: won't have been called."
This, or didReceiveRemoteNotification:withExpirationHandler, should get called if the app is in the background and gets switched to the foreground when the user taps on the notification.
However I got into a situation when this wasn't working as the reason was the content of the push was incorrect, I can't remember the details but double check what's in there.
Upvotes: 0
Reputation: 1138
According to Apple documentation the method didFinishLauchingWithOptions: is called when the user taps the action button of the notification.
As a result of the presented notification, the user taps the action button of the alert or taps (or clicks) the application icon. If the action button is tapped (on a device running iOS), the system launches the application and the application calls its delegate’s application:didFinishLaunchingWithOptions: method (if implemented); it passes in the notification payload (for remote notifications) or the local-notification object (for local notifications).
Then in this method it is easy to recover the content of the notification by doing for example :
UILocalNotification *localNotif =
[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif) {
NSString *itemName = [localNotif.userInfo objectForKey:ToDoItemKey];
[viewController displayItem:itemName]; // custom method
app.applicationIconBadgeNumber = localNotif.applicationIconBadgeNumber-1;
}
Upvotes: 0