我试图找出如何检查推送通知的有效负载,以便确定当用户从通知中打开应用时打开哪个视图。例如,如果通知说" x:test"当点击通知并且通知显示" y:test"视图你会打开。
编辑:我想我应该澄清一下我不确定的部分。我在didFinishLaunchingWithOptions中有这个:
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (notification)
{
// check payload then load appropriate view controller
}
如何检查某些文本的有效负载以确定要加载的相应视图控制器?
答案 0 :(得分:1)
在app delegate
中的两个地方处理接收推送通知 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
。在这种情况下,您的推送通知数据包含在[launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
。在这种情况下,userInfo
是您的推送通知数据。
答案 1 :(得分:1)
这是我过去项目的代码。通知显示在这样的设备中" 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];
}