Question

I am trying to publish to SNS using the SNSPublishRequest but I can't figure out how to format the JSON to actually use the parameters set for the aps dictionary. If I put in something for the "default" it will send that message. But if I add the APNS dictionary as well, it seems to not show anything. This is how I am formatting the JSON request - am I missing something?

NSDictionary *parameters = @{@"default" : @"",
                             @"APNS" : @{@"aps": @{@"alert": @"hello"}}};
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parameters
                                                   options:0
                                                     error:&error];

NSString *JSONString = [[NSString alloc] initWithBytes:[jsonData bytes] length:[jsonData length] encoding:NSUTF8StringEncoding];
NSLog(@"JSON OUTPUT: %@",JSONString);

SNSPublishRequest *pr = [[SNSPublishRequest alloc] initWithTopicArn:@"someTopic" andMessage:JSONString];
pr.messageStructure = @"json";
Was it helpful?

Solution

I think SNS's APNs dictionary expects an JSON encoded string format itself. We have to escape all the " and add the \s

NSString* JSONString = @"{\"default\": \"<enter your message here>\",\"APNS_SANDBOX\":\"{\\\"aps\\\":{\\\"alert\\\":\\\"<HELLO>\\\"}}\"}";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top