PushService.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 logging
  9. import os
  10. import apns2
  11. import jpush
  12. import requests
  13. from pyfcm import FCMNotification
  14. from AnsjerPush.config import APP_BUNDLE_DICT, APNS_MODE, BASE_DIR, APNS_CONFIG, FCM_CONFIG, JPUSH_CONFIG, XMPUSH_CONFIG
  15. from Service.CommonService import CommonService
  16. class PushObject:
  17. # 推送对象
  18. @staticmethod
  19. def get_msg_title(app_bundle_id, nickname):
  20. """
  21. 获取推送消息标题
  22. @param app_bundle_id: app包id
  23. @param nickname: 设备名
  24. @return: msg_title
  25. """
  26. if app_bundle_id in APP_BUNDLE_DICT.keys():
  27. msg_title = APP_BUNDLE_DICT[app_bundle_id] + '(' + nickname + ')'
  28. else:
  29. msg_title = nickname
  30. return msg_title
  31. @staticmethod
  32. def get_gateway_msg_text(n_time, tz, lang, alarm):
  33. """
  34. 获取网关推送消息内容
  35. @param n_time: 当前时间
  36. @param tz: 时区
  37. @param lang: 语言
  38. @param alarm: 警报
  39. @return: msg_text
  40. """
  41. n_date = CommonService.get_now_time_str(n_time=n_time, tz=tz, lang=lang)
  42. if lang == 'cn':
  43. msg_text = '{} 日期:{}'.format(alarm, n_date)
  44. else:
  45. msg_text = '{} date:{}'.format(alarm, n_date)
  46. return msg_text
  47. @staticmethod
  48. def get_ai_msg_text(channel, n_time, lang, tz, label):
  49. """
  50. 获取AI推送内容
  51. @param channel: 通道
  52. @param n_time: 当前时间
  53. @param lang: 语言
  54. @param tz: 时区
  55. @param label: 识别到的标签
  56. @return: ai_msg_text
  57. """
  58. n_date = CommonService.get_now_time_str(n_time=n_time, tz=tz, lang=lang)
  59. if lang == 'cn':
  60. msg = '摄像头AI识别到了{}'.format(label)
  61. ai_msg_text = '{msg} 通道:{channel} 日期:{date}'.format(msg=msg, channel=channel, date=n_date)
  62. else:
  63. msg = 'Camera AI recognizes {}'.format(label)
  64. ai_msg_text = '{msg} channel:{channel} date:{date}'.format(msg=msg, channel=channel, date=n_date)
  65. return ai_msg_text
  66. @staticmethod
  67. def get_low_power_msg_text(channel, n_time, lang, tz, electricity, is_sys=0):
  68. """
  69. 获取低电量推送内容
  70. @param channel: 通道
  71. @param n_time: 当前时间
  72. @param lang: 语言
  73. @param tz: 时区
  74. @param electricity: 电量
  75. @param is_sys: 是否为系统消息
  76. @return: low_power_msg_text
  77. """
  78. n_date = CommonService.get_now_time_str(n_time=n_time, tz=tz, lang=lang)
  79. if lang == 'cn':
  80. alarm = '剩余电量:' + electricity
  81. if is_sys:
  82. low_power_msg_text = '{} 通道:{}'.format(alarm, channel)
  83. else:
  84. low_power_msg_text = '{} 通道:{} 日期:{}'.format(alarm, channel, n_date)
  85. else:
  86. alarm = 'Battery remaining:' + electricity
  87. if is_sys:
  88. low_power_msg_text = '{} channel:{}'.format(alarm, channel)
  89. else:
  90. low_power_msg_text = '{} channel:{} date:{}'.format(alarm, channel, n_date)
  91. return low_power_msg_text
  92. @staticmethod
  93. def ios_apns_push(nickname, app_bundle_id, token_val, n_time, event_type, msg_title, msg_text,
  94. uid='', channel='1', launch_image=None):
  95. """
  96. ios apns 推送
  97. @param nickname: 设备昵称
  98. @param app_bundle_id: app包id
  99. @param token_val: 推送token
  100. @param n_time: 当前时间
  101. @param event_type: 事件类型
  102. @param msg_title: 推送标题
  103. @param msg_text: 推送内容
  104. @param uid: uid
  105. @param channel: 通道
  106. @param launch_image: 推送图片链接
  107. @return: None
  108. """
  109. logger = logging.getLogger('info')
  110. try:
  111. pem_path = os.path.join(BASE_DIR, APNS_CONFIG[app_bundle_id]['pem_path'])
  112. logger.info('apns推送app_bundle_id:{}, pem_path:{}'.format(app_bundle_id, pem_path))
  113. cli = apns2.APNSClient(mode=APNS_MODE, client_cert=pem_path)
  114. alert = apns2.PayloadAlert(title=msg_title, body=msg_text, launch_image=launch_image)
  115. push_data = {'alert': 'Motion', 'msg': '', 'sound': '', 'zpush': '1', 'uid': uid, 'channel': channel,
  116. 'received_at': n_time, 'event_time': n_time, 'event_type': event_type, 'nickname': nickname,
  117. 'image_url': launch_image
  118. }
  119. payload = apns2.Payload(alert=alert, custom=push_data, sound='default', category='myCategory',
  120. mutable_content=True)
  121. n = apns2.Notification(payload=payload, priority=apns2.PRIORITY_LOW)
  122. res = cli.push(n=n, device_token=token_val, topic=app_bundle_id)
  123. assert res.status_code == 200
  124. except Exception as e:
  125. logger.info('--->IOS推送异常{}'.format(repr(e)))
  126. return repr(e)
  127. @staticmethod
  128. def android_fcm_push(nickname, app_bundle_id, token_val, n_time, event_type, msg_title, msg_text,
  129. uid='', channel='1', image=''):
  130. """
  131. android fcm 推送
  132. @param nickname: 设备昵称
  133. @param app_bundle_id: app包id
  134. @param token_val: 推送token
  135. @param n_time: 当前时间
  136. @param event_type: 事件类型
  137. @param msg_title: 推送标题
  138. @param msg_text: 推送内容
  139. @param uid: uid
  140. @param channel: 通道
  141. @param image: 推送图片链接
  142. @return: None
  143. """
  144. logger = logging.getLogger('info')
  145. try:
  146. serverKey = FCM_CONFIG[app_bundle_id]
  147. push_service = FCMNotification(api_key=serverKey)
  148. push_data = {'alert': 'Motion', 'msg': '', 'sound': 'sound.aif', 'zpush': '1', 'image': image,
  149. 'received_at': n_time, 'event_time': n_time, 'event_type': event_type, 'nickname': nickname,
  150. 'uid': uid, 'channel': channel
  151. }
  152. result = push_service.notify_single_device(registration_id=token_val, message_title=msg_title,
  153. message_body=msg_text, data_message=push_data,
  154. extra_kwargs={'default_sound': True,
  155. 'default_vibrate_timings': True,
  156. 'default_light_settings': True,
  157. }
  158. )
  159. logger.info('fcm推送结果:{}'.format(result))
  160. except Exception as e:
  161. return repr(e)
  162. @staticmethod
  163. def android_jpush(nickname, app_bundle_id, token_val, n_time, event_type, msg_title, msg_text):
  164. """
  165. android 极光 推送
  166. @param nickname: 设备昵称
  167. @param app_bundle_id: app包id
  168. @param token_val: 推送token
  169. @param n_time: 当前时间
  170. @param event_type: 事件类型
  171. @param msg_title: 推送标题
  172. @param msg_text: 推送内容
  173. @return: None
  174. """
  175. try:
  176. app_key = JPUSH_CONFIG[app_bundle_id]['Key']
  177. master_secret = JPUSH_CONFIG[app_bundle_id]['Secret']
  178. # 换成各自的app_key和master_secret
  179. _jpush = jpush.JPush(app_key, master_secret)
  180. push = _jpush.create_push()
  181. push.audience = jpush.registration_id(token_val)
  182. push_data = {'alert': 'Motion', 'msg': '', 'sound': 'sound.aif', 'zpush': '1',
  183. 'received_at': n_time, 'event_time': n_time, 'event_type': event_type, 'nickname': nickname
  184. }
  185. android = jpush.android(title=msg_title, big_text=msg_text, alert=msg_text, extras=push_data,
  186. priority=1, style=1, alert_type=7
  187. )
  188. push.notification = jpush.notification(android=android)
  189. push.platform = jpush.all_
  190. res = push.send()
  191. assert res.status_code == 200
  192. except Exception as e:
  193. return repr(e)
  194. @staticmethod
  195. def android_xmpush(nickname, app_bundle_id, token_val, n_time, event_type, msg_title, msg_text,
  196. uid='', channel='1', image=''):
  197. """
  198. android 小米 推送
  199. @param nickname: 设备昵称
  200. @param app_bundle_id: app包id
  201. @param token_val: 推送token
  202. @param n_time: 当前时间
  203. @param event_type: 事件类型
  204. @param msg_title: 推送标题
  205. @param msg_text: 推送内容
  206. @return: None
  207. """
  208. try:
  209. url = 'https://api.xmpush.xiaomi.com/v3/message/regid'
  210. app_secret = XMPUSH_CONFIG[app_bundle_id]
  211. payload = {'alert': 'Motion', 'msg': '', 'sound': 'sound.aif', 'zpush': '1',
  212. 'received_at': n_time, 'event_time': n_time, 'event_type': event_type, 'nickname': nickname,
  213. 'uid': uid, 'channel': channel
  214. }
  215. data = {
  216. 'title': msg_title,
  217. 'description': msg_text,
  218. 'payload': payload,
  219. 'restricted_package_name': app_bundle_id,
  220. 'registration_id': token_val,
  221. }
  222. if image:
  223. data['extra.notification_style_type'] = 2
  224. data['extra.notification_bigPic_uri'] = image
  225. headers = {
  226. 'Authorization': 'key={}'.format(app_secret)
  227. }
  228. response = requests.post(url, data=data, headers=headers)
  229. assert response.status_code == 200
  230. except Exception as e:
  231. return repr(e)