How to catch all iOS Push Notifications with different user actions including tap on app icon How to catch all iOS Push Notifications with different user actions including tap on app icon ios ios

How to catch all iOS Push Notifications with different user actions including tap on app icon


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:

  • FOREGROUND:

    • notification appears without user's action:

      func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {    //handle your notification}

    You can display banner using third party library: BSForegroundNotification

  • BACKGROUND

    • notification appears on the screen. (Note that setting content-available=1 in a push notification can lead to the latest push message being visible once the app icon is pressed, as didReceive... is called).

      //nothing is called in the app, but app icon badge changes// OR - if the notification contains the field content-available set to 1 - func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {    //handle your notification} 
    • user tap on notification

      func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {    //handle your notification}           
    • user take notification action

      func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forRemoteNotification userInfo: [NSObject : AnyObject], completionHandler: () -> Void) {     //handle your notification's action}

      or

      func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forRemoteNotification userInfo: [NSObject : AnyObject], withResponseInfo responseInfo: [NSObject : AnyObject], completionHandler: () -> Void) {     //handle your notification's action response info}
    • user tap app icon

      func applicationDidBecomeActive(application: UIApplication) {    //fetch pending notifications from server}
  • NOT RUNNING AT ALL

    • notification appears on the screen.

      //nothing is called in the app, but app icon badge changes
    • user tap on notification

      func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {    if let userInfo = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? [NSObject: AnyObject] {        //handle your notification    }}
    • user take notification action

      func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forRemoteNotification userInfo: [NSObject : AnyObject], completionHandler: () -> Void) {     //handle your notification's action}

      or

      func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forRemoteNotification userInfo: [NSObject : AnyObject], withResponseInfo responseInfo: [NSObject : AnyObject], completionHandler: () -> Void) {     //handle your notification's action response info}
    • user tap app icon

      func applicationDidBecomeActive(application: UIApplication) {    //fetch pending notifications from server}

How to handle notification?

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

  2. 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:


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;}