PushService.py 29 KB

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