在选择接收到的推送通知时获取APNS有效载荷内容



我正在收到推送通知列表。如果用户选择通知,我想从选定的通知中获取数据。从通知列表

预先感谢

我所做的是

-(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];
    }
}

对于swift :通知数据已在application:didReceiveRemoteNotification:中传递到您的应用程序。如果要在applicationDidBecomeActive:中处理它,则应将其存储在application:didReceiveRemoteNotification:中,然后在applicationDidBecomeActive中再次阅读。

当您的应用程序在应用程序: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) {
    }
}

对于目标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;
}
-(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;
}

相关内容

  • 没有找到相关文章

最新更新