from urllib import request
import json
to = <fmc token>
url = 'https://fcm.googleapis.com/fcm/send'
key = <firebase key>
headers = {
'Content-Type' : 'application/json',
'Authorization' : 'key=' + key,
}
data = {
"to": to,
"content_available" : True,
"data": {
'info' : 'data to background mode',
},
'notification' : {
'title' : 'this is title',
'body' : 'this is body',
},
}
dataJson = json.dumps(data).encode('utf-8')
r = request.Request(url, dataJson, headers = headers)
result = request.urlopen(r)
result.getcode()
詳細的payload內容可以參考firebase的說明網頁:
Firebase Cloud Messaging HTTP protocol
https://firebase.google.com/docs/cloud-messaging/http-server-ref