Python阿里云消息推送调用API
很多公司测试APP推送时候,应该也是很头疼:推送环境:测试、正式,稍不注意就把测试的push到正式上,导致所有用户都收到
例子很多:
其实阿里、极光都有推送Api,直接调用API就ok,特别是有的公司有很多APP,直接调用API最方便:
代码量不多,直接贴代码:
config
aliyun.ini 配置阿里云的各种参数:各种key
data_news.py 维护推送的数据
devices.ini 维护需要推送的pushid
common_push.py api推送的封装
get_api_push.py 查询推送的结果
rmt_push.py 推送
aliyun.ini
# aliyun config
[fm]
accessKeyId = ''
accessKeySecret = ''
appKey = ''
regionId = ''
[hy]
accessKeyId = ''
accessKeySecret = ''
appKey = ''
regionId = ''
[hlj]
accessKeyId = ''
accessKeySecret = ''
appKey = ''
regionId = ''
devices.ini
# devices config
[fm]
android = 655b4e0a231742b7830ac658eb46c979
iOS = ''
[hy]
android = 18937640de964cffa3f770d3c997e8b5
iOS = ''
[hlj]
android = 23665a1c1a0746ababf408385f474d22,3296d6ce68124fa184c8c4bc6f1c66e3
iOS = ''
data_news.# coding=utf-8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | <em id = "__mceDel" > # get news # 获取推送新闻 def get_news(system_name, new_type): """ :param system_name: :param new_type: audio_news:xx新闻; subject_news:xx新闻; link_news:xx新闻; collect_news:xx新闻; ordinary_news:xx新闻 :return: """ news = '' # if system_name = = 'fm' : # xx新闻 if new_type = = 'audio_news' : news = { 'title' : '中西机场' , 'content' : '2018年12月客吞吐量突破5000万人次' , 'url_to' : 2 , 'detail_id' : 28ert36 , 'un_read' : 2 , 'flag' : 2 } # xx新闻 if new_type = = 'subject_news' : news = { 'title' : '中西部唯双流国际机场' , 'content' : '20年旅客吞吐量突破5000万人次' , 'url_to' : 1 , 'detail_id' : 1500erter59 , 'un_read' : 1 , 'flag' : 0 } # xx新闻 if new_type = = 'link_news' : news = { 'title' : '中西部唯机场' , 'content' : '2018年12年旅客吞吐量突破5000万人次' , 'url_to' : 1 , 'detail_id' : 1501ret263 , 'un_read' : 1 , 'flag' : 14 } # xx新闻 if new_type = = 'collect_news' : news = { 'title' : '高考语文炉' , 'content' : '高考语文作文题目纷选一:新时代青年、绿水青山图;上海:谈谈“被需要”的心态>>' , 'url_to' : 2 , 'detail_id' : 3289 , 'un_read' : 1 , 'flag' : 1 } # xx新闻 if new_type = = 'ordinary_news' : news = { 'title' : '中西国际机场' , 'content' : '2018年12月11日,成量突破5000万人次' , 'url_to' : 1 , 'detail_id' : 1345398 , 'un_read' : 1 , 'flag' : 0 }<br> return news < / em> |
common_push.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | #!/usr/bin/python # coding=utf-8 import json from datetime import * import configparser from aliyunsdkcore.client import AcsClient from aliyunsdkpush.request.v20160801 import PushRequest aly_path = "C:\\python\\thecover_project\\aliyun_push\\config\\aliyun.ini" device_path = "C:\\python\\thecover_project\\aliyun_push\\config\\devices.ini" aly_config = configparser.ConfigParser() aly_config.read(aly_path) device_config = configparser.ConfigParser() device_config.read(device_path) def ali_yun_push(rmt_name, push_system, push_content): """ :param rmt_name: 推送APP的别称 :param push_system: iOS或android :param push_content: 需要push的内容, 字典格式 :return: """ push_news = json.dumps(push_content, ensure_ascii = False ) access_key_id = aly_config[rmt_name][ 'accessKeyId' ] access_key_secret = aly_config[rmt_name][ 'accessKeySecret' ] region_id = aly_config[rmt_name][ 'regionId' ] app_key = int (aly_config[rmt_name][ 'appKey' ]) clt = AcsClient(access_key_id, access_key_secret, region_id) request = PushRequest.PushRequest() """ # 阿里推送参数参考:https://help.aliyun.com/knowledge_detail/48089.html """ request.set_AppKey(app_key) # 推送方式 这里是DEVICE,只给指定的设备推送,就不会出现测试推送给所有用户了 request.set_Target( 'DEVICE' ) request.set_accept_format( 'json' ) request.set_action_name( 'PUSH' ) # 推送标题/内容 request.set_Title(push_content[ 'title' ]) request.set_Body(push_news) # iOS推送 if push_system = = 'iOS' : request.set_TargetValue(device_config[rmt_name][ 'iOS' ]) request.set_DeviceType( "iOS" ) request.set_PushType( "NOTICE" ) # iOS应用图标右上角角标 request.set_iOSBadge( 0 ) request.set_iOSRemindBody(push_content[ 'content' ]) request.set_IOSMusic( "default" ) # 环境信息 DEV:表示开发环境,PRODUCT:表示生产环境 request.set_IOSApnsEnv( "PRODUCT" ) request.set_IOSExtParameters(push_news) request.set_StoreOffline( True ) request.set_IOSRemind( True ) # android推送 if push_system = = 'android' : request.set_TargetValue(device_config[rmt_name][ 'android' ]) request.set_DeviceType( "ANDROID" ) request.set_PushType( "MESSAGE" ) # 辅助通道弹窗配置 request.set_StoreOffline( True ) request.set_AndroidNotificationChannel( '1' ) request.set_AndroidRemind( True ) request.set_AndroidPopupTitle(push_content[ 'title' ]) request.set_AndroidPopupBody(push_content[ 'content' ]) request.set_AndroidPopupActivity( "XXXXActivity" ) request.set_AndroidExtParameters(push_news) request.set_AndroidNotifyType( "SOUND" ) # 通知栏自定义样式1-100 request.set_AndroidNotificationBarType( 1 ) request.set_AndroidOpenType( "ACTIVITY" ) # Android通知声音 request.set_AndroidMusic( "default" ) # 推送控制 # 30秒之后发送, 也可以设置成你指定固定时间 push_date = datetime.utcnow() + timedelta(seconds = + 30 ) # 24小时后消息失效, 不会再发送 expire_date = datetime.utcnow() + timedelta(hours = + 24 ) push_time = push_date.strftime( "%Y-%m-%dT%XZ" ) expire_time = expire_date.strftime( "%Y-%m-%dT%XZ" ) request.set_PushTime(push_time) request.set_ExpireTime(expire_time) result = clt.do_action_with_exception(request) return str (result, encoding = 'utf-8' ) |
rmt_push.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | #!/usr/bin/python # coding=utf-8 import config.data_news as news import common_push as push """ # xxx """ """ # xxAPP推送 news = news.get_news('fm', 'ordinary_news') rs = push.ali_yun_push('fm', 'android', news) """ """ # XXAPP推送 news = news.get_news('hy', 'ordinary_news') rs = push.ali_yun_push('hy', 'android', news) """ # xxAPP推送 news = news.get_news( 'hlj' , 'ordinary_news' ) rs = push.ali_yun_push( 'hlj' , 'android' , news) print (rs) |
差不多了,还不懂的自己去api接口认真看看,我这主要用的是辅助通道
本文来自博客园,作者:drewgg,转载请注明原文链接:https://www.cnblogs.com/drewgg/p/12574972.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义