VseesHuaweiPushObject.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import json
  2. from AnsjerPush.config import LOGGER
  3. from Object.enums.EventTypeEnum import EventTypeEnumObj
  4. from Service.VSeesHuaweiPushService import push_admin
  5. from Service.VSeesHuaweiPushService.push_admin import messaging
  6. class VseesHuaweiPushObject:
  7. # 华为推送服务类
  8. def __init__(self):
  9. self.app_id = '108703647'
  10. self.app_secret = '6bc5b0b0ab4588fcd667b3e6c1ab4fd5d857de21ad69ee9d46e077b9f5f24e8f'
  11. self.init_app()
  12. def init_app(self):
  13. """init sdk app"""
  14. push_admin.initialize_app(self.app_id, self.app_secret)
  15. def send_push_notify_message(self, token_val, msg_title, msg_text, image_url=None, uid='', nickname='', n_time='',
  16. event_type='0', channel='', app_bundle_id='', appBundleId=''):
  17. """
  18. 发送推送消息
  19. @param token_val: 手机推送token
  20. @param msg_title: 标题
  21. @param msg_text: 内容
  22. @param image_url: 图片链接
  23. @param uid: uid
  24. @param nickname: 设备昵称
  25. @param n_time: 当前时间
  26. @param event_type: 事件类型
  27. @param channel: 通道
  28. @param app_bundle_id: APP包id
  29. @param appBundleId: APP包id
  30. @return: bool
  31. """
  32. LOGGER.info(
  33. '华为推送参数(微瞳): '
  34. 'uid:{}, token_val:{}, msg_title:{}, msg_text:{}, image_url:{}, event_type:{}, n_time:{}, channel:{}'.
  35. format(uid, token_val, msg_title, msg_text, image_url, event_type, n_time, channel))
  36. send_succeed = self.send_notify_message(msg_title, msg_text, image_url, uid, nickname,
  37. event_type, n_time, token_val, channel)
  38. if int(event_type) in EventTypeEnumObj.DATA_PUSH_EVENT_TYPE_LIST.value:
  39. self.send_data_message(uid, event_type, n_time, token_val, channel)
  40. return send_succeed
  41. def send_notify_message(
  42. self, msg_title, msg_text, image_url, uid, nickname, event_type, n_time, token_val, channel):
  43. """
  44. 发送通知推送
  45. @param msg_title:
  46. @param msg_text:
  47. @param image_url:
  48. @param uid:
  49. @param nickname:
  50. @param event_type:
  51. @param n_time:
  52. @param token_val:
  53. @param channel:
  54. @return: bool
  55. """
  56. LOGGER.info('{}进入发送通知推送函数(微瞳)'.format(uid))
  57. msg_title = '设备昵称: {}'.format(msg_title)
  58. notification = messaging.Notification(
  59. title=msg_title,
  60. body=msg_text,
  61. image=image_url
  62. )
  63. # 自定义键值对
  64. data = {
  65. 'alert': msg_text, 'msg': '', 'sound': 'sound.aif', 'zpush': '1', 'uid': uid, 'nickname': nickname,
  66. 'event_type': event_type, 'received_at': n_time, 'event_time': n_time, 'channel': channel
  67. }
  68. data = json.dumps(data)
  69. # 推送通知内容配置
  70. intent = 'intent://com.vivo.pushvideo/detail?#Intent;scheme=vpushscheme;launchFlags=0x10000000;S.uid={};S.event_type={};S.event_time={};end'.format(
  71. uid, event_type, n_time)
  72. android_notification = self.android_notification(msg_title, msg_text, intent)
  73. # 微瞳消息类型暂时改为音视频通话
  74. category = 'VOIP' if self.app_id == '108703647' else 'DEVICE_REMINDER'
  75. # 安卓配置
  76. android = messaging.AndroidConfig(
  77. data=data,
  78. collapse_key=-1,
  79. urgency=messaging.AndroidConfig.NORMAL_PRIORITY,
  80. ttl='10000s',
  81. bi_tag='the_sample_bi_tag_for_receipt_service',
  82. notification=android_notification,
  83. category=category
  84. )
  85. message = messaging.Message(
  86. notification=notification,
  87. android=android,
  88. token=[token_val]
  89. )
  90. try:
  91. import certifi
  92. response = messaging.send_message(message, verify_peer=certifi.where())
  93. LOGGER.info('{}华为通知推送响应(微瞳): {}'.format(uid, json.dumps(vars(response))))
  94. assert (response.code == '80000000')
  95. return True
  96. except Exception as e:
  97. LOGGER.info('华为通知推送异常(微瞳): {}'.format(repr(e)))
  98. return False
  99. @staticmethod
  100. def send_data_message(uid, event_type, n_time, token_val, channel):
  101. """
  102. 发送透传推送
  103. @param uid:
  104. @param event_type:
  105. @param n_time:
  106. @param token_val:
  107. @param channel:
  108. @return: None
  109. """
  110. LOGGER.info('{}进入发送透传推送函数(微瞳)'.format(uid))
  111. data = {
  112. 'uid': uid, 'event_type': event_type, 'event_time': n_time, 'channel': channel
  113. }
  114. data = json.dumps(data)
  115. android = messaging.AndroidConfig(
  116. collapse_key=-1,
  117. urgency=messaging.AndroidConfig.HIGH_PRIORITY,
  118. ttl='10000s',
  119. bi_tag='the_sample_bi_tag_for_receipt_service'
  120. )
  121. message = messaging.Message(
  122. data=data,
  123. android=android,
  124. token=[token_val]
  125. )
  126. try:
  127. import certifi
  128. response = messaging.send_message(message, verify_peer=certifi.where())
  129. LOGGER.info('{}华为透传推送响应(微瞳): {}'.format(uid, json.dumps(vars(response))))
  130. assert (response.code == '80000000')
  131. except Exception as e:
  132. LOGGER.info('华为透传推送异常(微瞳): {}'.format(repr(e)))
  133. @staticmethod
  134. def android_notification(msg_title, msg_text, intent):
  135. return messaging.AndroidNotification(
  136. icon='/raw/ic_launcher2',
  137. color='#AACCDD',
  138. sound='/raw/shake',
  139. default_sound=True,
  140. click_action=messaging.AndroidClickAction(
  141. action_type=1,
  142. intent=intent
  143. ),
  144. body_loc_key='M.String.body',
  145. body_loc_args=('boy', 'dog'),
  146. title_loc_key='M.String.title',
  147. title_loc_args=['Girl', 'Cat'],
  148. channel_id='1',
  149. notify_summary='',
  150. multi_lang_key={'title_key': {'en': 'value1'}, 'body_key': {'en': 'value2'}},
  151. style=1,
  152. big_title=msg_title,
  153. big_body=msg_text,
  154. auto_clear=86400000,
  155. importance=messaging.AndroidNotification.PRIORITY_HIGH,
  156. light_settings=messaging.AndroidLightSettings(color=messaging.AndroidLightSettingsColor(
  157. alpha=0, red=0, green=1, blue=1), light_on_duration='3.5', light_off_duration='5S'),
  158. badge=messaging.AndroidBadgeNotification(
  159. add_num=1, clazz='Classic'),
  160. visibility=messaging.AndroidNotification.PUBLIC,
  161. foreground_show=False,
  162. # buttons=[
  163. # {'name': '接听', 'action_type': 1},
  164. # {'name': '拒绝', 'action_type': 3}
  165. # ]
  166. )
  167. @staticmethod
  168. def huawei_transparent_transmission(nickname, app_bundle_id, token_val, n_time, event_type, msg_title,
  169. msg_text, user_id):
  170. """
  171. 发送透传推送
  172. @param nickname:
  173. @param app_bundle_id:
  174. @param event_type:
  175. @param n_time:
  176. @param token_val:
  177. @param msg_title:
  178. @param msg_text:
  179. @param user_id:
  180. @return: None
  181. """
  182. data = {
  183. 'nickname': nickname, 'event_type': event_type, 'event_time': n_time, 'msg_title': msg_title,
  184. 'msg_text': msg_text
  185. }
  186. data = json.dumps(data)
  187. android = messaging.AndroidConfig(
  188. collapse_key=-1,
  189. urgency=messaging.AndroidConfig.HIGH_PRIORITY,
  190. ttl='10000s',
  191. bi_tag='the_sample_bi_tag_for_receipt_service'
  192. )
  193. message = messaging.Message(
  194. data=data,
  195. android=android,
  196. token=[token_val]
  197. )
  198. try:
  199. import certifi
  200. response = messaging.send_message(message, verify_peer=certifi.where())
  201. LOGGER.info('{}退出登录,华为透传推送响应(微瞳): {}'.format(user_id, json.dumps(vars(response))))
  202. assert (response.code == '80000000')
  203. except Exception as e:
  204. LOGGER.info('{}退出登录,华为透传推送异常(微瞳): {}'.format(user_id, repr(e)))