在选择收到的推送通知时获取 APNS 有效负载内容 [关闭]
Posted
技术标签:
【中文标题】在选择收到的推送通知时获取 APNS 有效负载内容 [关闭]【英文标题】:Get APNS payload contents while selecting the received push notification [closed] 【发布时间】:2017-03-01 04:43:43 【问题描述】:我正在接收推送通知列表。如果用户选择一个通知,我想从该选定的通知中获取数据。从通知列表中
提前致谢
【问题讨论】:
你试过什么? @Anbu.Karthik 我已经发布了我的代码,请分享你的想法 @GeneCode 我已经发布了我的代码,请分享你的想法 【参考方案1】:我做的是
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
NSLog(@"Notification Received .. Dictionary %@",userInfo);
NSString *user_TYP = [[NSUserDefaults standardUserDefaults]valueForKey:@"User_TYPE_LOG"];
NSLog(@"Usr type == %@",user_TYP);
if ([user_TYP isEqualToString:@"type1"])
// VW_User_NOTific *VW_SER = [[VW_User_NOTific alloc]initWithNibName:@"VW_User_NOTific" bundle:nil];
VW_User_NOTific *listingVC = [[VW_User_NOTific alloc] init];
[(UINavigationController *)self.window.rootViewController pushViewController:listingVC animated:YES];
else
VW_JS_Notific *listingVC = [[VW_JS_Notific alloc] init];
[(UINavigationController *)self.window.rootViewController pushViewController:listingVC animated:YES];
【讨论】:
【参考方案2】:适用于 Swift:通知数据通过 application:didReceiveRemoteNotification:
传递到您的应用。
如果你想在applicationDidBecomeActive:
中处理它,你应该将它存储在application:didReceiveRemoteNotification:
并在applicationDidBecomeActive
中再次读取。
当您的应用在 application:didReceiveRemoteNotification:
中处于前台时,您可以查询 UIApplication.sharedApplication().applicationState
以了解您的应用是否已在前台。如果是,则立即处理通知。
如果应用是通过推送通知启动的,您还可以在
中查看推送数据 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
// app was launched from push notification, handling it
let remoteNotification: NSDictionary! = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary
if (remoteNotification != nil)
用于 Objective C:查询应用状态
您可以查询-[UIApplication applicationState]
以了解您的应用是否已经在前台以及应用何时从推送使用启动:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
//Check for options
if (launchOptions != nil)
//Store the data from the push.
dUserInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (dUserInfo != nil)
//Do whatever you need
return YES;
【讨论】:
谢谢,我会试试这个 非常感谢这项工作:)【参考方案3】:-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
NSLog(@"Notification Received .. Dictionary %@",userInfo);
NSString *user_TYP = [[NSUserDefaults standardUserDefaults]valueForKey:@"User_TYPE_LOG"];
NSLog(@"Usr type == %@",user_TYP);
// all the data required is present in your info dictionary
id data1 = userInfo[@"key1"]; // user your data_type instead of id
id data2 = userInfo[@"key2"];
if(received data == x && [user_TYP isEqualToString:@"type1"])
do_something;
else
do_something_else;
【讨论】:
它在 ios 10 中不起作用 @Anbu.Karthik 它正在工作以上是关于在选择收到的推送通知时获取 APNS 有效负载内容 [关闭]的主要内容,如果未能解决你的问题,请参考以下文章