Created
October 9, 2018 19:23
-
-
Save t0mdicks0n/51c04eeed676fc4820ed8cbc0c9d8d5b to your computer and use it in GitHub Desktop.
A short gist on how to post a push notifications on Android with Python and requests. Much easier to fiddle with this intuitive code when exploring FCM compared to for example Postman.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import json | |
body = { | |
"data":{ | |
"title":"Panprices", | |
"body":"Go online and shop!", | |
"url":"https://www.panprices.com" | |
}, | |
"notification": { | |
"title":"Panprices", | |
"body":"Has now a bunch of new offers", | |
"content_available": "true" | |
}, | |
"to":"<add a token to your user here>" | |
} | |
headers = {"Content-Type":"application/json", "Authorization": "key=<your server key here>"} | |
try : | |
res = requests.post( | |
'https://fcm.googleapis.com/fcm/send', | |
data=json.dumps(body), | |
headers=headers | |
) | |
except Exception as e: | |
print("There was an error: ", e) | |
finally : | |
print res.status_code |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment