收到通知后如何才能在应用程序图标点击上获得有效负载?

时间:2016-08-03 05:43:21

标签: ios objective-c swift remote-notifications

我的推送通知在正常情况下正常工作,但我遇到以下情况的问题:

1)当我的应用从后台删除并获得通知&点击应用程序图标,然后我想推送视图控制器并在该视图控制器中显示有效负载数据。

2)当我的应用在后台获取通知&点击应用程序图标,然后我想推送视图控制器并在该视图控制器中显示有效负载数据。

以下是我的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;
    };
}

关注我的代码。

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

}

当我收到通知时,请按照以下方法调用。

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

    NSLog(@"%@",userInfo);

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

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


}

请帮忙。非常感谢。

4 个答案:

答案 0 :(得分:1)

推送通知有效负载包括:

alert - 警报字符串和操作

徽章

内容可用的

关键内容 - 可用是一项新功能,正是这一关键使得无声推送成为可能。

要启用,您还必须添加remote-notifcation作为您的应用UIBackgroundModes,如此处所述。

当有效内容可用时,会发生这种情况:

如果app已暂停,系统会将其带入后台

如果应用被用户杀死,则没有任何反应,应用仍处于“未运行”状态 阅读有关应用状态更改的信息。

潜力是陷阱:

使用content-available = 1启用。但是,使用content-available = 0禁用是错误的。要禁用,您必须删除有效负载中的密钥。

使用此

-(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");
      }
    }

有关更多信息,请阅读此链接http://samwize.com/2015/08/07/how-to-handle-remote-notification-with-background-mode-enabled/

答案 1 :(得分:0)

application: didReceiveRemoteNotification:  
当操作系统收到RemoteNotification并且应用程序正在运行时(在后台/暂停或在前台),将调用

application:(UIApplication *)application didFinishLaunchingWithOptions
当应用程序未运行且您单击应用程序图标时,将调用

1)当我的应用从后台删除并获得通知&点按应用图标 -

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

在TestViewController中,您可以推送控制器以显示有效负载数据。

2)无论你写的是什么都是正确的。    希望这会对你有所帮助。

答案 2 :(得分:0)

应用程序在特定情况下提供通知数据。如果用户的交互与其中一个不一致,则数据不可用。没有例外。

  1. 用户点按通知横幅(或提醒)。应用程序是否正在运行并不重要。
  2. 有效负载的content-available字段设置为真值, iOS选择提供通知。
  3. 注意:这个答案没有解释如何调用或处理任何场景。

答案 3 :(得分:0)

来自userinfo的字段值是您在

下所需的值
-(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 
    } 
 }