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://admin.xmpush.xiaomi.com/zh_CN/app/nav2.业务场景
小程序端在访问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 APISenderfrom xmpush.base.APIMessage import *#------------------------------from common.core.http.response import JsONResponsefrom push.models import PushDevicefrom account.models import OAuthUserfrom push import settingsimport copyclass 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 ret3.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=17883.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,type3.4 在项目的其他接口中调用推送方法
PushService.push_to_user(app_user_ID,goods_visit_message(wx_user_ID,goods_name))更多推送姿势,有待在更多的业务场景中解锁!!! 总结
以上是内存溢出为你收集整理的python - 小米推送使用全部内容,希望文章能够帮你解决python - 小米推送使用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)