AWS SNS直接发送远程通知,iOS的总误差误差、直接发送、通知、AWS

2023-09-11 11:39:18 作者:爱痛苦的根源

我使用AWS SNS(V2.0),为我的iOS应用程序(的iOS 8)直接发送远程通知给其他用户。有一个在中间的通知没有代理。但不断的,我有这样的问题,即,只要我想发出一个JSON消息如下,我会得到错误。当我切换到纯文本,该通知交付和接收没有问题。

I am using AWS SNS (v2.0) for my iOS app (iOS 8) to send out remote notifications directly to another user. There is no proxy in the middle for the notification. But constantly, I have this problem where as soon as I want to send out a JSON message as below, I will get error. When I switch back to pure text, the notification is delivered and received with no problem.

    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])

打印出来的信息是:

The print out of what the message is:

{ "APNS_SANDBOX" : 
 {
  "aps" : {
    "sound" : "mo.wav",
    "badge" : 1,
    "alert" : "Hello World"
  }
 }
}

你们知道是什么原因导致这个错误?谢谢!

You guys know what causes this error? THANKS!

推荐答案

我已经得到了来自AWS论坛这样的回答:

I've got this answer from AWS Forum:

SNS发布的消息是AA JSON字典其中两个键和值必须是字符串。关键APNS_SANDBOX的值是一个字典不是一个字符串。请逃避JSON价值为字符串,并把它传递。

SNS Publish Message is a a JSON dictionary where both key and value must be strings. The value of key "APNS_SANDBOX" is a dictionary not a string. Please escape the JSON value to a string and pass it.

然后,它的工作原理:D

Then it works :D