didReceiveRemoteNotification将用户转到正确的视图

我有一个聊天应用程序,当我的服务器发送新消息时发送推送通知。 我遇到的问题是如何让用户正确的看法? 我在推送通知中发送一个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 } } 

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

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) 当应用程序没有启动(closures)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. } } 

您将在以下情况下收到推送通知。

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

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

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

一旦你收到通知字典,你就可以访问channelId并根据收到的channelId显示各自的聊天画面。

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