HuaweiPushService.py 6.6 KB

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