python集成魅族推送

魅族推送开发文档
魅族推送目前只提供java sdk。

请结合开发文档理解以下内容

1. 封装meizuPusher类,这里把SecretKey和APPID直接写死了(应该写到配置文件里的)

#meizuPush.py
# 我使用的是别名推送,自定义内容
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']), # 这里替换成你的推送内容
            },
            "clickTypeInfo": {
                "clickType": 3, # 3是自定义推送
                "customAttribute": str(dict(SUB_PUSH, **content)), # 这里是客户端需要的参数
            },
            "pushTimeInfo": {
                "offLine": 1,
                "validTime": 24,
            },
            "advanceInfo": {
                "suspend": 1,
                "clearNoticeBar": 1,
                "notificationType": {
                    "vibrate": 1,
                    "lights": 0,
                    "sound": 1,
                },
                "notifyKey": str(dict(SUB_PUSH, **content)['code']), # 推送内容去重,这里是以推送状态码中的code为唯一标识,重复的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.如何调用

# 魅族推送
meizupush = meizuPusher(phone)
try:
    print(meizupush.push(content={"content": "你的推送内容", "title": "你的title"}))
except Exception as e:
    print('meizu推送报错', e)

你可能感兴趣的:(python集成魅族推送)