As per the Apple guide:
"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).
If the application icon is tapped on a device running iOS, the application calls the same method, but furnishes no information about the notification . If the application icon is clicked on a computer running Mac OS X, the application calls the delegate’s applicationDidFinishLaunching: method in which the delegate can obtain the remote-notification payload."
My question is
Suppose user got 3-4 Push notifications from provider and all are stored in apple's notification center. If user tapped on notification alert, he/she can easily get the notification data in the app. But if user tapped app icon on iPhone, how to get all the data related of all previous notifications.
Thanks in advance!
You can't, you will only receive information about the notification that was used to open your app.
So if a user opens your app, and your app has notifications, you will not be able to retrieve them from with in your app.
A work around could be to also keep track of notification on a server and handle this with in the app. Thus the server keeps track on which notification has been read. This is how Facebook does it.
To do it in a right way, some conditions must be met:
Your server knows about what your app currently have seen and what notifications it could send once again.
Let's consider only remote notifications. There are three states of app:
How to handle notification?
let notification = WLNotification(userInfo: userInfo)
Within WLNotification
remember to keep current application state when you receive notification. In future you may need it to know where that notification come from.
WLNetworkClient.sharedClient().notificationForIdentifier(notification.identifier)
Fetch from server details about that notification, and in the same time let it know that you REALLY get that notification, and effected on user's data.
How to fetch all pending notifications?
WLNetworkClient.sharedClient().pendingNotificationsWithCompletionBlock(nil)
Fetch all notifications you missed. in other words, fetch those ones, which were not marked in server as received by you.
Read Limitations of Apple Push Notifications.
See the related questions:
- How to ensure that iOS app received EVERY push notification send by server?
I had the same problem: if user clicks on push banner he gets info of push in app, if he clicks on app icon he doesn't get it. You can handle derivative one from it, but with some limits only. Example, if you want to have a badge number from push, you can do it:
(Push -> App icon -> App icon badge -> your var)
in AppDelegate
- (void)applicationWillEnterForeground:(UIApplication *)application
{
newMessages = application.applicationIconBadgeNumber;
}
Conceptually when application is loaded with clicking alert view presented for Push notification, than application is launched with didReceiveLocalNotification
delegate method if your application is in background. At this point applicationDidFinishLaunching
delegate method is not called.
When your application is not in background, clicking alert view presented for Push notification will call applicationDidFinishLaunching
method.
Hope this clears your understanding between these 2 delegate methods.
You can use this code:
NSArray *pendingNotifications = [[[UIApplication sharedApplication] scheduledLocalNotifications] sortedArrayUsingComparator:^(id obj1, id obj2) {
if ([obj1 isKindOfClass:[UILocalNotification class]] && [obj2 isKindOfClass:[UILocalNotification class]])
{
UILocalNotification *notif1 = (UILocalNotification *)obj1;
UILocalNotification *notif2 = (UILocalNotification *)obj2;
return [notif1.fireDate compare:notif2.fireDate];
}
return NSOrderedSame;
}];
// if there are any pending notifications -> adjust their badge number
if (pendingNotifications.count != 0)
{
//do something
}