魅族推送開發(fā)文檔
魅族推送目前只提供java sdk埋心。
請結(jié)合開發(fā)文檔理解以下內(nèi)容
1. 封裝meizuPusher類,這里把SecretKey和APPID直接寫死了(應(yīng)該寫到配置文件里的)
#meizuPush.py
# 我使用的是別名推送典鸡,自定義內(nèi)容
import hashlib
import requests
import json
from utils.codetable import SUB_PUSH
class meizuPusher:
def __init__(self, phone):
self.SecretKey = "你的SecretKey"
self.APPID = "你的APPID"
self.Alias = phone # 用戶的唯一標識寞酿,這里是使用的用戶手機號
self.Url = "http://server-api-push.meizu.com/garcia/api/server/push/varnished/pushByAlias"
self.Header = {
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
}
def push(self, content):
message = {
"noticeBarInfo": {
"noticeBarType": 0,
"title": '你的title',
"content": str(dict(**content)['content']), # 這里替換成你的推送內(nèi)容
},
"clickTypeInfo": {
"clickType": 3, # 3是自定義推送
"customAttribute": str(dict(SUB_PUSH, **content)), # 這里是客戶端需要的參數(shù)
},
"pushTimeInfo": {
"offLine": 1,
"validTime": 24,
},
"advanceInfo": {
"suspend": 1,
"clearNoticeBar": 1,
"notificationType": {
"vibrate": 1,
"lights": 0,
"sound": 1,
},
"notifyKey": str(dict(SUB_PUSH, **content)['code']), # 推送內(nèi)容去重,這里是以推送狀態(tài)碼中的code為唯一標識擦俐,重復(fù)的code就不會再推送
},
}
messageJson = json.dumps(message)
str2 = 'alias=' + self.Alias + 'appId=' + self.APPID + 'messageJson=' + messageJson + self.SecretKey
m = hashlib.md5()
b = bytes(str2, encoding='utf-8')
m.update(str2.encode())
md5_sign = m.hexdigest()
headers = self.Header
data = {
"alias": self.Alias,
"appId": self.APPID,
"messageJson": messageJson,
"sign": md5_sign,
}
ret = requests.post(url=self.Url, data=data, headers=headers)
return ret.content.decode('utf-8')
2.如何調(diào)用
# 魅族推送
meizupush = meizuPusher(phone)
try:
print(meizupush.push(content={"content": "你的推送內(nèi)容", "title": "你的title"}))
except Exception as e:
print('meizu推送報錯', e)