PushService.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. # -*- coding: utf-8 -*-
  2. """
  3. @Time : 2022/5/19 11:43
  4. @Auth : Locky
  5. @File :PushService.py
  6. @IDE :PyCharm
  7. """
  8. import hashlib
  9. import json
  10. import logging
  11. import os
  12. import time
  13. import apns2
  14. import jpush
  15. import requests
  16. from pyfcm import FCMNotification
  17. from AnsjerPush.config import APP_BUNDLE_DICT, APNS_MODE, BASE_DIR, APNS_CONFIG, FCM_CONFIG, JPUSH_CONFIG, XMPUSH_CONFIG \
  18. , VIVOPUSH_CONFIG, OPPOPUSH_CONFIG, MEIZUPUSH_CONFIG, CONFIG_INFO, HONORPUSH_CONFIG
  19. from Model.models import UidPushModel
  20. from Object.RedisObject import RedisObject
  21. from Object.S3Email import S3Email
  22. from Service.CommonService import CommonService
  23. from Service.VivoPushService.push_admin.APIMessage import PushMessage
  24. from Service.VivoPushService.push_admin.APISender import APISender
  25. from AnsjerPush.config import LOGGER
  26. class PushObject:
  27. # 推送对象
  28. @staticmethod
  29. def get_msg_title(nickname):
  30. """
  31. 获取推送消息标题
  32. @param nickname: 设备名
  33. @return: msg_title
  34. """
  35. return nickname
  36. @staticmethod
  37. def get_gateway_msg_text(n_time, tz, lang, alarm):
  38. """
  39. 获取网关推送消息内容
  40. @param n_time: 当前时间
  41. @param tz: 时区
  42. @param lang: 语言
  43. @param alarm: 警报
  44. @return: msg_text
  45. """
  46. n_date = CommonService.get_now_time_str(n_time=n_time, tz=tz, lang=lang)
  47. if lang == 'cn':
  48. msg_text = '{} 日期:{}'.format(alarm, n_date)
  49. else:
  50. msg_text = '{} date:{}'.format(alarm, n_date)
  51. return msg_text
  52. @staticmethod
  53. def get_ai_msg_text(channel, n_time, lang, tz, label):
  54. """
  55. 获取AI推送内容
  56. @param channel: 通道
  57. @param n_time: 当前时间
  58. @param lang: 语言
  59. @param tz: 时区
  60. @param label: 识别到的标签
  61. @return: ai_msg_text
  62. """
  63. n_date = CommonService.get_now_time_str(n_time=n_time, tz=tz, lang=lang)
  64. if lang == 'cn':
  65. msg = '摄像头AI识别到了{}'.format(label)
  66. ai_msg_text = '{msg} 通道:{channel} 日期:{date}'.format(msg=msg, channel=channel, date=n_date)
  67. else:
  68. msg = 'Camera AI recognizes {}'.format(label)
  69. ai_msg_text = '{msg} channel:{channel} date:{date}'.format(msg=msg, channel=channel, date=n_date)
  70. return ai_msg_text
  71. @staticmethod
  72. def get_low_power_msg_text(channel, n_time, lang, tz, electricity, is_sys=0):
  73. """
  74. 获取低电量推送内容
  75. @param channel: 通道
  76. @param n_time: 当前时间
  77. @param lang: 语言
  78. @param tz: 时区
  79. @param electricity: 电量
  80. @param is_sys: 是否为系统消息
  81. @return: low_power_msg_text
  82. """
  83. n_date = CommonService.get_now_time_str(n_time=n_time, tz=tz, lang=lang)
  84. if lang == 'cn':
  85. alarm = '剩余电量 ' + electricity
  86. if is_sys:
  87. low_power_msg_text = '{} 通道:{}'.format(alarm, channel)
  88. else:
  89. low_power_msg_text = '{} 通道:{} 日期:{}'.format(alarm, channel, n_date)
  90. else:
  91. alarm = 'Battery remaining ' + electricity
  92. if is_sys:
  93. low_power_msg_text = '{} channel:{}'.format(alarm, channel)
  94. else:
  95. low_power_msg_text = '{} channel:{} date:{}'.format(alarm, channel, n_date)
  96. return low_power_msg_text
  97. @staticmethod
  98. def ios_apns_push(nickname, app_bundle_id, token_val, n_time, event_type, msg_title, msg_text,
  99. uid='', channel='1', launch_image=None):
  100. """
  101. ios apns 推送
  102. @param nickname: 设备昵称
  103. @param app_bundle_id: app包id
  104. @param token_val: 推送token
  105. @param n_time: 当前时间
  106. @param event_type: 事件类型
  107. @param msg_title: 推送标题
  108. @param msg_text: 推送内容
  109. @param uid: uid
  110. @param channel: 通道
  111. @param launch_image: 推送图片链接
  112. @return: bool
  113. """
  114. pem_path = os.path.join(BASE_DIR, APNS_CONFIG[app_bundle_id]['pem_path'])
  115. LOGGER.info('IOS推送: app_bundle_id:{}, pem_path:{}'.format(app_bundle_id, pem_path))
  116. try:
  117. cli = apns2.APNSClient(mode=APNS_MODE, client_cert=pem_path)
  118. alert = apns2.PayloadAlert(title=msg_title, body=msg_text, launch_image=launch_image)
  119. push_data = {'alert': 'Motion', 'msg': '', 'sound': '', 'zpush': '1', 'uid': uid, 'channel': channel,
  120. 'received_at': n_time, 'event_time': n_time, 'event_type': event_type, 'nickname': nickname,
  121. 'image_url': launch_image
  122. }
  123. payload = apns2.Payload(alert=alert, custom=push_data, sound='default', category='myCategory',
  124. mutable_content=True)
  125. n = apns2.Notification(payload=payload, priority=apns2.PRIORITY_LOW)
  126. res = cli.push(n=n, device_token=token_val, topic=app_bundle_id)
  127. LOGGER.info('IOS推送响应状态码{},params,uid:{},{}'.format(res.status_code, uid, json.dumps(push_data)))
  128. assert res.status_code == 200
  129. return True
  130. except Exception as e:
  131. LOGGER.info('IOS推送异常: {}, 证书路径: {}'.format(repr(e), pem_path))
  132. return False
  133. @staticmethod
  134. def android_fcm_push(nickname, app_bundle_id, token_val, n_time, event_type, msg_title, msg_text,
  135. uid='', channel='1', image=''):
  136. """
  137. android fcm 推送
  138. @param nickname: 设备昵称
  139. @param app_bundle_id: app包id
  140. @param token_val: 推送token
  141. @param n_time: 当前时间
  142. @param event_type: 事件类型
  143. @param msg_title: 推送标题
  144. @param msg_text: 推送内容
  145. @param uid: uid
  146. @param channel: 通道
  147. @param image: 推送图片链接
  148. @return: bool
  149. """
  150. try:
  151. serverKey = FCM_CONFIG[app_bundle_id]
  152. push_service = FCMNotification(api_key=serverKey)
  153. push_data = {'alert': 'Motion', 'msg': '', 'sound': 'sound.aif', 'zpush': '1', 'image': image,
  154. 'received_at': n_time, 'event_time': n_time, 'event_type': event_type, 'nickname': nickname,
  155. 'uid': uid, 'channel': channel
  156. }
  157. if event_type in [606, 607]:
  158. push_data['priority'] = 'high'
  159. push_data['content_available'] = True
  160. push_data['direct_boot_ok'] = True
  161. result = push_service.notify_single_device(registration_id=token_val, message_title=msg_title,
  162. message_body=msg_text, data_message=push_data,
  163. click_action='android.intent.action.VIEW',
  164. extra_kwargs={'default_sound': True,
  165. 'default_vibrate_timings': True,
  166. 'default_light_settings': True,
  167. },
  168. )
  169. LOGGER.info('fcm推送结果:{}'.format(result))
  170. return True
  171. except Exception as e:
  172. LOGGER.info('fcm推送异常:{}'.format(repr(e)))
  173. return False
  174. @staticmethod
  175. def android_jpush(nickname, app_bundle_id, token_val, n_time, event_type, msg_title, msg_text, channel=1):
  176. """
  177. android 极光 推送
  178. @param nickname: 设备昵称
  179. @param app_bundle_id: app包id
  180. @param token_val: 推送token
  181. @param n_time: 当前时间
  182. @param event_type: 事件类型
  183. @param msg_title: 推送标题
  184. @param msg_text: 推送内容
  185. @param channel: 设备通道
  186. @return: bool
  187. """
  188. try:
  189. app_key = JPUSH_CONFIG[app_bundle_id]['Key']
  190. master_secret = JPUSH_CONFIG[app_bundle_id]['Secret']
  191. # 换成各自的app_key和master_secret
  192. _jpush = jpush.JPush(app_key, master_secret)
  193. push = _jpush.create_push()
  194. push.audience = jpush.registration_id(token_val)
  195. if event_type in [606, 607]:
  196. channel_id = '111934'
  197. else:
  198. channel_id = '1'
  199. push_data = {'alert': 'Motion', 'msg': '', 'sound': 'sound.aif', 'zpush': '1', 'uid': nickname,
  200. 'received_at': n_time, 'event_time': n_time, 'event_type': event_type, 'nickname': nickname,
  201. 'channel': channel
  202. }
  203. android = jpush.android(title=msg_title, big_text=msg_text, alert=msg_text, extras=push_data,
  204. priority=1, style=1, alert_type=7, channel_id=channel_id
  205. )
  206. push.notification = jpush.notification(android=android)
  207. push.platform = jpush.all_
  208. res = push.send()
  209. assert res.status_code == 200
  210. return True
  211. except Exception as e:
  212. LOGGER.info('极光推送异常:{}'.format(repr(e)))
  213. return False
  214. @staticmethod
  215. def android_xmpush(channel_id, nickname, app_bundle_id, token_val, n_time, event_type, msg_title, msg_text,
  216. uid='', channel='1', image=''):
  217. """
  218. android 小米 推送
  219. @param channel_id: 通知通道
  220. @param nickname: 设备昵称
  221. @param app_bundle_id: app包id
  222. @param token_val: 推送token
  223. @param n_time: 当前时间
  224. @param event_type: 事件类型
  225. @param msg_title: 推送标题
  226. @param msg_text: 推送内容
  227. @param uid: uid
  228. @param channel: 通道
  229. @param image: 推送图片链接
  230. @return: bool
  231. """
  232. try:
  233. url = 'https://api.xmpush.xiaomi.com/v3/message/regid'
  234. app_secret = XMPUSH_CONFIG[app_bundle_id]
  235. # payload = {'alert': 'Motion', 'msg': '', 'sound': 'sound.aif', 'zpush': '1',
  236. # 'received_at': n_time, 'event_time': n_time, 'event_type': event_type, 'nickname': nickname,
  237. # 'uid': uid, 'channel': channel
  238. # }
  239. data = {
  240. 'title': msg_title,
  241. 'description': msg_text,
  242. 'payload': 'payload',
  243. 'restricted_package_name': app_bundle_id,
  244. 'registration_id': token_val,
  245. 'extra.channel_id': channel_id,
  246. 'extra.alert': 'Motion',
  247. 'extra.msg': '',
  248. 'extra.sound': 'sound.aif',
  249. 'extra.zpush': '1',
  250. 'extra.received_at': n_time,
  251. 'extra.event_time': n_time,
  252. 'extra.event_type': event_type,
  253. 'extra.nickname': nickname,
  254. 'extra.uid': uid,
  255. 'extra.channel': channel,
  256. }
  257. # if image:
  258. # data['extra.notification_style_type'] = 2
  259. # data['extra.notification_bigPic_uri'] = image
  260. headers = {
  261. 'Authorization': 'key={}'.format(app_secret)
  262. }
  263. response = requests.post(url, data=data, headers=headers)
  264. LOGGER.info("小米推送返回值:{}".format(response.json()))
  265. assert response.status_code == 200
  266. return True
  267. except Exception as e:
  268. LOGGER.info("小米推送异常:{}".format(repr(e)))
  269. return False
  270. @staticmethod
  271. def android_vivopush(token_val, n_time, event_type, msg_title, msg_text, app_bundle_id='', uid='', channel='1',
  272. image='', nickname='', appBundleId='', jg_token_val=''):
  273. """
  274. vivo 推送(不支持图片)
  275. @param app_bundle_id: app包名
  276. @param appBundleId: app包名
  277. @param token_val: 推送token
  278. @param jg_token_val: 极光推送token
  279. @param event_type: 事件类型
  280. @param msg_title: 推送标题
  281. @param msg_text: 推送内容
  282. @param n_time: 当前时间
  283. @param nickname: 设备昵称
  284. @param uid: uid
  285. @param image: 推送图片链接
  286. @param channel: 通道
  287. @return: bool
  288. """
  289. try:
  290. app_bundle_id = app_bundle_id if app_bundle_id != '' else appBundleId
  291. # 获取redis里面的authToken
  292. if msg_title == '':
  293. msg_title = APP_BUNDLE_DICT[app_bundle_id]
  294. app_id = VIVOPUSH_CONFIG[app_bundle_id]['ID']
  295. app_key = VIVOPUSH_CONFIG[app_bundle_id]['Key']
  296. app_secret = VIVOPUSH_CONFIG[app_bundle_id]['Secret']
  297. sender = APISender(app_secret)
  298. rec = sender.get_token(app_id, app_key)
  299. # 鉴权接口调用获得authToken
  300. sender_send = APISender(app_secret)
  301. sender_send.set_token(rec['authToken'])
  302. push_data = {'alert': 'Motion', 'msg': '', 'sound': 'sound.aif', 'zpush': '1', 'image': image,
  303. 'received_at': n_time, 'event_time': n_time, 'event_type': event_type, 'nickname': nickname,
  304. 'uid': uid, 'channel': channel
  305. }
  306. # 获取唯一标识符
  307. uid_push_qs = UidPushModel.objects.filter(token_val=token_val).values('m_code')
  308. m_code = uid_push_qs[0]['m_code'] if uid_push_qs[0]['m_code'] else ''
  309. # 推送 push_mode: 推送模式 (0:正式推送;1:测试推送,默认为0)
  310. # 推送 event_type: 消息类型 (0:运营类消息,1:系统类消息。默认为 0)
  311. # 推送 skip_type: 跳转类型(1:打开 APP 首页 2:打开链接 3:自定义 4:打开 app 内指定页面)
  312. activity = 'vpushscheme://com.vivo.pushvideo/detail'
  313. message = PushMessage() \
  314. .reg_id(token_val) \
  315. .title(msg_title) \
  316. .content(msg_text) \
  317. .push_mode(0) \
  318. .notify_type(3) \
  319. .skip_type(4) \
  320. .skip_content(activity) \
  321. .request_id(m_code) \
  322. .classification(1) \
  323. .client_custom_map(**push_data) \
  324. .message_dict()
  325. rec = sender_send.send(message)
  326. LOGGER.info('vivo推送结果:{}, 设备uid:{}'.format(rec, uid))
  327. if rec['result'] == 0 and event_type in [606, 607]:
  328. PushObject.jpush_transparent_transmission(msg_title, msg_text, app_bundle_id, jg_token_val, push_data)
  329. return True
  330. except Exception as e:
  331. LOGGER.info('vivo推送异常:{}'.format(e))
  332. return False
  333. @staticmethod
  334. def android_oppopush(channel_id, nickname, app_bundle_id, token_val, n_time, event_type, msg_title, msg_text,
  335. uid='', channel='1', image='', jg_token_val=''):
  336. """
  337. android oppo 推送
  338. @param channel_id: 通知通道id
  339. @param nickname: 设备昵称
  340. @param app_bundle_id: app包id
  341. @param token_val: 推送token
  342. @param jg_token_val: 推送token
  343. @param n_time: 当前时间
  344. @param event_type: 事件类型
  345. @param msg_title: 推送标题
  346. @param msg_text: 推送内容
  347. @param uid: uid
  348. @param channel: 通道
  349. @param image: 推送图片链接
  350. @return: bool
  351. """
  352. try:
  353. """
  354. android 国内oppo APP消息提醒推送
  355. """
  356. app_key = OPPOPUSH_CONFIG[app_bundle_id]['Key']
  357. master_secret = OPPOPUSH_CONFIG[app_bundle_id]['Secret']
  358. url = 'https://api.push.oppomobile.com/'
  359. now_time = str(round(time.time() * 1000))
  360. # 1、实例化一个sha256对象
  361. sha256 = hashlib.sha256()
  362. # 2、调用update方法进行加密
  363. sha256.update((app_key + now_time + master_secret).encode('utf-8'))
  364. # 3、调用hexdigest方法,获取加密结果
  365. sign = sha256.hexdigest()
  366. # 获取auth_token
  367. get_token_url = url + 'server/v1/auth'
  368. post_data = {
  369. 'app_key': app_key,
  370. 'sign': sign,
  371. 'timestamp': now_time
  372. }
  373. headers = {'Content-Type': 'application/x-www-form-urlencoded'}
  374. response = requests.post(get_token_url, data=post_data, headers=headers)
  375. result = response.json()
  376. # 发送推送
  377. push_url = url + 'server/v1/message/notification/unicast'
  378. extra_data = {'alert': 'Motion', 'msg': '', 'sound': 'sound.aif', 'zpush': '1',
  379. 'received_at': n_time, 'event_time': n_time, 'event_type': event_type, 'nickname': nickname,
  380. 'uid': uid, 'channel': channel
  381. }
  382. message = {
  383. "target_type": 2,
  384. "target_value": token_val,
  385. "notification": {
  386. "title": msg_title,
  387. "content": msg_text,
  388. 'channel_id': channel_id,
  389. 'action_parameters': extra_data,
  390. 'click_action_type': 1,
  391. 'click_action_activity': 'com.ansjer.zccloud_a.AJ_MainView.AJ_Home.AJMainActivity'
  392. }
  393. }
  394. push_data = {
  395. 'auth_token': result['data']['auth_token'],
  396. 'message': json.dumps(message)
  397. }
  398. response = requests.post(push_url, data=push_data, headers=headers)
  399. LOGGER.info("oppo推送返回值:{}".format(response.json()))
  400. if response.status_code == 200 and event_type in [606, 607]:
  401. PushObject.jpush_transparent_transmission(msg_title, msg_text, app_bundle_id, jg_token_val, extra_data)
  402. return True
  403. except Exception as e:
  404. LOGGER.info("oppo推送异常:{}".format(repr(e)))
  405. return False
  406. @staticmethod
  407. def android_meizupush(token_val, n_time, event_type, msg_title, msg_text, uid='', channel='1',
  408. app_bundle_id='', appBundleId='', nickname='', image=''):
  409. """
  410. android 魅族推送(不支持图片)
  411. @param app_bundle_id: app包名
  412. @param appBundleId: app包名
  413. @param token_val: 推送token
  414. @param event_type: 消息类型 (0:运营类消息,1:系统类消息。默认为 0)
  415. @param msg_title: 推送标题
  416. @param msg_text: 推送内容
  417. @param n_time: 当前时间
  418. @param nickname: 设备昵称
  419. @param uid: uid
  420. @param image: 推送图片链接
  421. @param channel: 通道
  422. @return: bool
  423. """
  424. try:
  425. # 获取包和AppSecret
  426. app_bundle_id = app_bundle_id if app_bundle_id != '' else appBundleId
  427. appId = MEIZUPUSH_CONFIG[app_bundle_id]['ID']
  428. appSecret = MEIZUPUSH_CONFIG[app_bundle_id]['AppSecret']
  429. url = 'https://server-api-push.meizu.com/garcia/api/server/push/varnished/pushByPushId'
  430. extra_data = {'alert': 'Motion', 'msg': '', 'sound': 'sound.aif', 'zpush': '1',
  431. 'received_at': n_time, 'event_time': n_time, 'event_type': event_type, 'nickname': nickname,
  432. 'uid': uid, 'channel': channel
  433. }
  434. # 转换为json格式
  435. extra_data = json.dumps(extra_data)
  436. if msg_title == '':
  437. msg_title = APP_BUNDLE_DICT[app_bundle_id]
  438. # 拼接发送内容
  439. activity = "com.ansjer.zccloud_a.AJ_MainView.AJ_Home.AJMainActivity" # 应用页面地址
  440. # clickType点击动作, 0打开应用, 1打开应用页面, 2打开url页面, 3应用客户端自定义
  441. messageJson = '{"clickTypeInfo": {"activity": "%s",' \
  442. '"clickType": 1, "parameters": %s },"extra": {},' % (activity, extra_data)
  443. noticeBarInfo = ('"noticeBarInfo": {"title": "%s", "content": "%s"},' % (msg_title, msg_text))
  444. noticeExpandInfo = '"noticeExpandInfo": {"noticeExpandType": 0}, "pushTimeInfo": {"validTime": 24}}'
  445. messageJson += noticeBarInfo
  446. messageJson += noticeExpandInfo
  447. data_meizu = {
  448. 'appId': appId,
  449. 'pushIds': token_val,
  450. 'messageJson': messageJson
  451. }
  452. # 魅族MD5加密,生成密钥
  453. sign = CommonService.getMD5Sign(data=data_meizu, key=appSecret)
  454. data = {
  455. 'appId': appId,
  456. 'messageJson': messageJson,
  457. 'sign': sign,
  458. 'pushIds': token_val,
  459. }
  460. # 进行推送
  461. response = requests.post(url, data=data)
  462. LOGGER.info("魅族推送结果:{}".format(response.json()))
  463. return True
  464. except Exception as e:
  465. LOGGER.info("魅族推送异常:{}".format(repr(e)))
  466. return False
  467. @staticmethod
  468. def android_honorpush(token_val, n_time, event_type, msg_title, msg_text,
  469. uid='', channel='1', image='', app_bundle_id='', appBundleId='', channel_id='', nickname=''):
  470. """
  471. android honor 推送
  472. @param channel_id: 通知通道id
  473. @param nickname: 设备昵称
  474. @param app_bundle_id: app包id
  475. @param appBundleId: app包id
  476. @param token_val: 推送token
  477. @param n_time: 当前时间
  478. @param event_type: 事件类型
  479. @param msg_title: 推送标题
  480. @param msg_text: 推送内容
  481. @param uid: uid
  482. @param channel: 通道
  483. @param image: 推送图片链接
  484. @return: bool
  485. """
  486. app_bundle_id = appBundleId if appBundleId else app_bundle_id
  487. try:
  488. client_id = HONORPUSH_CONFIG[app_bundle_id]['client_id']
  489. client_secret = HONORPUSH_CONFIG[app_bundle_id]['client_secret']
  490. app_id = HONORPUSH_CONFIG[app_bundle_id]['app_id']
  491. get_access_token_url = 'https://iam.developer.hihonor.com/auth/token'
  492. post_data = {
  493. 'grant_type': 'client_credentials',
  494. 'client_id': client_id,
  495. 'client_secret': client_secret
  496. }
  497. headers = {'Content-Type': 'application/x-www-form-urlencoded'}
  498. access_token_response = requests.post(get_access_token_url, data=post_data, headers=headers)
  499. access_result = access_token_response.json()
  500. authorization_token = 'Bearer ' + access_result['access_token']
  501. # 发送推送
  502. push_url = 'https://push-api.cloud.hihonor.com/api/v1/{}/sendMessage'.format(app_id)
  503. headers = {'Content-Type': 'application/json', 'Authorization': authorization_token,
  504. 'timestamp': str(int(time.time()) * 1000)}
  505. extra_data = {'alert': 'Motion', 'msg': '', 'sound': 'sound.aif', 'zpush': '1',
  506. 'received_at': n_time, 'event_time': n_time, 'event_type': str(event_type), 'nickname': nickname,
  507. 'uid': uid, 'channel': channel, 'title': msg_title, 'body': msg_text
  508. }
  509. # 通知推送
  510. push_data = {
  511. "android": {
  512. "notification": {
  513. "body": msg_text,
  514. "title": msg_title,
  515. "clickAction": {
  516. "type": 3
  517. }
  518. },
  519. "targetUserType": 0,
  520. "data": json.dumps(extra_data)
  521. },
  522. "token": [token_val]
  523. }
  524. response = requests.post(push_url, json=push_data, headers=headers)
  525. LOGGER.info("uid:{},时间:{},荣耀推送通知返回值:{}".format(uid, n_time, response.json()))
  526. # 一键通话透传推送
  527. if int(event_type) in [606, 607]:
  528. push_data = {
  529. "data": json.dumps(extra_data),
  530. "token": [token_val]
  531. }
  532. response = requests.post(push_url, json=push_data, headers=headers)
  533. LOGGER.info("uid:{},时间:{},荣耀透传推送返回值:{}".format(uid, n_time, response.json()))
  534. return True
  535. except Exception as e:
  536. LOGGER.info("荣耀推送异常:error_line:{},error_msg:{}".format(e.__traceback__.tb_lineno, repr(e)))
  537. return False
  538. @staticmethod
  539. def jpush_transparent_transmission(msg_title, msg_text, app_bundle_id, token_val, extra_data):
  540. """
  541. android 极光透传
  542. @param msg_title: 推送标题
  543. @param msg_text: 推送内容
  544. @param token_val: 推送token
  545. @param app_bundle_id: app包id
  546. @param extra_data: 额外数据
  547. @return: None
  548. """
  549. try:
  550. app_key = JPUSH_CONFIG[app_bundle_id]['Key']
  551. master_secret = JPUSH_CONFIG[app_bundle_id]['Secret']
  552. # 换成各自的app_key和master_secret
  553. _jpush = jpush.JPush(app_key, master_secret)
  554. push = _jpush.create_push()
  555. push.audience = jpush.registration_id(token_val)
  556. push.message = jpush.message(msg_content=msg_text, title=msg_title, extras=extra_data)
  557. push.platform = jpush.all_
  558. res = push.send()
  559. assert res.status_code == 200
  560. except Exception as e:
  561. return repr(e)