HuaweiPushService.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import json
  2. from AnsjerPush.config import LOGGER
  3. from Object.enums.EventTypeEnum import EventTypeEnumObj
  4. from Service.HuaweiPushService import push_admin
  5. from Service.HuaweiPushService.push_admin import messaging
  6. class HuaweiPushObject:
  7. # 华为推送服务类
  8. def __init__(self):
  9. self.app_id = '101064781'
  10. self.app_secret = '29d5c5367208e35079f14779597b8f6bcc28ee39091546ed577862231fdd0fdd'
  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. 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='DEVICE_REMINDER'
  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.error('uid:{}, 华为通知推送异常: {}'.format(uid ,repr(e)))
  96. return False
  97. @staticmethod
  98. def send_data_message(uid, event_type, n_time, token_val, channel):
  99. """
  100. 发送透传推送
  101. @param uid:
  102. @param event_type:
  103. @param n_time:
  104. @param token_val:
  105. @param channel:
  106. @return: None
  107. """
  108. LOGGER.info('{}进入发送透传推送函数'.format(uid))
  109. data = {
  110. 'uid': uid, 'event_type': event_type, 'event_time': n_time, 'channel': channel
  111. }
  112. data = json.dumps(data)
  113. android = messaging.AndroidConfig(
  114. collapse_key=-1,
  115. urgency=messaging.AndroidConfig.HIGH_PRIORITY,
  116. ttl='10000s',
  117. bi_tag='the_sample_bi_tag_for_receipt_service'
  118. )
  119. message = messaging.Message(
  120. data=data,
  121. android=android,
  122. token=[token_val]
  123. )
  124. try:
  125. import certifi
  126. response = messaging.send_message(message, verify_peer=certifi.where())
  127. LOGGER.info('{}华为透传推送响应: {}'.format(uid, json.dumps(vars(response))))
  128. assert (response.code == '80000000')
  129. except Exception as e:
  130. LOGGER.info('华为透传推送异常: {}'.format(repr(e)))
  131. @staticmethod
  132. def android_notification(msg_title, msg_text, intent):
  133. return messaging.AndroidNotification(
  134. icon='/raw/ic_launcher2',
  135. color='#AACCDD',
  136. sound='/raw/shake',
  137. default_sound=True,
  138. click_action=messaging.AndroidClickAction(
  139. action_type=1,
  140. intent=intent
  141. ),
  142. body_loc_key='M.String.body',
  143. body_loc_args=('boy', 'dog'),
  144. title_loc_key='M.String.title',
  145. title_loc_args=['Girl', 'Cat'],
  146. channel_id='1',
  147. notify_summary='',
  148. multi_lang_key={'title_key': {'en': 'value1'}, 'body_key': {'en': 'value2'}},
  149. style=1,
  150. big_title=msg_title,
  151. big_body=msg_text,
  152. auto_clear=86400000,
  153. importance=messaging.AndroidNotification.PRIORITY_HIGH,
  154. light_settings=messaging.AndroidLightSettings(color=messaging.AndroidLightSettingsColor(
  155. alpha=0, red=0, green=1, blue=1), light_on_duration='3.5', light_off_duration='5S'),
  156. badge=messaging.AndroidBadgeNotification(
  157. add_num=1, clazz='Classic'),
  158. visibility=messaging.AndroidNotification.PUBLIC,
  159. foreground_show=False,
  160. # buttons=[
  161. # {'name': '接听', 'action_type': 1},
  162. # {'name': '拒绝', 'action_type': 3}
  163. # ]
  164. )
  165. @staticmethod
  166. def huawei_transparent_transmission(nickname, app_bundle_id, token_val, n_time, event_type, msg_title,
  167. msg_text, user_id):
  168. """
  169. 发送透传推送
  170. @param nickname:
  171. @param app_bundle_id:
  172. @param event_type:
  173. @param n_time:
  174. @param token_val:
  175. @param msg_title:
  176. @param msg_text:
  177. @param user_id:
  178. @return: None
  179. """
  180. data = {
  181. 'nickname': nickname, 'event_type': event_type, 'event_time': n_time, 'msg_title': msg_title,
  182. 'msg_text': msg_text
  183. }
  184. data = json.dumps(data)
  185. android = messaging.AndroidConfig(
  186. collapse_key=-1,
  187. urgency=messaging.AndroidConfig.HIGH_PRIORITY,
  188. ttl='10000s',
  189. bi_tag='the_sample_bi_tag_for_receipt_service'
  190. )
  191. message = messaging.Message(
  192. data=data,
  193. android=android,
  194. token=[token_val]
  195. )
  196. try:
  197. import certifi
  198. response = messaging.send_message(message, verify_peer=certifi.where())
  199. LOGGER.info('{}退出登录,华为透传推送响应: {}'.format(user_id, json.dumps(vars(response))))
  200. assert (response.code == '80000000')
  201. except Exception as e:
  202. LOGGER.info('{}退出登录,华为透传推送异常: {}'.format(user_id, repr(e)))