HuaweiPushService.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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='', 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:
  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. self.send_notify_message(msg_title, msg_text, image_url, uid, nickname, event_type, n_time, token_val)
  35. if int(event_type) in [606, 607]:
  36. self.send_data_message(uid, event_type, n_time, token_val)
  37. def send_notify_message(self, msg_title, msg_text, image_url, uid, nickname, event_type, n_time, token_val):
  38. """
  39. 发送通知推送
  40. @param msg_title:
  41. @param msg_text:
  42. @param image_url:
  43. @param uid:
  44. @param nickname:
  45. @param event_type:
  46. @param n_time:
  47. @param token_val:
  48. @return: None
  49. """
  50. LOGGER.info('{}进入发送通知推送函数'.format(uid))
  51. msg_title = '设备昵称: {}'.format(msg_title)
  52. notification = messaging.Notification(
  53. title=msg_title,
  54. body=msg_text,
  55. image=image_url
  56. )
  57. # 自定义键值对
  58. data = {'alert': 'Motion', 'msg': '', 'sound': 'sound.aif', 'zpush': '1', 'uid': uid,
  59. 'nickname': nickname, 'event_type': event_type, 'received_at': n_time, 'event_time': n_time
  60. }
  61. # 一键通话和视频通话使用自定义铃声
  62. if int(event_type) in [606, 607]:
  63. category = 'VOIP'
  64. else:
  65. category = 'DEVICE_REMINDER'
  66. data = json.dumps(data)
  67. # 推送通知内容配置
  68. intent = 'intent://com.vivo.pushvideo/detail?#Intent;scheme=vpushscheme;launchFlags=0x10000000;S.uid={};S.event_type={};S.event_time={};end'.format(
  69. uid, event_type, n_time)
  70. android_notification = self.android_notification(event_type, msg_title, msg_text, intent)
  71. # 安卓配置
  72. android = messaging.AndroidConfig(
  73. data=data,
  74. collapse_key=-1,
  75. urgency=messaging.AndroidConfig.NORMAL_PRIORITY,
  76. ttl='10000s',
  77. bi_tag='the_sample_bi_tag_for_receipt_service',
  78. notification=android_notification,
  79. category=category
  80. )
  81. message = messaging.Message(
  82. notification=notification,
  83. android=android,
  84. token=[token_val]
  85. )
  86. try:
  87. import certifi
  88. response = messaging.send_message(message, verify_peer=certifi.where())
  89. LOGGER.info('{}华为通知推送响应: {}'.format(uid, json.dumps(vars(response))))
  90. assert (response.code == '80000000')
  91. except Exception as e:
  92. LOGGER.info('华为通知推送异常: {}'.format(repr(e)))
  93. @staticmethod
  94. def send_data_message(uid, event_type, n_time, token_val):
  95. """
  96. 发送透传推送
  97. @param uid:
  98. @param event_type:
  99. @param n_time:
  100. @param token_val:
  101. @return: None
  102. """
  103. LOGGER.info('{}进入发送透传推送函数'.format(uid))
  104. data = {'uid': uid, 'event_type': event_type, 'event_time': n_time}
  105. data = json.dumps(data)
  106. android = messaging.AndroidConfig(
  107. collapse_key=-1,
  108. urgency=messaging.AndroidConfig.HIGH_PRIORITY,
  109. ttl='10000s',
  110. bi_tag='the_sample_bi_tag_for_receipt_service'
  111. )
  112. message = messaging.Message(
  113. data=data,
  114. android=android,
  115. token=[token_val]
  116. )
  117. try:
  118. import certifi
  119. response = messaging.send_message(message, verify_peer=certifi.where())
  120. LOGGER.info('{}华为透传推送响应: {}'.format(uid, json.dumps(vars(response))))
  121. assert (response.code == '80000000')
  122. except Exception as e:
  123. LOGGER.info('华为透传推送异常: {}'.format(repr(e)))
  124. @staticmethod
  125. def android_notification(event_type, msg_title, msg_text, intent):
  126. # 一键通话和视频通话使用自定义铃声
  127. if int(event_type) in [606, 607]:
  128. sound = '/raw/phone_call'
  129. default_sound = False
  130. else:
  131. sound = '/raw/shake'
  132. default_sound = True
  133. return messaging.AndroidNotification(
  134. icon='/raw/ic_launcher2',
  135. color='#AACCDD',
  136. sound=sound,
  137. default_sound=default_sound,
  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. )