Issue in parsing moengage notification response in iOS?

310 Views Asked by At

I am getting an issue in parsing Moengage notification response which is below from

From:

   -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
{
  NSLog(@"notification appdelegate  %@",userInfo);
  [self customPushHandler:userInfo];

}

notification app delegate: { "app_extra" = { screenData = { "" = ""; }; screenName = ""; }; aps = { alert = "iOS Test "; badge = 1; "content-available" = 0; sound = default; }; moengage = { "" = ""; cid = ; }; }

- (void) customPushHandler:(NSDictionary *)notification {

 if (notification !=nil && [notification objectForKey:@"app_extra"] != nil) {
    NSDictionary* app_extra_dict = [notification objectForKey:@"app_extra"];

      NSDictionary* app_extra_dict1 = [[notification objectForKey:@"app_extra"]objectForKey:@"aps"];

      NSDictionary* app_extra_dict2 = [[notification objectForKey:@"aps"];

      NSLog(@"Moenage notification %@",notification);
      NSLog(@"Menage   apps  %@",app_extra_dict1);
      NSLog(@"Moenage apps %@",app_extra_dict2);               
      NSLog(@"Moenage %@",app_extra_dict );

   }
  }

Log:

Moengage notification :Same as above response

Menage apps (null)

Moenage apps (null)

Moenage:

{ screenData = { "" = ""; }; screenName = ""; }

Now my issue is I am trying to retrieve " aps = { alert = "iOS Test ";" ..But this not is JSON..can any please suggest me to parse this response or is their way to retrieve "iOS test" from this response

3

There are 3 best solutions below

1
soumya On BEST ANSWER

Solved this by converting above response to Jsonstring and than to NSDictionary:

   - (void) customPushHandler:(NSDictionary *)notification {

 if (notification !=nil && [notification objectForKey:@"app_extra"] != nil) {

    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:notification
                                                       options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
                                                         error:&error];

    if (! jsonData) {
        NSLog(@"Got an error: %@", error);
    } else {
        NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

        NSLog(@"Got jsonString: %@", jsonString);

        NSError *jsonError;
        NSData *objectData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objectData
                                                             options:NSJSONReadingMutableContainers
                                                               error:&jsonError];


         NSLog(@"json %@",json[@"aps"][@"alert"]);


    }

}

Console:

2016-01-29 12:28:06.613 json iOS Test

5
Justlike On

Try to print like this:

- (void) customPushHandler:(NSDictionary *)notification {
    NSLog(@"notification:%@", notification);
    NSLog(@"Moenage:%@", notification[@"app_extra"]);
    NSLog(@"Menage apps:%@", notification[@"aps"]);
}
0
Chengappa C D On

There is nothing wrong with the format you are getting data and there is no need to convert to JSON, you are already getting the data in a NSDictionary. Your answer where you are converting the dictionary to JSON and again converting the JSON for getting the same dictionary doesn't make any sense. You can simply access all the values by using the keys as below :

- (void) customPushHandler:(NSDictionary *)notification {

if (notification !=nil && [notification objectForKey:@"app_extra"] != nil) {

    NSDictionary* app_extra_dict = [notification objectForKey:@"app_extra"];
    NSDictionary* aps_dict = [notification objectForKey:@"aps"];

    NSLog(@"Moengage notification : %@",notification);
    NSLog(@"Moengage   appsExtra : %@",app_extra_dict);
    NSLog(@"Moengage aps : %@",aps_dict);
}
}

And below are the logs for the same :

    Moengage notification :  {
    "app_extra" =     {
        screenData =         {
            key1 = Val1;
        };
        screenName = Screen1;
    };
    aps =     {
        alert = "Hello!!!";
        badge = 1;
        "content-available" = 0;
        sound = default;
    };
    moengage =     {
        cid = 5715f243597b7b0f37a9254a;
        key1 = Val1;
    };
}

 Moengage   appsExtra :  {
    screenData =     {
        key1 = Val1;
    };
    screenName = Screen1;
}

Moengage aps :  {
    alert = "Hello!!!";
    badge = 1;
    "content-available" = 0;
    sound = default;
}