HuaweiPushService.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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, msg_title, msg_text, image_url, token_val):
  15. """
  16. 发送推送消息
  17. :param: token
  18. :return:
  19. """
  20. logger = logging.getLogger('info')
  21. logger.info('华为推送参数:{}, {}, {}, {}'.format(msg_title, msg_text, image_url, token_val))
  22. notification = messaging.Notification(
  23. title=msg_title,
  24. body=msg_text,
  25. image=image_url
  26. )
  27. # 推送通知内容配置
  28. android_notification = self.android_notification(msg_title, msg_text)
  29. # 安卓配置
  30. android = messaging.AndroidConfig(
  31. collapse_key=-1,
  32. urgency=messaging.AndroidConfig.NORMAL_PRIORITY,
  33. ttl='10000s',
  34. bi_tag='the_sample_bi_tag_for_receipt_service',
  35. notification=android_notification,
  36. category='DEVICE_REMINDER'
  37. )
  38. message = messaging.Message(
  39. notification=notification,
  40. android=android,
  41. token=[token_val]
  42. )
  43. try:
  44. # Case 1: Local CA sample code
  45. # response = messaging.send_message(message, verify_peer='../Push-CA-Root.pem')
  46. # Case 2: No verification of HTTPS's certificate
  47. # response = messaging.send_message(message)
  48. # Case 3: use certifi Library
  49. import certifi
  50. response = messaging.send_message(message, verify_peer=certifi.where())
  51. logger.info('华为推送响应: {}'.format(json.dumps(vars(response))))
  52. assert (response.code == '80000000')
  53. except Exception as e:
  54. logger.info('华为推送异常: {}'.format(repr(e)))
  55. @staticmethod
  56. def android_notification(msg_title, msg_text):
  57. return messaging.AndroidNotification(
  58. icon='/raw/ic_launcher2',
  59. color='#AACCDD',
  60. sound='/raw/shake',
  61. default_sound=True,
  62. tag='tagBoom',
  63. click_action=messaging.AndroidClickAction(
  64. action_type=1,
  65. intent='intent://com.huawei.codelabpush/deeplink?#Intent;scheme=pushscheme;launchFlags=0x4000000;i.age=180;S.name=abc;end'),
  66. body_loc_key='M.String.body',
  67. body_loc_args=('boy', 'dog'),
  68. title_loc_key='M.String.title',
  69. title_loc_args=['Girl', 'Cat'],
  70. channel_id='1',
  71. notify_summary='',
  72. multi_lang_key={'title_key': {'en': 'value1'}, 'body_key': {'en': 'value2'}},
  73. style=0,
  74. big_title=msg_title,
  75. big_body=msg_text,
  76. auto_clear=86400000,
  77. notify_id=4861,
  78. group='Group1',
  79. importance=messaging.AndroidNotification.PRIORITY_HIGH,
  80. light_settings=messaging.AndroidLightSettings(color=messaging.AndroidLightSettingsColor(
  81. alpha=0, red=0, green=1, blue=1), light_on_duration='3.5', light_off_duration='5S'),
  82. badge=messaging.AndroidBadgeNotification(
  83. add_num=1, clazz='Classic'),
  84. visibility=messaging.AndroidNotification.PUBLIC,
  85. foreground_show=True
  86. )