Android開發中經常需要測試GCM發送推送的情況,下面提供Python腳本來模擬推送過程。
#!/usr/bin/env python
# coding=utf8
import urllib2
import json
# 你的應用serverKey
serverKey = '*******************'
# 要推送手機的GCM token
device_token = "********************"
url = 'https://gcm-http.googleapis.com/gcm/send'
headers = {"Content-type": "application/json",
"Authorization": "key=" + serverKey
}
# 推送協議接口
data = {
"to": device_token,
"priority": "normal",
# "delay_while_idle": True,
# "time_to_live": 3600,
"data": {
# TODO 你的應用定義的推送協議接口
}
}
req = urllib2.Request(url, json.dumps(data), headers)
response = urllib2.urlopen(req)
print response
compressedData = response.read()
print compressedData