简体   繁体   中英

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.

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/

application: didReceiveRemoteNotification:  

is called when the OS receives a RemoteNotification and the app is running (in the background/suspended or in the foreground.)

and

application:(UIApplication *)application didFinishLaunchingWithOptions

is called when app is not running and you click on app icon.

1) when my app remove from background and get notification & tap on app icon -

TestViewController *testVC = your_test_vc;
UINavigationController *navVC = [[UINavigationController alloc]initWithRootViewController:testVC];

In TestViewController you can push your controller to show the payload data.

2) Whatever you wrote is correct. Hope this will help you.

Apps have notification data delivered in a specific set of circumstances. If the user's interaction does not coincide with one of these, the data is not available. There are no exceptions.

  1. the user taps a notification banner (or alert). It does not matter if the app was running or not.
  2. the payload has the content-available field set to a true value, and iOS opts to deliver the notification.

Note: this answer does not explain how to invoke or handle any of the scenarios.

The field value from the userinfo is what you require then under

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
   UIApplicationState state = [[UIApplication sharedApplication] applicationState]; 
   if (state == UIApplicationStateBackground || state == UIApplicationStateInactive)
    {      //notification is received when your app is in background
          //open the view controller you expected
    }
    else if(state == UIApplicationStateActive)
    { 
      //notification is received when your app is in foreground 
      //do nothing 
    } 
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM