2

我有一个聊天应用程序,当发送新消息时,我的服务器会发送推送通知。我遇到的问题是如何将用户带到正确的视图?我在推送通知中发送一个channelID,但我如何检索它并将用户带到实际的对话中?

我正在使用此代码来检测何时单击推送通知

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    if ( application.applicationState == UIApplicationStateInactive || application.applicationState == UIApplicationStateBackground  )
    {
         //opened from a push notification when the app was on background
    }
}
4

2 回答 2

10

如果您发送channelID推送通知,则可以channelID从 userInfo 字典中检索。正如midhere所说 -

1) 当应用程序在后台运行时当应用程序在前台运行时 application:didReceiveRemoteNotification:方法将调用如下。

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
if ( application.applicationState == UIApplicationStateInactive)
     {
     //opened from a push notification when the app was on background

       NSString channelID = [[userInfo objectForKey:@"aps"] objectForKey:@"channelID"];
       NSLog(@"channelID->%@",channelID);
     }
  else if(application.applicationState == UIApplicationStateActive)
     {
     // a push notification when the app is running. So that you can display an alert and push in any view

       NSString channelID = [[userInfo objectForKey:@"aps"] objectForKey:@"channelID"];
       NSLog(@"channelID->%@",channelID);
     }
}

2) 当应用程序未启动(关闭)时,application:didFinishedLaunchWithOptions将调用方法。

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  if (launchOptions != nil)
    {
         //opened from a push notification when the app is closed
        NSDictionary* userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
        if (userInfo != nil)
        {
            NSString channelID = [[userInfo objectForKey:@"aps"] objectForKey:@"channelID"];
            NSLog(@"channelID->%@",channelID);
        }

    }
     else{
             //opened app without a push notification.
         }
}
于 2013-11-13T10:21:30.963 回答
5

您将收到有关以下情况的推送通知。

  1. 当应用程序未启动时:通知将显示在通知中心,应用程序徽章编号将根据通知徽章详细信息更新。当用户点击通知中心的通知时,您的聊天应用程序将通过调用方法application:didFinishedLaunchWithOptions启动通知信息。您只需要检查remoteNotification 数据的选项字典。

  2. 当应用程序在前台运行时:您将在application:didReceiveRemoteNotification:中收到推送通知 ,您只需检查userInfo字典以获取远程通知数据。

  3. 当应用程序在后台运行时:通知将显示在通知中心,应用程序徽章编号将根据通知徽章详细信息更新。当用户从通知中心点击通知时,您的聊天应用程序将进入前台,您将在application:didReceiveRemoteNotification:中收到用户点击通知,您只需检查userInfo字典以获取远程通知数据。

获得通知字典后,您可以访问channelId并根据收到的channelId显示相应的聊天屏幕。

请参考苹果文档处理远程通知

于 2013-11-13T06:33:43.607 回答