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, token_val, msg_title, msg_text, image_url=None, uid='', channel='', n_time='', event_type='', app_bundle_id=''): """ 发送推送消息 @param token_val: 手机推送token @param msg_title: 标题 @param msg_text: 内容 @param n_time: 不使用 @param event_type: 不使用 @param app_bundle_id: 不使用 @param uid: 不使用 @param channel: 不使用 @param msg_text: 不使用 @param image_url: 图片链接 @return: """ logger = logging.getLogger('info') logger.info('华为推送参数:{}, {}, {}, {}'.format(token_val, msg_title, msg_text, image_url)) 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, 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=1, big_title=msg_title, big_body=msg_text, auto_clear=86400000, 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 )