HuaweiPushService.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import json
  2. from AnsjerPush.config import LOGGER
  3. from Service.HuaweiPushService import push_admin
  4. from Service.HuaweiPushService.push_admin import messaging
  5. class HuaweiPushObject:
  6. # 华为推送服务类
  7. def __init__(self):
  8. self.app_id = '101064781'
  9. self.app_secret = '29d5c5367208e35079f14779597b8f6bcc28ee39091546ed577862231fdd0fdd'
  10. self.init_app()
  11. def init_app(self):
  12. """init sdk app"""
  13. push_admin.initialize_app(self.app_id, self.app_secret)
  14. def send_push_notify_message(self, token_val, msg_title, msg_text, image_url=None, uid='', nickname='', n_time='',
  15. event_type='0', channel='', app_bundle_id='', appBundleId=''):
  16. """
  17. 发送推送消息
  18. @param token_val: 手机推送token
  19. @param msg_title: 标题
  20. @param msg_text: 内容
  21. @param image_url: 图片链接
  22. @param uid: uid
  23. @param nickname: 设备昵称
  24. @param n_time: 当前时间
  25. @param event_type: 事件类型
  26. @param channel: 通道
  27. @param app_bundle_id: APP包id
  28. @param appBundleId: APP包id
  29. @return: bool
  30. """
  31. LOGGER.info(
  32. '华为推送参数: uid:{}, token_val:{}, msg_title:{}, msg_text:{}, image_url:{}, event_type:{}, n_time:{}'.format(
  33. uid, token_val, msg_title, msg_text, image_url, event_type, n_time))
  34. send_succeed = self.send_notify_message(msg_title, msg_text, image_url, uid, nickname,
  35. event_type, n_time, token_val)
  36. if int(event_type) in [606, 607]:
  37. self.send_data_message(uid, event_type, n_time, token_val)
  38. return send_succeed
  39. def send_notify_message(self, msg_title, msg_text, image_url, uid, nickname, event_type, n_time, token_val):
  40. """
  41. 发送通知推送
  42. @param msg_title:
  43. @param msg_text:
  44. @param image_url:
  45. @param uid:
  46. @param nickname:
  47. @param event_type:
  48. @param n_time:
  49. @param token_val:
  50. @return: bool
  51. """
  52. LOGGER.info('{}进入发送通知推送函数'.format(uid))
  53. msg_title = '设备昵称: {}'.format(msg_title)
  54. notification = messaging.Notification(
  55. title=msg_title,
  56. body=msg_text,
  57. image=image_url
  58. )
  59. # 自定义键值对
  60. data = {'alert': 'Motion', 'msg': '', 'sound': 'sound.aif', 'zpush': '1', 'uid': uid,
  61. 'nickname': nickname, 'event_type': event_type, 'received_at': n_time, 'event_time': n_time
  62. }
  63. # 一键通话和视频通话使用自定义铃声
  64. if int(event_type) in [606, 607]:
  65. category = 'VOIP'
  66. else:
  67. category = 'DEVICE_REMINDER'
  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(event_type, msg_title, msg_text, intent)
  73. # 安卓配置
  74. android = messaging.AndroidConfig(
  75. data=data,
  76. collapse_key=-1,
  77. urgency=messaging.AndroidConfig.NORMAL_PRIORITY,
  78. ttl='10000s',
  79. bi_tag='the_sample_bi_tag_for_receipt_service',
  80. notification=android_notification,
  81. category=category
  82. )
  83. message = messaging.Message(
  84. notification=notification,
  85. android=android,
  86. token=[token_val]
  87. )
  88. try:
  89. import certifi
  90. response = messaging.send_message(message, verify_peer=certifi.where())
  91. LOGGER.info('{}华为通知推送响应: {}'.format(uid, json.dumps(vars(response))))
  92. assert (response.code == '80000000')
  93. return True
  94. except Exception as e:
  95. LOGGER.info('华为通知推送异常: {}'.format(repr(e)))
  96. return False
  97. @staticmethod
  98. def send_data_message(uid, event_type, n_time, token_val):
  99. """
  100. 发送透传推送
  101. @param uid:
  102. @param event_type:
  103. @param n_time:
  104. @param token_val:
  105. @return: None
  106. """
  107. LOGGER.info('{}进入发送透传推送函数'.format(uid))
  108. data = {'uid': uid, 'event_type': event_type, 'event_time': n_time}
  109. data = json.dumps(data)
  110. android = messaging.AndroidConfig(
  111. collapse_key=-1,
  112. urgency=messaging.AndroidConfig.HIGH_PRIORITY,
  113. ttl='10000s',
  114. bi_tag='the_sample_bi_tag_for_receipt_service'
  115. )
  116. message = messaging.Message(
  117. data=data,
  118. android=android,
  119. token=[token_val]
  120. )
  121. try:
  122. import certifi
  123. response = messaging.send_message(message, verify_peer=certifi.where())
  124. LOGGER.info('{}华为透传推送响应: {}'.format(uid, json.dumps(vars(response))))
  125. assert (response.code == '80000000')
  126. except Exception as e:
  127. LOGGER.info('华为透传推送异常: {}'.format(repr(e)))
  128. @staticmethod
  129. def android_notification(event_type, msg_title, msg_text, intent):
  130. # 一键通话和视频通话使用自定义铃声
  131. if int(event_type) in [606, 607]:
  132. sound = '/raw/phone_call'
  133. default_sound = False
  134. channel_id = '111934'
  135. else:
  136. sound = '/raw/shake'
  137. default_sound = True
  138. channel_id = '1'
  139. return messaging.AndroidNotification(
  140. icon='/raw/ic_launcher2',
  141. color='#AACCDD',
  142. sound=sound,
  143. default_sound=default_sound,
  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=channel_id,
  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. )