VseesHuaweiPushObject.py 8.4 KB

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