import json 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_android_notify_message(self, title, body, image, token): """ 发送推送消息 :param: token :return: """ notification = messaging.Notification( title=title, body=body, image=image ) # 推送通知内容配置 android_notification = self.android_notification(title, body) # 安卓配置 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] ) 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()) print('response is ', json.dumps(vars(response))) assert (response.code == '80000000') except Exception as e: print(repr(e)) @staticmethod def android_notification(title, body): 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='some summary', multi_lang_key={'title_key': {'en': 'value1'}, 'body_key': {'en': 'value2'}}, style=0, big_title=title, big_body=body, 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 )