python消息推送_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
# 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
}
return news
common_push.py
#!/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
#!/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接口認真看看,我這主要用的是輔助通道
總結
以上是生活随笔為你收集整理的python消息推送_Python阿里云消息推送调用API的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python函数使用易错点_Python
- 下一篇: python 返回函数对象_返回函数