我有一个聊天应用程序,当发送新消息时,我的服务器会发送推送通知。我遇到的问题是如何将用户带到正确的视图?我正在发送一个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
在推送通知中您可以检索channelID
来自 userInfo 字典。
正如midhere所说——
1) 当应用程序在后台运行时 and 当应用程序在前台运行时
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) 当应用程序未启动时(关闭) than 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.
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)