0

hi i am new to iOS so please help me ..

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

        NSDictionary *segueDictionary = [userInfo valueForKey:@"aps"];
        NSLog(@"%@",userInfo);
        NSString * string=[[NSString alloc]initWithFormat:@"%@",[segueDictionary valueForKey:@"vendor_data"]];}

the output is

{
    aps =     {
        alert = "Get Upto 50% Off ";
        sound = default;
        "vendor_data" =         {
            cat = "Baby & kids";
            "closing_hours" = "09:00 PM";
            latitude = "28.57089323";
            longitude = "77.32666539";
            "offer_desc" = "Get Upto 50% Off On All Kids Clothing.";
            "offer_id" = 1128;

            "offer_in_percentage" = "Get Upto 50% Off ";
            "offer_item_name" = Lilliput;
            "offer_valid_upto" = "2015-10-30";
            "opening_hours" = "10:00 AM";
            "phone_no" = "*********";
            pincode = 0;
            rating = 0;
            "sub_category" = "Clothing,Footwear";
            title = "Offer Of The Day";
            "vendor_address" = "***********";
            "vendor_id" = "lliputkids.com";
            "vendor_location" = "*****";
            "vendor_name" = Lilliput;
            "vendor_type" = shop;
        };
    };
}

this coming from push notification data

NSString * string = {
cat = "Baby & kids";
"closing_hours" = "09:00 PM";
latitude = "28.57089323";
longitude = "77.32666539";
"offer_desc" = "Get Upto 50% Off On All Kids Clothing.";
"offer_id" = 1128;
"offer_in_percentage" = "Get Upto 50% Off ";
"offer_item_name" = Lilliput;
"offer_valid_upto" = "2015-10-30";
"opening_hours" = "10:00 AM";
"phone_no" = "********";
pincode = 0;
rating = 0;
"sub_category" = "Clothing,Footwear";
title = "Offer Of The Day";
"vendor_address" = "shop.no- 6, sun market";
"vendor_id" = "illiputkids.com";
"vendor_location" = "*******";
"vendor_name" = Lilliput;
"vendor_type" = shop;}

this data is in string format but i want in dictionary format i am getting data in string format. so please help me Thanks in advance

iOS Developer
  • 138
  • 2
  • 18

4 Answers4

1

I believe you are misinterpreting the JSON format.

NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

Now if you do following NSLog statement

NSLog(@"%@",[json objectForKey:@"sub_category"]);

It will give you:- Clothing,Footwear

pkc456
  • 8,350
  • 38
  • 53
  • 109
1

The userInfo parameter of didReceivePushNotification returns an NSDictionary object.

You can get the vendor_data dictionary directly with

NSDictionary *vendorDataDictionary = [[userInfo objectForKey:@"aps"] objectForKey:@"vendor_data"];

or with subscripting

NSDictionary *vendorDataDictionary = userInfo[@"aps"][@"vendor_data"];

The method valueForKey: is a special KVC method. The standard method to get an object for a key is objectForKey:

vadian
  • 274,689
  • 30
  • 353
  • 361
1

Please check with this.

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

    NSDictionary *segueDictionary = [userInfo valueForKey:@"aps"];
    NSLog(@"%@",userInfo);
    NSDictionary *vendorDic = [segueDictionary valueForKey:@"vendor_data"];
    NSString *vedorAddress = [vendorDic valueForKey:@"vendor_address"];
}

Note: in {key:value,key:value} is dictionary formate.but value itself may be a dictionary. i.e {key:{key:value},key:{key:value,key:value}}

Jamil
  • 2,977
  • 1
  • 13
  • 23
  • beacuse i am sure "vendor_data" return a dictionary – Jamil Oct 06 '15 at 07:48
  • NSLog(@“vedorAddress:%@”, vedorAddress);//shop.no- 6, sun market //you will see – Jamil Oct 06 '15 at 07:55
  • thanks it's success .. but i want now one thing ? why down voting ? – iOS Developer Oct 06 '15 at 07:57
  • because they think this data has come from webservice. so blindly convert with "NSJSONSerialization". unfortunately, they did not know push notification does not retun NSData, it returns NSDictionary directly (may be nested dictionary in your case). You may also download vote which are wrong answer. accept /upvote on write answer. so that other may find the right answer eassily. i am also upvoting you as you are right. – Jamil Oct 06 '15 at 08:03
  • can you tell me the difference b/w "valueForKey"and"valueForKeyPath" – iOS Developer Oct 13 '15 at 07:59
  • valueForKeyPath is used when you key contains period(.). For example, [vendorDic valueForKey:@"vendor.address"]; – Jamil Oct 13 '15 at 08:23
1

When you send a remote notification, its payload is formatted in JSON. Roughly explained, it's just all your objects put together in dictionaries & arrays so you can have a clear hierarchy of your objects in the payload.

What you're doing now is just putting that JSON in a string, and instead of having separate objects (arrays and dictionaries containing objects), you have one long string containing all of them.

You need to do the following

  • Make sure you send a valid formatted JSON in your notification payload. I don't know how you're sending it so I can't really help you there.

  • Once you receive the push, you have a userInfo dictionary which you can navigate using :objectForKey. This is your JSON, already transformed as an Objective-C object.

You should see your complete structure in the dictionary. Objects and keys, with the proper hierarchy.

If you see one single string in your aps key or other custom key, then you're sending the data wrong

But if you have

  • aps
    • alert
    • message
    • sound
  • YourCustomKey
    • a custom object
      • another custom subobject
    • etc.

With that kind of structure in your userInfo then it means you're good to go. It's easy to find with a breakpoint on the first line of your notification handling and look at what's in userInfo.

Then just look at whats in there and fetch it manually, but if you created the notification payload yourself (which you probably did) then you should know which arrays/dictionary contains what objects, and just fetch it manually.

//I'm just giving EXAMPLES : 
NSDictionary *corePayload = [userInfo objectForKey:@"aps"];
NSDictinoary *firstLevel = [userInfo objectForKey:@"vendor_data"];
float latitude = [[firstLevel objectForKey:@"latitude"]floatValue];

and like that you can fetch anything and everything else.

Note that from what you showed, your content is not formatted properly, some of your keys have "" and some don't, which is not normal. So I suggest you go have a real look at what you're sending, and follow all my steps carefully. And if your first step is not correct, don't go to the next step ;)

Hope you'll find what you're looking for !

Gil Sand
  • 5,802
  • 5
  • 36
  • 78