从推送通知中将JSON有效负载数据转换为字符串

时间:2014-03-22 18:54:34

标签: ios objective-c json push-notification

我试图将推送通知中的JSON有效负载数据转换为字符串。

{
 aps = {
 alert = "BG push";

 sound = ,
 };
}

我研究了SO和Parse并尝试了各种方法,包括这个Apple Push Notification with Sending Custom Data但是我的字符串重新运行(null)或者在本例中以JSON格式

我希望字符串中包含alert数据"BG Push",以便将其置于警报视图中

 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

    if ( application.applicationState == UIApplicationStateActive ){

        // app was already in the foreground
        [PFPush handlePush:userInfo]; //<-----userInfo returns payload data in JSON format

    }

        else {                
            // app was just brought from background to foreground
            NSLog(@"App was in background and opened from Push message");

            UIAlertView *alert = [[UIAlertView alloc]
                                  initWithTitle:@"Megger"
                                        message: [NSString stringWithFormat:@"%@", userInfo]
                                       delegate:self
                              cancelButtonTitle: @"Ok"
                              otherButtonTitles: nil];

            [alert show];

        }
  }

2 个答案:

答案 0 :(得分:3)

NSString *alert = userInfo[@"aps"][@"alert"];

UIAlertView *alertView = [[UIAlertView alloc]
                              initWithTitle:@"Megger"
                              message: [NSString stringWithFormat:@"%@", alert]
                              delegate:self
                              cancelButtonTitle: @"Ok"
                              otherButtonTitles: nil];

[alertView show];

答案 1 :(得分:1)

怎么样

NSDictionary *temp = userInfo[@"aps"];
NSString *message = temp[@"alert"];

如果它不起作用,请添加此行,让我知道你得到了什么

NSLog( @"%@\r%@\r%@", userInfo, temp, message );