HuaweiPushService.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. '华为推送参数: '
  33. 'uid:{}, token_val:{}, msg_title:{}, msg_text:{}, image_url:{}, event_type:{}, n_time:{}, channel:{}'.
  34. format(uid, token_val, msg_title, msg_text, image_url, event_type, n_time, channel))
  35. send_succeed = self.send_notify_message(msg_title, msg_text, image_url, uid, nickname,
  36. event_type, n_time, token_val, channel)
  37. if int(event_type) in [606, 607]:
  38. self.send_data_message(uid, event_type, n_time, token_val, channel)
  39. return send_succeed
  40. def send_notify_message(
  41. self, msg_title, msg_text, image_url, uid, nickname, event_type, n_time, token_val, channel):
  42. """
  43. 发送通知推送
  44. @param msg_title:
  45. @param msg_text:
  46. @param image_url:
  47. @param uid:
  48. @param nickname:
  49. @param event_type:
  50. @param n_time:
  51. @param token_val:
  52. @param channel:
  53. @return: bool
  54. """
  55. LOGGER.info('{}进入发送通知推送函数'.format(uid))
  56. msg_title = '设备昵称: {}'.format(msg_title)
  57. notification = messaging.Notification(
  58. title=msg_title,
  59. body=msg_text,
  60. image=image_url
  61. )
  62. # 自定义键值对
  63. data = {
  64. 'alert': 'Motion', 'msg': '', 'sound': 'sound.aif', 'zpush': '1', 'uid': uid, 'nickname': nickname,
  65. 'event_type': event_type, 'received_at': n_time, 'event_time': n_time, 'channel': channel
  66. }
  67. data = json.dumps(data)
  68. # 推送通知内容配置
  69. intent = 'intent://com.vivo.pushvideo/detail?#Intent;scheme=vpushscheme;launchFlags=0x10000000;S.uid={};S.event_type={};S.event_time={};end'.format(
  70. uid, event_type, n_time)
  71. android_notification = self.android_notification(msg_title, msg_text, intent)
  72. # 安卓配置
  73. android = messaging.AndroidConfig(
  74. data=data,
  75. collapse_key=-1,
  76. urgency=messaging.AndroidConfig.NORMAL_PRIORITY,
  77. ttl='10000s',
  78. bi_tag='the_sample_bi_tag_for_receipt_service',
  79. notification=android_notification,
  80. category='DEVICE_REMINDER'
  81. )
  82. message = messaging.Message(
  83. notification=notification,
  84. android=android,
  85. token=[token_val]
  86. )
  87. try:
  88. import certifi
  89. response = messaging.send_message(message, verify_peer=certifi.where())
  90. LOGGER.info('{}华为通知推送响应: {}'.format(uid, json.dumps(vars(response))))
  91. assert (response.code == '80000000')
  92. return True
  93. except Exception as e:
  94. LOGGER.info('华为通知推送异常: {}'.format(repr(e)))
  95. return False
  96. @staticmethod
  97. def send_data_message(uid, event_type, n_time, token_val, channel):
  98. """
  99. 发送透传推送
  100. @param uid:
  101. @param event_type:
  102. @param n_time:
  103. @param token_val:
  104. @param channel:
  105. @return: None
  106. """
  107. LOGGER.info('{}进入发送透传推送函数'.format(uid))
  108. data = {
  109. 'uid': uid, 'event_type': event_type, 'event_time': n_time, 'channel': channel
  110. }
  111. data = json.dumps(data)
  112. android = messaging.AndroidConfig(
  113. collapse_key=-1,
  114. urgency=messaging.AndroidConfig.HIGH_PRIORITY,
  115. ttl='10000s',
  116. bi_tag='the_sample_bi_tag_for_receipt_service'
  117. )
  118. message = messaging.Message(
  119. data=data,
  120. android=android,
  121. token=[token_val]
  122. )
  123. try:
  124. import certifi
  125. response = messaging.send_message(message, verify_peer=certifi.where())
  126. LOGGER.info('{}华为透传推送响应: {}'.format(uid, json.dumps(vars(response))))
  127. assert (response.code == '80000000')
  128. except Exception as e:
  129. LOGGER.info('华为透传推送异常: {}'.format(repr(e)))
  130. @staticmethod
  131. def android_notification(msg_title, msg_text, intent):
  132. return messaging.AndroidNotification(
  133. icon='/raw/ic_launcher2',
  134. color='#AACCDD',
  135. sound='/raw/shake',
  136. default_sound=True,
  137. click_action=messaging.AndroidClickAction(
  138. action_type=1,
  139. intent=intent
  140. ),
  141. body_loc_key='M.String.body',
  142. body_loc_args=('boy', 'dog'),
  143. title_loc_key='M.String.title',
  144. title_loc_args=['Girl', 'Cat'],
  145. channel_id='1',
  146. notify_summary='',
  147. multi_lang_key={'title_key': {'en': 'value1'}, 'body_key': {'en': 'value2'}},
  148. style=1,
  149. big_title=msg_title,
  150. big_body=msg_text,
  151. auto_clear=86400000,
  152. importance=messaging.AndroidNotification.PRIORITY_HIGH,
  153. light_settings=messaging.AndroidLightSettings(color=messaging.AndroidLightSettingsColor(
  154. alpha=0, red=0, green=1, blue=1), light_on_duration='3.5', light_off_duration='5S'),
  155. badge=messaging.AndroidBadgeNotification(
  156. add_num=1, clazz='Classic'),
  157. visibility=messaging.AndroidNotification.PUBLIC,
  158. foreground_show=False
  159. )