AWS SNS直接从iOS发送远程通知始终出错

时间:2014-11-24 03:21:14

标签: swift amazon-web-services ios8 apple-push-notifications

我在我的iOS应用程序(iOS 8)中使用AWS SNS(v2.0)将远程通知直接发送给其他用户。通知中间没有代理。但是不断地,我有这个问题,只要我想发送如下的JSON消息,我就会收到错误。当我切换回纯文本时,通知会在没有问题的情况下发送和接收。

    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"
  }
 }
}

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

5 个答案:

答案 0 :(得分:3)

我从AWS论坛得到了这个答案:

  

SNS Publish Message是一个JSON字典,其中键和值都必须是字符串。键“APNS_SANDBOX”的值是字典而不是字符串。请将JSON值转义为字符串并传递它。

然后它起作用:D

答案 1 :(得分:2)

这个人花了很长时间才弄明白。 AWS的SNS文档非常糟糕。以下是使用swift发布主题的方法。每个平台都需要是一个编码的字符串,如果你搞砸了,SNS只会传递你的默认信息。

func publishPush() {
    let sns = AWSSNS.defaultSNS()
    let request = AWSSNSPublishInput()
    request.messageStructure = "json"

    var dict = ["default": "The default message", "APNS_SANDBOX": "{\"aps\":{\"alert\": \"YOUR_MESSAGE\",\"sound\":\"default\", \"badge\":\"1\"} }"]

    let jsonData = NSJSONSerialization.dataWithJSONObject(dict, options: nil, error: nil)
    request.message = NSString(data: jsonData!, encoding: NSUTF8StringEncoding) as! String
    request.targetArn = "blahblahblah:MyTopic"
    sns.publish(request).continueWithBlock { (task) -> AnyObject! in
        println("error \(task.error), result:; \(task.result)")
        return nil
    }

}

答案 2 :(得分:0)

请将您的代码放在这里以使答案更清晰。 我寄了:

{
    "default" : "ENTER YOUR MESSAGE",
    "APNS_SANDBOX" : {
        "aps" : {
            "badge" : 1,
            "alert" : "hello vietnam"
        }
    }
}

然后我总是收到相同的消息内容:“输入你的消息”而不是“你好越南”

非常感谢!

答案 3 :(得分:0)

@leonard是对的

在Objective-C中相同

NSDictionary *sampleMessage = @{
                                @"default": @"This is the default message which must be present when publishing a message to a topic. The default message will only be used if a message is not present one of the notification platforms",
                                @"APNS_SANDBOX": @"{\"aps\":{\"alert\": \"YOUR_MESSAGE\",\"sound\":\"default\", \"badge\":\"1\"} }"
                                };
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:sampleMessage options:NSJSONWritingPrettyPrinted error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

AWSSNS *sns = [AWSSNS defaultSNS];
AWSSNSPublishInput *message = [[AWSSNSPublishInput alloc] init];
message.subject = @"test";
message.targetArn = [[NSUserDefaults standardUserDefaults] objectForKey:@"endpointArn"];
message.message = jsonString;
message.messageStructure = @"json";

[[sns publish:message] continueWithBlock:^id _Nullable(AWSTask<AWSSNSPublishResponse *> * _Nonnull task) {

    if (task.error) {
        NSLog(@"The request failed. Error: [%@]", task.error);
    }
    if (task.exception) {
        NSLog(@"The request failed. Exception: [%@]", task.exception);
    }
    if (task.result) {
        //Do something with the result.
    }
    return nil;
}];

答案 4 :(得分:-3)

APNS_SANDBOX:

 JSON.stringify({
            aps: {
                alert: "hello vietnam"
            }
        })

尝试这样来得到你好的越南而不是输入你的消息。