1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- import json
- import logging
- from Service.HuaweiPushService import push_admin
- from Service.HuaweiPushService.push_admin import messaging
- class HuaweiPushObject:
- # 华为推送服务类
- def __init__(self):
- self.app_id = '101064781'
- self.app_secret = '29d5c5367208e35079f14779597b8f6bcc28ee39091546ed577862231fdd0fdd'
- self.init_app()
- def init_app(self):
- """init sdk app"""
- push_admin.initialize_app(self.app_id, self.app_secret)
- def send_push_notify_message(self, msg_title, msg_text, image_url, token_val):
- """
- 发送推送消息
- :param: token
- :return:
- """
- logger = logging.getLogger('info')
- logger.info('华为推送参数:{}, {}, {}, {}'.format(msg_title, msg_text, image_url, token_val))
- msg_title = '设备昵称: {}'.format(msg_title)
- notification = messaging.Notification(
- title=msg_title,
- body=msg_text,
- image=image_url
- )
- # 推送通知内容配置
- android_notification = self.android_notification(msg_title, msg_text)
- # 安卓配置
- android = messaging.AndroidConfig(
- collapse_key=-1,
- urgency=messaging.AndroidConfig.NORMAL_PRIORITY,
- ttl='10000s',
- bi_tag='the_sample_bi_tag_for_receipt_service',
- notification=android_notification,
- category='DEVICE_REMINDER'
- )
- message = messaging.Message(
- notification=notification,
- android=android,
- token=[token_val]
- )
- try:
- # Case 1: Local CA sample code
- # response = messaging.send_message(message, verify_peer='../Push-CA-Root.pem')
- # Case 2: No verification of HTTPS's certificate
- # response = messaging.send_message(message)
- # Case 3: use certifi Library
- import certifi
- response = messaging.send_message(message, verify_peer=certifi.where())
- logger.info('华为推送响应: {}'.format(json.dumps(vars(response))))
- assert (response.code == '80000000')
- except Exception as e:
- logger.info('华为推送异常: {}'.format(repr(e)))
- @staticmethod
- def android_notification(msg_title, msg_text):
- return messaging.AndroidNotification(
- icon='/raw/ic_launcher2',
- color='#AACCDD',
- sound='/raw/shake',
- default_sound=True,
- tag='tagBoom',
- click_action=messaging.AndroidClickAction(
- action_type=1,
- intent='intent://com.huawei.codelabpush/deeplink?#Intent;scheme=pushscheme;launchFlags=0x4000000;i.age=180;S.name=abc;end'),
- body_loc_key='M.String.body',
- body_loc_args=('boy', 'dog'),
- title_loc_key='M.String.title',
- title_loc_args=['Girl', 'Cat'],
- channel_id='1',
- notify_summary='',
- multi_lang_key={'title_key': {'en': 'value1'}, 'body_key': {'en': 'value2'}},
- style=0,
- big_title=msg_title,
- big_body=msg_text,
- auto_clear=86400000,
- notify_id=4861,
- group='Group1',
- importance=messaging.AndroidNotification.PRIORITY_HIGH,
- light_settings=messaging.AndroidLightSettings(color=messaging.AndroidLightSettingsColor(
- alpha=0, red=0, green=1, blue=1), light_on_duration='3.5', light_off_duration='5S'),
- badge=messaging.AndroidBadgeNotification(
- add_num=1, clazz='Classic'),
- visibility=messaging.AndroidNotification.PUBLIC,
- foreground_show=True
- )
|