简体   繁体   中英

How to send push message through GCM/Firebase to an android device using boto3 client publish (AWS SNS)

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM