1

I'm getting this from my server pushnotification and this is in NSDictionary Format.

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler {

     NSLog(@"Recieved remote notification  %@", userInfo);
}

{
    aps =     {
        alert = "{\n      \"GCM\":{\n        \"data\":{\n          \"notificationType\":\"order\",\n          \"oid\":\"CRN14333313\",\n          \"c\":\"allocated\",\n          \"f\":\"253.633333333333\"   }\n      }\n    }";
    };
}

I'm trying to convert it to normal NSDictionary without any '\' or '\n'. How do i do that ?

Expected result in this NSDictionary format

{
    aps =     {
        alert = "{ 
                    "GCM"={ 
                         "data"={ 
                             "notificationType"="ons",
                             "oid"="N14333313",
                             "c"="allocated",
                             "f"="253.633333333333",

                            } 
                        }
                   }";
         };
}
Bangalore
  • 1,572
  • 4
  • 20
  • 50

4 Answers4

2

Try this,

NSError *jsonError;
NSData *objectData = [@"{\n      \"GCM\":{\n        \"data\":{\n          \"notificationType\":\"order\",\n          \"oid\":\"CRN14333313\",\n          \"c\":\"allocated\",\n          \"f\":\"253.633333333333\"   }\n      }\n    }" dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objectData
                                                     options:NSJSONReadingMutableContainers
                                                       error:&jsonError];
NSLog(@"dictionary is : %@",json);

I have keep your string as it is. You are getting json string as response so you need to convert it in json object like this way. Hope this will solve your issue. :)

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
1

plz check format of server data, it is not json format.it is just simple string

if you want to parse this data then try this, i think it helps you

NSString *alert_string[userInfo valueForKey:@"aps"] valueForKey:@"alert"];

NSData *jsondata=[alert_string dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *dictionary=[NSJSONSerialization JSONObjectWithData:jsondata options:NSJSONReadingAllowFragments error:nil];

by using this you can convert alert in required format

Ajharudeen khan
  • 246
  • 2
  • 10
0

Try this one

NSDictionary *dictionary = userInfo[@"aps"];

Sachin Amrale
  • 277
  • 1
  • 9
  • my problem is escape characters – Bangalore Apr 11 '16 at 12:42
  • NSDictionary *dictionary = userInfo[@"aps"][@"alert"][@"GCM"]; NSLog(@"dict: %@", dictionary); -[__NSCFString objectForKeyedSubscript:]: unrecognized selector sent to instance 0x1503ae6c0 – Bangalore Apr 11 '16 at 12:47
0

Your payload is incorrect! You should not send json in the alert. The text that's there will show up as a "notification" on the device.

This is how a correct payload with data looks:

{
    "aps" : {
        "alert" : {
            "title" : "Game Request",
            "body" : "Bob wants to play poker",
            "action-loc-key" : "PLAY"
        },
        "badge" : 5
    },
    "acme1" : "bar",
    "acme2" : [ "bang",  "whiz" ]
}

And if your payload is correct, so will your dictionary be. You won't need to parse anything.

I.e, in your case, the payload that you send from your server should look something like this (NOTE, this is not JSON but a pseudo description of how your JSON should look like):

{
    aps =     {
        alert = "Text that will show up as a push notification";
         };
    GCM={ 
            "data"={ 
                  "notificationType"="ons",
                  "oid"="N14333313",
                  "c"="allocated",
                  "f"="253.633333333333",
                   } 
         }
}

And you just get your GCM-dictionary with

NSDictionary *dictionary = userInfo[@"GCM"];

See this link for more info about the payload: https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/TheNotificationPayload.html#//apple_ref/doc/uid/TP40008194-CH107-SW1

ullstrm
  • 9,812
  • 7
  • 52
  • 83