HuaweiPushService.py 6.0 KB

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