HuaweiPushService.py 4.3 KB

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