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