0

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.

Maulik shah
  • 1,532
  • 18
  • 41

4 Answers4

1

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/

balkaran singh
  • 2,628
  • 1
  • 13
  • 29
  • hi it's working but when my app is terminate from background then it's not working – Maulik shah Aug 03 '16 at 07:34
  • are you check remote notification in background modes @Maulik ? – balkaran singh Aug 03 '16 at 07:49
  • yes i checked..everything work fine but when i remove app from background then it's not work on tapping appicon – Maulik shah Aug 03 '16 at 09:09
  • you mean your app is killed and notification come and you click on notification banner that time it's not work ? or you launch app from clicking on app icon.? – balkaran singh Aug 03 '16 at 09:17
  • @Maulik if launch app form icon click its work normal ass app lauch UIApplicationLaunchOptionsRemoteNotificationKey is nil this time. if your app is killed and you launch app from click on notification banner the you can get notification value form UIApplicationLaunchOptionsRemoteNotificationKey. – balkaran singh Aug 03 '16 at 09:29
  • yes i know that it's nil when you click on app icon but it's client requiremen – Maulik shah Aug 03 '16 at 09:36
  • is it any solution to read notification payload when i click on appicon – Maulik shah Aug 03 '16 at 09:37
  • you can clear all notification on app launch but you can not get info of notification on app launch . and if you have more than one notification in your notification list how you identify which notification info you need. – balkaran singh Aug 03 '16 at 09:44
  • i check on skype it's working on icon tap when call comes from desktop – Maulik shah Aug 03 '16 at 11:47
  • @Maulik you can save the notification info in nsuserdefaults. then use them on app launch and clear the nsuserdefaults value after the action. – balkaran singh Aug 03 '16 at 11:50
  • @Maulik it not work when your app is killed buz no method will get called when app was killed. in active and background its work. – balkaran singh Aug 03 '16 at 12:04
  • ya i know that you told me previously but i want to know how skype handling this – Maulik shah Aug 03 '16 at 12:09
  • @Maulik may be in skype they use some api for reading notification from server side and provide the data in api on launch time. – balkaran singh Aug 03 '16 at 12:22
0
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.

Sujit
  • 478
  • 5
  • 19
  • you are right.but when i remove app from background then notification comes from server then i tap on app then call this method application:(UIApplication *)application didFinishLaunchingWithOptions... but payload not get any data – Maulik shah Aug 03 '16 at 06:13
  • Yes. That method will get called. You can write as self.window.rootViewController = self.navcntrl. But if you want push animation to show your WebViewController then you need one more view controller on which you can push your controller. – Sujit Aug 03 '16 at 06:15
0

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.

Avi
  • 7,150
  • 1
  • 18
  • 22
0

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 
    } 
 }
gurmandeep
  • 1,166
  • 1
  • 11
  • 28
  • not work.when i tap on appicon then this method not called -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo – Maulik shah Aug 03 '16 at 06:29
  • The method is called when the notification arrives, you need to keep a check into the application that was there any recent notification. Then when you open the application check under didFinishLaunchingWithOptions for any notification. Refer http://stackoverflow.com/questions/16393673/detect-if-the-app-was-launched-opened-from-a-push-notification – gurmandeep Aug 03 '16 at 06:33
  • You are saying that if there has been a new notification and you click on the icon and open the app and then you want to navigate to the desired controller? – gurmandeep Aug 03 '16 at 06:44
  • Try http://weblog.plexobject.com/?p=1680 or http://code.tutsplus.com/tutorials/setting-up-push-notifications-on-ios--cms-21925 – gurmandeep Aug 03 '16 at 06:51