Search code examples
iosobjective-cswiftremote-notifications

After Receiving Notification how can i can get payload on app icon click?


My push notification works properly in Normal case but i have problem with following case:

1) when my app remove from background and get notification & tap on app icon then i want to push view controller and display payload data in that view controller.

2 ) when my app in background and get notification & tap on app icon then i want to push view controller and display payload data in that view controller.

following is my userInfo

{
    aps =     {
        alert = "Call from rohan panchal";
        appointmentId = 220;
        badge = 0;
        "call_token" = "T1==cGFydG5lcl9pZD00NTI1ODY1MiZzaWc9MzM1MmM0M2E2MjkwN2JiYWMzNjgyNjk0MjFlZWMyNWEzNTZmZmM3MjpzZXNzaW9uX2lkPTJfTVg0ME5USTFPRFkxTW41LU1UUTNNREl3TVRBd01qVXdOWDV3WXpCRFMyWTRlR2xhUWpGdU1YbFpNamhvV0hoNFVHTi1VSDQmY3JlYXRlX3RpbWU9MTQ3MDIwMTAwMiZyb2xlPXB1Ymxpc2hlciZub25jZT0xNDcwMjAxMDAyLjUyMDM0NDAzNjQzMjMmZXhwaXJlX3RpbWU9MTQ3MDgwNTgwMg==";
        doctorId = 238;
        "doctor_country" = US;
        "doctor_name" = "John smith";
        patientId = 239;
        "patient_country" = US;
        "patient_name" = "Lottry patel";
        sessionId = "2_MX40NTI1ODY1Mn5-MTQ3MDIwMTAwMjUwNX5wYzBDS2Y4eGlaQjFuMXlZMjhoWHh4UGN-UH4";
        sound = default;
    };
}

following my code.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

// when i remove app from background & click on notification then following code run.
        NSDictionary *notificationPayload = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];

        if(notificationPayload)
        {
               NSLog(@"%@",notificationPayload);  
              WebViewController *DashBoard = [[WebViewController alloc]initWithNibName:@"WebViewController" bundle:nil];
              self.navcntrl=[[UINavigationController alloc]initWithRootViewController:DashBoard];

        }

        else
        {

              DoctorMenuViewController *DoctorVC = [[DoctorMenuViewController alloc]initWithNibName:@"DoctorMenuViewController" bundle:nil];
             self.navcntrl=[[UINavigationController alloc]initWithRootViewController:DoctorVC];
        }

}

When I got notification then following method called.

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{

    NSLog(@"%@",userInfo);

     WebViewController *DashBoard = [[WebViewController alloc]initWithNibName:@"WebViewController" bundle:nil];

     [self.navcntrl pushViewController:DashBoard animated:YES];


}

Please help.Any help appreciated.


Solution

  • The push notification payload consists of:

    alert - the alert string and actions

    badge

    sound

    content-available

    The key content-available is a new feature, and it is this key that makes silent push possible.

    To enable, you also have to add remote-notifcation as your app UIBackgroundModes as described here.

    This is what happens when content-available is in the payload:

    If app is Suspended, the system will bring it into Background

    If app was killed by user, nothing happens and app remains in Not Running Read about app state changes.

    A potential is pitfall:

    You enable with content-available=1. But, it is WRONG to disable with content-available=0. To disable, you have to REMOVE the key in the payload.

    plz use this

    -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
        {
           if(application.applicationState == UIApplicationStateInactive) 
        {
               NSLog(@"Inactive");
    
           //do your things when you click on notification
        }
        else if (application.applicationState == UIApplicationStateBackground)
         {
    
                        NSLog(@"Background");
    
    
        }
          else if (application.applicationState == UIApplicationStateActive)
          {
          NSLog(@"Active");
          }
        }
    

    for more information plz read this link http://samwize.com/2015/08/07/how-to-handle-remote-notification-with-background-mode-enabled/