python - 小米推送使用
发布时间:2020-12-20 12:58:59 所属栏目:Python 来源:网络整理
导读:1. 小米文档及SDK下载 1.文档介绍https://dev.mi.com/console/doc/detail?pId=863sdk说明:2.开发者需要登录开发者网站(申请AppID,AppKey,AppSecret)。3.访问开发者网站下载SDK。地址:http://admin.xmpush.xiaomi.com/zh_CN/mipush/downpage4. 创建应用http
1. 小米文档及SDK下载1.文档介绍 https://dev.mi.com/console/doc/detail?pId=863 sdk说明: 2.开发者需要登录开发者网站(申请AppID,AppKey,AppSecret)。 3.访问开发者网站下载SDK。 地址:http://admin.xmpush.xiaomi.com/zh_CN/mipush/downpage 4. 创建应用 http://admin.xmpush.xiaomi.com/zh_CN/app/nav 2.业务场景小程序端在访问app端分享的商品后,app接收消息,某用户访问了某商品 3. 推送demo在实际项目中应用3.1 判断设备表消息,调对应的(ios,android)方法from django.conf import settings as project_settings # 引入sdk # ----------------------------- from xmpush.base.APIConstants import * from xmpush import APISender from xmpush.base.APIMessage import * #------------------------------ from common.core.http.response import JSONResponse from push.models import PushDevice from account.models import OAuthUser from push import settings import copy class PushService(object): @staticmethod def push_to_user(user_id,message): u""" 推送消息给指定的用户的设备 :param user_id: app端用户id :param message: 要推送给app端的消息 :return: """ record = PushDevice.objects.filter(user_id=user_id,app_name=settings.AppNameEnum.QQC).order_by('-modify_time').first() if not record: error = copy.deepcopy(qqc.ERROR["NOT_EXIST_ERR"]) error["msg"] = u"用户设备信息不存在" return JSONResponse(error=error) os_type = record.os_type # 别名推送 alias_id = u'QQC_%s' % user_id ret = None # 如果设备是ios if os_type == settings.DeviceOSEnum.iOS: ret = push_to_ios_user(alias_id,message) # 如果是Android elif os_type == settings.DeviceOSEnum.Android: ret = push_to_android_user(alias_id,message) return ret 3.2 不同系统对应的推送demo# android 系统 def push_to_android_user(alias_id,msg): """ :param alias_id: :type alias_id str :param msg: :type msg AppPushMessage :return: """ # android 不支持测试环境 Constants.use_official() message = PushMessage() # 创建应用时的包名 .restricted_package_name("") .title(msg[0]).description(msg[1]) .pass_through(0).payload('') # 通过extra中数据,app端扩展额外的判断 message.extra(msg[2]) # android key and secret sender = APISender("") recv = sender.send_to_alias(message.message_dict(),alias_id) return recv # ios系统 def push_to_ios_user(alias_id,msg): """ :param alias_id: :type alias_id str :param msg: :type msg AppPushMessage :return: """ # 环境判断 if project_settings.QQC_PUSH_MODE == 'production': Constants.use_official() else: Constants.use_sandbox() message = PushMessage() .description(msg[1]) # 创建应用时的bundle_id .restricted_package_name("") .badge(1) # 通过extra中数据,app端扩展额外的判断 message.extra(msg[2]) # ios key and secret sender = APISender("") return sender.send_to_alias(message.message_dict_ios(),alias_id) demo地址(里面有详细说明): https://dev.mi.com/console/doc/detail?pId=1788 3.3 拼接要推送的消息# 访问商品时推送的消息 def goods_visit_message(user_id,name): """ :param user_id: 用户id :param name: 商品名 :return: title,description """ user = OAuthUser.objects.filter(user_id=user_id).first() user_name = user.nickname title = u'您有一条新消息~' description = u'【%s】刚刚浏览了您的商品【%s】~' % (user_name,name) # 自己可定义type的用途 type = {"type": 2} return title,description,type 3.4 在项目的其他接口中调用推送方法PushService.push_to_user(app_user_id,goods_visit_message(wx_user_id,goods_name))
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |