HuaweiPushService.py 3.4 KB

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