HuaweiPushService.py 3.6 KB

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