I am using AWS SNS (v2.0) for my iOS application (iOS 8) to send remote notifications directly to another user. There is no proxy server for notification. But constantly, I have this problem, when as soon as I want to send a JSON message, as shown below, I get an error. When I return to the plain text, the notification is delivered and received without problems.
let sns = AWSSNS.defaultSNS()
let request = AWSSNSPublishInput()
request.messageStructure = "json"
var notificationKeys = MONotificationKeys()
var aps: NSMutableDictionary = NSMutableDictionary()
aps.addEntriesFromDictionary(["alert": "Hello World"])
aps.addEntriesFromDictionary(["sound": "sound.wav"])
aps.addEntriesFromDictionary(["badge": 1])
var raw1: NSDictionary = NSDictionary(dictionary: ["aps":aps])
var raw2: NSDictionary = NSDictionary(dictionary: ["APNS_SANDBOX":raw1])
var dataWithJSON = NSJSONSerialization.dataWithJSONObject(raw2, options: NSJSONWritingOptions.allZeros, error: nil)
request.message = NSString(data: dataWithJSON!, encoding: NSUTF8StringEncoding)
request.targetArn = targetEndpointARN
sns.publish(request).continueWithExecutor(BFExecutor.mainThreadExecutor(), withSuccessBlock: { (task: BFTask!) -> AnyObject! in
println(task.result)
return nil
}).continueWithExecutor(BFExecutor.mainThreadExecutor(), withBlock: { (task: BFTask!) -> AnyObject! in
if (task.error != nil) {
println("Error: \(task.error.userInfo)")
}
return nil
})
And the error:
Error: Optional([Code: InvalidParameter,
Message: Invalid parameter: JSON must contain an entry for 'default' or 'APNS_SANDBOX'., __text: (
"\n ",
"\n ",
"\n ",
"\n "),
Type: Sender])
Printout message:
{ "APNS_SANDBOX" :
{
"aps" : {
"sound" : "mo.wav",
"badge" : 1,
"alert" : "Hello World"
}
}
}
Do you guys know what causes this error? THANK!
source
share