Search code examples
iosnsnotification

Determine if user did not clicked on the notification, when app is in the background


Is there any way to find out after notification is been sent, how many users clicked on the notification and how many people didnt click on the noficiation event (badge) when the app is in the background?

I am more interested to find out how many people didnt click, as people who clicked can be tracked as app will go in the foreground and request can be made vs if app is in the background, your http request may get lost.


Solution

  • update your app delegate code to the following code:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
            [super application:application didFinishLaunchingWithOptions:launchOptions];
            NSDictionary *remoteNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    
            if(remoteNotif)
            {
                //launched from push notification
            }else{
                //Did not launch from push notification (tapped on app icon, or from multi tasking)
                //**Didn't click on notification**
            }
        }
    

    and this:

    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
        if([application applicationState] == UIApplicationStateActive) {
           // app was open, did not display the push alert/banner/badge
           // **Didn't click on notification**
        }else{
            //launched from push notification
        }
    }
    

    Its quite self explanatory. you can track when app was opened by tapping on a push notification and when it was opened without tapping on a notification.