Reputation: 14793
Sending the push message through the AWS Management Console works just fine using the JSON message generator
. But whenever i call the publish()
function, the message never reaches the phone.
Publishing to iOS works just fine like so:
import boto3
client = boto3.client('sns', region_name=REGION_NAME)
client.publish(TargetArn=SOME_VALID_ARN, Message='This message gets pushed to iOS')
Doing this with GCM/Firebase endpoints just does not work. I tried an ridiculous amount of json.dumps()
or manual quotation mark escaping combinations.
I hope this question saves someone some time and frustration.
Upvotes: 0
Views: 1200
Reputation: 14793
The publish call that actually works needs TWO nested json.dumps
:
client.publish(TargetArn=ARN, MessageStructure='json', Message=json.dumps({'GCM': json.dumps('This finally gets delivered to Android')}))
First of all, the boto3 SNS documentation of the publish() function is pretty confusing:
If you want to send the same message to all transport protocols, include the text of the message as a String value.
Wrong, GCM, a transport protocol that nearly all android devices use, does not work this way! Maybe this should be mentioned.
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.
No, you can't use a JSON object for the Message parameter. The parameter still needs to be a string object. But it needs to contain parsable JSON. This should be reworded.
the value of the Message parameter must:
be a syntactically valid JSON object; and
contain at least a top-level JSON key of "default" with a value that is a string.
Neither point is true.
Also, the fact that you need another json.dumps()
within the JSON object is mentioned nowhere. A simple example like the one above would have helped wonders in that documentation.
Upvotes: 5