2014-11-24 111 views
4

我正在使用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" 
    } 
} 
} 

你們知道是什麼原因導致這個錯誤?謝謝!

回答

3

我已經得到了來自AWS論壇這樣的回答:

SNS發佈消息是一個JSON字典其中兩個鍵和值必須是字符串。鍵「APNS_SANDBOX」的值是字典而不是字符串。請將JSON值轉義爲字符串並傳遞給它。

然後,它的工作原理:d

0

請把你的代碼在這裏做出的回答更加清晰。 我送:

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

,然後我總是收到同樣的短信內容:「輸入您的信息」,而不是「你好越南」

謝謝了!

+0

Ryan的帖子非常有幫助:-) – leonard 2015-03-11 07:46:28

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 
    } 

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

APNS_SANDBOX:

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

嘗試這樣讓你好越南,而不是輸入您的信息。