我正在使用AWS SNS(v2.0)爲我的iOS應用程序(iOS 8)直接發送遠程通知給其他用戶。中間沒有用於通知的代理。但不斷,我有這個問題,只要我想發出一個JSON消息如下,我會得到錯誤。當我切換回純文本時,通知就會順利發送和接收。AWS SNS直接從iOS發送遠程通知總是錯誤
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
})
和錯誤是:
Error: Optional([Code: InvalidParameter,
Message: Invalid parameter: JSON must contain an entry for 'default' or 'APNS_SANDBOX'., __text: (
"\n ",
"\n ",
"\n ",
"\n "),
Type: Sender])
打印出來的消息是什麼:
{ "APNS_SANDBOX" :
{
"aps" : {
"sound" : "mo.wav",
"badge" : 1,
"alert" : "Hello World"
}
}
}
你們知道是什麼原因導致這個錯誤?謝謝!
Ryan的帖子非常有幫助:-) – leonard 2015-03-11 07:46:28