如何在 iOS 中读取 applaunchwithoption 中的 payload?
How to read the payload in applaunchwithoption in iOS?
我已经为我的 iOS 应用实现了远程推送通知。它有一个自定义的有效载荷。喜欢这个
"aps": {
"alert": "joetheman",
"sound": "default"
},
"message": "Some custom message for your app",
"id": 1234
当点击锁屏通知时,根据id我想加载应用的不同屏幕(当应用不是运行,如果我收到通知并且id是2,应用应在打开 BookingViewController 的情况下打开)。那么我如何才能在 applaunchwithOption
和 notificationdidreceived
委托中读取自定义负载。
请帮助我。谢谢
您可以使用以下方法处理推送通知:---
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(@"%@",userInfo);
NSString *msg=[userInfo objectForKey:@"message"];
//Application is Running
if ( application.applicationState == UIApplicationStateActive ){
if ([[userInfo objectForKey:@"id"]isEqualToString:@"1"]) {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
YourViewController *obj = [[YourViewController alloc]initWithNibName:@"YourViewController" bundle:nil];
nav = [[UINavigationController alloc]initWithRootViewController:obj];
[self.nav setNavigationBarHidden:YES];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
}
else if ([[userInfo objectForKey:@"id"]isEqualToString:@"2"]) {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
YourViewController *obj = [[YourViewController alloc]initWithNibName:@"YourViewController" bundle:nil];
nav = [[UINavigationController alloc]initWithRootViewController:obj];
[self.nav setNavigationBarHidden:YES];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
}
}
//when Application in Background
else{
if ([[userInfo objectForKey:@"id"]isEqualToString:@"1"]) {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
YourViewController *obj = [[YourViewController alloc]initWithNibName:@"YourViewController" bundle:nil];
nav = [[UINavigationController alloc]initWithRootViewController:obj];
[self.nav setNavigationBarHidden:YES];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
}
else if ([[userInfo objectForKey:@"id"]isEqualToString:@"2"]) {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
YourViewController *obj = [[YourViewController alloc]initWithNibName:@"YourViewController" bundle:nil];
nav = [[UINavigationController alloc]initWithRootViewController:obj];
[self.nav setNavigationBarHidden:YES];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
}
}
}
我已经为我的 iOS 应用实现了远程推送通知。它有一个自定义的有效载荷。喜欢这个
"aps": {
"alert": "joetheman",
"sound": "default"
},
"message": "Some custom message for your app",
"id": 1234
当点击锁屏通知时,根据id我想加载应用的不同屏幕(当应用不是运行,如果我收到通知并且id是2,应用应在打开 BookingViewController 的情况下打开)。那么我如何才能在 applaunchwithOption
和 notificationdidreceived
委托中读取自定义负载。
请帮助我。谢谢
您可以使用以下方法处理推送通知:---
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(@"%@",userInfo);
NSString *msg=[userInfo objectForKey:@"message"];
//Application is Running
if ( application.applicationState == UIApplicationStateActive ){
if ([[userInfo objectForKey:@"id"]isEqualToString:@"1"]) {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
YourViewController *obj = [[YourViewController alloc]initWithNibName:@"YourViewController" bundle:nil];
nav = [[UINavigationController alloc]initWithRootViewController:obj];
[self.nav setNavigationBarHidden:YES];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
}
else if ([[userInfo objectForKey:@"id"]isEqualToString:@"2"]) {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
YourViewController *obj = [[YourViewController alloc]initWithNibName:@"YourViewController" bundle:nil];
nav = [[UINavigationController alloc]initWithRootViewController:obj];
[self.nav setNavigationBarHidden:YES];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
}
}
//when Application in Background
else{
if ([[userInfo objectForKey:@"id"]isEqualToString:@"1"]) {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
YourViewController *obj = [[YourViewController alloc]initWithNibName:@"YourViewController" bundle:nil];
nav = [[UINavigationController alloc]initWithRootViewController:obj];
[self.nav setNavigationBarHidden:YES];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
}
else if ([[userInfo objectForKey:@"id"]isEqualToString:@"2"]) {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
YourViewController *obj = [[YourViewController alloc]initWithNibName:@"YourViewController" bundle:nil];
nav = [[UINavigationController alloc]initWithRootViewController:obj];
[self.nav setNavigationBarHidden:YES];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
}
}
}