AWS SNS sends remote notification directly from iOS always with an error

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!

+4
source share
5 answers

I have an answer from the AWS forum:

SNS - JSON, . "APNS_SANDBOX" - , . JSON .

: D

+3

. AWS SNS . . , , 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

, , . :

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

: " ", " "

!

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;
}];
0

APNS_SANDBOX:

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

, ENTER YOUR MESSAGE.

-3

All Articles