我正试图弄清楚如何检查推送通知的有效负载,以确定当用户从通知中打开应用程序时,会打开哪个视图。例如,如果通知上写着"x:test",则在点击通知时会打开视图x,如果通知中写着"y:test",则会打开视图y。
编辑:我想我应该澄清一下我不确定的部分。
我在didFinishLaunchingWithOptions:中有这个
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (notification)
{
// check payload then load appropriate view controller
}
如何检查有效负载中的某些文本,以确定要加载的适当视图控制器?
这是我过去一个项目的代码。通知显示在设备中,如下所示:"Kostas:希望将您添加为好友"。
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
NSDictionary *remoteNotif =
[launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (remoteNotif) {
NSString *name = [[NSString alloc] init];
NSString *message = [[NSString alloc] init];
// 'wants to add you as friend.'
NSString* alertValue = [[remoteNotif valueForKey:@"aps"] valueForKey:@"alert"];
NSMutableArray* parts = [NSMutableArray arrayWithArray:[alertValue componentsSeparatedByString:@": "]];
name = [parts objectAtIndex:0];
[parts removeObjectAtIndex:0];
message = [parts componentsJoinedByString:@": "];
if ([message isEqualToString:@": wants to add you as friend."]) {
UITabBarController *tabController = (UITabBarController *)self.window.rootViewController;
// tabController.delegate = self;
tabController.selectedIndex = 1;
}
else{
UITabBarController *tabController = (UITabBarController *)self.window.rootViewController;
// tabController.delegate = self;
tabController.selectedIndex = 2;
[self addMessageFromRemoteNotification:remoteNotif updateUI:NO];
}
接收推送通知在应用程序代理中的两个位置进行处理
-
从推送通知启动应用程序时的
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
。在这种情况下,您的推送通知数据包含在[launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]
中 -
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
当您的应用程序在运行时收到通知。在这种情况下,userInfo
是您的推送通知数据。