Python:极光推送(Jpush)

阅读: 评论:0

Python:极光推送(Jpush)

Python:极光推送(Jpush)

极光推送的整体流程:

APP -->注册过滤字段(device_id、robot.uuid)--> 后台指定过滤字段 -->极光服务器发送给指定APP

与Python交互

1、celery异步任务

@app.task
def jpush_message(method, **kwargs):log.info('jpush mesaage start')# 通过jpush_api调用jpush   APIif hasattr(jpush_api, method):method_func = getattr(jpush_api, method)response = method_func(**kwargs)log.info('jpush mesaage response: %s' % response)if response:return 'change state success'return 'change state error'return 'jpush error ,no have%s' % method

2、jpush  API

_jpush = jpush.JPush(JPUSH_APP_KEY, JPUSH_MASTER_SECRET)
_jpush.set_logging("DEBUG")import base64def alias(message, title=None, list_alias=None, list_tags=None):log.info("title :%s,  list_alias: %s list_tags:%s" % (str(title), str(list_alias), str(list_tags)))messages = base64.b64encode(str(message).encode('utf-8'))msg = messages.decode('utf-8')push = _ate_push()if list_alias:alias1 = {"alias": list_alias}if list_tags:if len(list_tags) > 1:tags = {"tag_and": list_tags}else:tags = {"tag": list_tags}if list_alias is None and list_tags is None:push.audience = jpush.audience('all')elif not list_alias:push.audience = tagselif not list_tags:push.audience = alias1else:push.audience = tags.update(alias1)if ification = ification(alert=ssage = ssage(msg)push.platform = jpush.all_push.options = {"apns_production": False}log.info(str(push.payload))try:push.send()except Exception as e:log.info('message: %s, except: %s' % (str(message), str(e)))return Falseelse:log.info('message: %s, success' % str(message))return True

3、使用

# 极光推送
content = {"task_res": -1, 'task_type': main_task, 'clean_mode': -1, 'error_code': error_code,"robot_name": bot_name}
data = MessageResponse(title='robot error', code=settings.JPUSH_MESSAGE_TYPE_DICT['robot_error'],content=content)
message = data.jsonify()
# 中文推送
cn_title = get_error_title(bot_name, error_code, "cn")
# "alias":推送的方法,message:推送的内容,list_tags:指定过滤字段,title:推送给主页面的主题
jpush_message('alias', message=message, list_tags=[robot_obj.uuid, settings.tag["cn"]],title=cn_title, )
# 英文推送
en_title = get_error_title(bot_name, error_code, "en")
jpush_message('alias', message=message, list_tags=[robot_obj.uuid, settings.tag["en"]],title=en_title, )

 

本文发布于:2024-02-01 22:24:58,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170679750039824.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:极光   Python   Jpush
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23