aws-sdk-js: MessageStructure: 'json' is buggy?

I am trying to send a JSON message to an iOS client. When I send the message as plain text it work just fine. However, setting the “MessageStructure: ‘json’” and then the “Message” as a JSON object, as detailed in the AWS document I get a funny error: “InvalidParameterType: Expected params.Message to be a string”

If you want to send different messages for each transport protocol, set the value of the MessageStructure parameter to json and use a JSON object for the Message parameter. See the Examples section for the format of the JSON object. (From: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/frames.html)

I am sending:

params = {
  MessageStructure: 'json',
  Message: { 
      default: 'A notification from us!',
      APNS_SANDBOX: {aps:{alert:"Hello and have a good day."}}
   },
   TargetArn: '...'
};

(I have also tried sending the APNS_SANDBOX as an escaped string, same issue…) To me this seems like a major bug.

Any ideas?

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 34 (4 by maintainers)

Most upvoted comments

Found the issue about the “default” overriding the APNS property’s value; the “Massage” itself must be encoded as a valid string, say JSON.stringify() for example. The trick is applying the same to the “APNS_SANDBOX”. For example:

params = {
  MessageStructure: 'json',
  Message: JSON.stringify({
      default: "I am default":
      APNS_SANDBOX: JSON.stringify({aps:{alert:"Hello and have a good day."}})
   }),
   TargetArn: '...'
};

The above will make AWS SNS “ignore” the default and actually deliver what is expected. I wish I had access to the SNS documentation, I would make quite some changes.

Anyway, hope this helps somebody.

That’s incredible that still in 2017 we have to dig in such old issues. Why not mention this example on the docs?

Anyway thank you @aichholzer 👍

Same issue here. Serious issues with the documentation and would never have figured this json -> string stuff out myself. Thanks, and please, someone fix those docs 😕

And yes, I agree; the AWS documentation has room for a lot of improvement. A JSON encoded string is not a JSON object, not to me, at least.

@aichholzer You’re still saving people hours and hours of headaches! Thanks!

Thanks you so much for this answer!

@gloopeezza most welcome, I know how frustrating it can be.