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