Search code examples
iosobjective-cparsingmoengage

Issue in parsing moengage notification response in iOS?


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


Solution

  • 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