HuaweiPushService.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import json
  2. from AnsjerPush.config import LOGGER, DATA_PUSH_EVENT_TYPE_LIST
  3. from Object.enums.EventTypeEnum import EventTypeEnumObj
  4. from Service.CommonService import CommonService
  5. from Service.HuaweiPushService import push_admin
  6. from Service.HuaweiPushService.push_admin import messaging
  7. class HuaweiPushObject:
  8. # 华为推送服务类
  9. def __init__(self):
  10. self.app_id = '101064781'
  11. self.app_secret = '29d5c5367208e35079f14779597b8f6bcc28ee39091546ed577862231fdd0fdd'
  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 DATA_PUSH_EVENT_TYPE_LIST:
  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. android = messaging.AndroidConfig(
  79. data=data,
  80. collapse_key=-1,
  81. urgency=messaging.AndroidConfig.NORMAL_PRIORITY,
  82. ttl='10000s',
  83. bi_tag='the_sample_bi_tag_for_receipt_service',
  84. notification=android_notification,
  85. category='DEVICE_REMINDER'
  86. )
  87. message = messaging.Message(
  88. notification=notification,
  89. android=android,
  90. token=[token_val]
  91. )
  92. try:
  93. import certifi
  94. response = messaging.send_message(message, verify_peer=certifi.where())
  95. LOGGER.info('{}华为通知推送响应: {}'.format(uid, json.dumps(vars(response))))
  96. assert (response.code == '80000000')
  97. return True
  98. except Exception as e:
  99. LOGGER.error('uid:{}, 华为通知推送异常: {}'.format(uid ,repr(e)))
  100. return False
  101. @staticmethod
  102. def send_data_message(uid, event_type, n_time, token_val, channel):
  103. """
  104. 发送透传推送
  105. @param uid:
  106. @param event_type:
  107. @param n_time:
  108. @param token_val:
  109. @param channel:
  110. @return: None
  111. """
  112. LOGGER.info('{}进入发送透传推送函数'.format(uid))
  113. data = {
  114. 'uid': uid, 'event_type': event_type, 'event_time': n_time, 'channel': channel
  115. }
  116. data = json.dumps(data)
  117. android = messaging.AndroidConfig(
  118. collapse_key=-1,
  119. urgency=messaging.AndroidConfig.HIGH_PRIORITY,
  120. ttl='10000s',
  121. bi_tag='the_sample_bi_tag_for_receipt_service'
  122. )
  123. message = messaging.Message(
  124. data=data,
  125. android=android,
  126. token=[token_val]
  127. )
  128. try:
  129. import certifi
  130. response = messaging.send_message(message, verify_peer=certifi.where())
  131. LOGGER.info('{}华为透传推送响应: {}'.format(uid, json.dumps(vars(response))))
  132. assert (response.code == '80000000')
  133. except Exception as e:
  134. LOGGER.info('华为透传推送异常: {}'.format(repr(e)))
  135. @staticmethod
  136. def android_notification(msg_title, msg_text, intent):
  137. return messaging.AndroidNotification(
  138. icon='/raw/ic_launcher2',
  139. color='#AACCDD',
  140. sound='/raw/shake',
  141. default_sound=True,
  142. click_action=messaging.AndroidClickAction(
  143. action_type=1,
  144. intent=intent
  145. ),
  146. body_loc_key='M.String.body',
  147. body_loc_args=('boy', 'dog'),
  148. title_loc_key='M.String.title',
  149. title_loc_args=['Girl', 'Cat'],
  150. channel_id='1',
  151. notify_summary='',
  152. multi_lang_key={'title_key': {'en': 'value1'}, 'body_key': {'en': 'value2'}},
  153. style=1,
  154. big_title=msg_title,
  155. big_body=msg_text,
  156. auto_clear=86400000,
  157. importance=messaging.AndroidNotification.PRIORITY_HIGH,
  158. light_settings=messaging.AndroidLightSettings(color=messaging.AndroidLightSettingsColor(
  159. alpha=0, red=0, green=1, blue=1), light_on_duration='3.5', light_off_duration='5S'),
  160. badge=messaging.AndroidBadgeNotification(
  161. add_num=1, clazz='Classic'),
  162. visibility=messaging.AndroidNotification.PUBLIC,
  163. foreground_show=False
  164. )