Quickstart
Installation
pip install python_huawei_push_admin
Usage Examples
This section contains examples of how to use the library.
Setup Environment
# Production ENV
app_id = "your aappId"
app_secret = "your appSecret"
app_package_name = "your packageName"
token_server = "https://oauth-login.cloud.huawei.com/oauth2/v2/token"
push_open_api = "https://push-api.cloud.huawei.com"
# Production EVN (instance APP)
app_id = "your aappId"
app_secret = "your appSecret"
token_server = "https://oauth-login.cloud.huawei.com/oauth2/v2/token"
push_open_api = "https://push-api.cloud.huawei.com"
Send APNs Message
import push_admin
import json
from push_admin import messaging
headers = {
messaging.APNsHeader.HEAD_APNs_ID: "6532dc0e-f581-7bfb-e1ab-60ec3cecea73",
}
apns_alert = messaging.APNsAlert(
title="HMS Push Title",
body="HMS Push Body",
launch_image="Default.png",
custom_data={"k1": "v1", "k2": "v2"},
)
apns_payload_aps = messaging.APNsAps(
alert=apns_alert,
badge=1,
sound="wtewt.mp4",
content_available=True,
category="category",
thread_id="id",
)
payload = messaging.APNsPayload(
aps=apns_payload_aps,
acme_account="jane.appleseed@apple.com",
acme_message="message123456",
)
apns_hms_options = messaging.APNsHMSOptions(target_user_type=1)
apns_push_config = messaging.APNsConfig(
headers=headers,
payload=payload,
apns_hms_options=apns_hms_options,
)
def send_apns_push_message():
"""A sample to show how to send web push message.
:return:
"""
message = messaging.Message(
apns=apns_push_config,
# TODO:
token=["your 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))
def init_app():
"""Init sdk app.
The appID & app Secret use the Android's application ID
and Secret under the same project, next version you can use
the IOS application's own appId & secret!"""
# TODO:
app_id_at = "Your android application's app id"
app_secret_at = "Your android application's app secret"
app_id_push = "Your IOS application' app id "
push_admin.initialize_app(app_id_at, app_secret_at, app_id_push)
def main():
init_app()
send_apns_push_message()
if __name__ == "__main__":
main()
Send Condition Message
import push_admin
import json
from push_admin import messaging
notification = messaging.Notification(
title="sample title",
body="sample message body",
)
android_notification = 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="Your Channel ID",
notify_summary="some summary",
multi_lang_key={
"title_key": {"en": "value1"},
"body_key": {"en": "value2"},
},
style=1,
big_title="Big Boom Title(Topic)",
big_body="Big Boom Body(Topic)",
auto_clear=86400000,
notify_id=486,
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,
)
android = messaging.AndroidConfig(
collapse_key=-1,
urgency=messaging.AndroidConfig.HIGH_PRIORITY,
ttl="10000s",
bi_tag="the_sample_bi_tag_for_receipt_service",
notification=android_notification,
category=None,
)
def send_push_android_data_message():
"""
a sample to show hwo to send web push message
:return:
"""
message = messaging.Message(
notification=notification,
android=android,
# TODO
condition="'your topic' in topics",
)
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))
def init_app():
"""init sdk app"""
# TODO
app_id = "Your android application's app id"
app_secret = "Your android application's app secret"
push_admin.initialize_app(app_id, app_secret)
def main():
init_app()
send_push_android_data_message()
if __name__ == "__main__":
main()
Send Data Message
import push_admin
import json
from push_admin import messaging
"""
[ANDROID] android
"""
android = messaging.AndroidConfig(
collapse_key=-1,
urgency=messaging.AndroidConfig.HIGH_PRIORITY,
ttl="10000s",
bi_tag="the_sample_bi_tag_for_receipt_service",
)
def send_push_android_data_message():
"""
a sample to show hwo to send web push message
:return:
"""
message = messaging.Message(
data="{'k1':'v1', 'k2':'v2'}",
android=android,
# TODO
token=["your 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))
def init_app():
"""init sdk app"""
# TODO
app_id = "Your android application's app id"
app_secret = "Your android application's app secret"
push_admin.initialize_app(app_id, app_secret)
def main():
init_app()
send_push_android_data_message()
if __name__ == "__main__":
main()
Send Instance App Message
import json
import push_admin
from push_admin import messaging
android = messaging.AndroidConfig(
collapse_key=-1,
urgency=messaging.AndroidConfig.HIGH_PRIORITY,
ttl="10000s",
bi_tag="the_sample_bi_tag_for_receipt_service",
fast_app_target=1,
category=None,
)
def send_push_android_data_message():
"""
a sample to show hwo to send web push message
:return:
"""
message = messaging.Message(
# English sample
# data = "{\"pushtype\":0,\"pushbody\":{\"title\":\"Welcome to use Huawei HMS Push Kit?\",\"description\":\"One "
# + "of the best push platform on the planet!!!\",\"page\":\"/\",\"params\":{\"key1\":\"test1\",\"key2\":\"test2\"},\"ringtone\":"
# + "{\"vibration\":\"true\",\"breathLight\":\"true\"}}}",
# Chinese sample
data='{"pushtype":0,"pushbody":{"title":"欢迎使用华为HMS Push Kit!","description":"世界上最好,'
+ '最优秀的推送平台!!!","page":"/","params":{"key1":"test1","key2":"test2"},"ringtone":'
+ '{"vibration":"true","breathLight":"true"}}}',
android=android,
# TODO
token=["your 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))
def init_app():
"""init sdk app"""
# TODO
app_id = "Your instance application's (not android app) app id"
app_secret = "Your instance application's (not android app) app secret"
push_admin.initialize_app(app_id, app_secret)
def main():
init_app()
send_push_android_data_message()
if __name__ == "__main__":
main()
Send Notify Message
import push_admin
import json
from push_admin import messaging
notification = messaging.Notification(
title="sample title",
body="sample message body",
image="https://www.huawei.com/path/icon.png",
)
android_notification = 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="Your Channel ID",
notify_summary="some summary",
multi_lang_key={"title_key": {"en": "value1"},
"body_key": {"en": "value2"}},
style=0,
big_title="Big Boom Title",
big_body="Big Boom 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,
)
android = messaging.AndroidConfig(
collapse_key=-1,
urgency=messaging.AndroidConfig.HIGH_PRIORITY,
ttl="10000s",
bi_tag="the_sample_bi_tag_for_receipt_service",
notification=android_notification,
)
def send_push_android_notify_message():
"""
a sample to show hwo to send web push message
:return:
"""
message = messaging.Message(
notification=notification,
android=android,
# TODO
token=["Your Token"],
)
try:
# TODO
# 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))
def init_app():
"""init sdk app"""
# TODO
app_id = "Your android application's app id"
app_secret = "Your android application's app secret"
push_admin.initialize_app(app_id, app_secret)
def main():
init_app()
send_push_android_notify_message()
if __name__ == "__main__":
main()
Send Test Message
import push_admin
import json
from push_admin import messaging
notification = messaging.Notification(
title="sample title",
body="sample message body",
)
android_notification = 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=["jack", "Cat"],
channel_id="Your Channel ID",
notify_summary="some summary",
multi_lang_key={"title_key": {"en": "value1"},
"body_key": {"en": "value2"}},
style=1,
big_title="Big Boom Title",
big_body="Big Boom Body",
auto_clear=86400000,
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,
)
android = messaging.AndroidConfig(
collapse_key=-1,
urgency=messaging.AndroidConfig.HIGH_PRIORITY,
ttl="10000s",
bi_tag="the_sample_bi_tag_for_receipt_service",
notification=android_notification,
category=None,
)
def send_push_android_data_message():
"""
a sample to show hwo to send web push message
:return:
"""
message = messaging.Message(
notification=notification,
android=android,
# TODO
token=["Your Token"],
)
try:
# Case 1: Local CA sample code
# response = messaging.send_message(message, validate_only=True, verify_peer="../Push-CA-Root.pem")
# Case 2: No verification of HTTPS's certificate
response = messaging.send_message(message, validate_only=True)
# Case 3: use certifi Library
# import certifi
# response = messaging.send_message(message, validate_only=True, verify_peer=certifi.where())
print("response is ", json.dumps(vars(response)))
assert response.code == "80000000"
except Exception as e:
print(repr(e))
def init_app():
"""init sdk app"""
# TODO
app_id = "Your android application's app id"
app_secret = "Your android application's app secret"
push_admin.initialize_app(app_id, app_secret)
def main():
init_app()
send_push_android_data_message()
if __name__ == "__main__":
main()
Send Topic Message
import json
from push_admin import initialize_app
from push_admin import messaging
notification = messaging.Notification(
title="sample title",
body="sample message body",
)
android_notification = 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="Your Channel ID",
notify_summary="some summary",
multi_lang_key={
"title_key": {"en": "value1"},
"body_key": {"en": "value2"},
},
style=1,
big_title="Big Boom Title",
big_body="Big Boom Body",
auto_clear=86400000,
notify_id=486,
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,
)
android = messaging.AndroidConfig(
collapse_key=-1,
urgency=messaging.AndroidConfig.HIGH_PRIORITY,
ttl="10000s",
bi_tag="the_sample_bi_tag_for_receipt_service",
notification=android_notification,
category=None,
)
def send_push_android_data_message():
"""
a sample to show hwo to send web push message
:return:
"""
message = messaging.Message(
notification=notification,
android=android,
# TODO
topic="Your topic",
)
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))
def init_app():
"""init sdk app"""
# TODO
app_id = "Your android application's app id"
app_secret = "Your android application's app secret"
initialize_app(app_id, app_secret)
def main():
init_app()
send_push_android_data_message()
if __name__ == "__main__":
main()
Send WebPush Message
import json
import push_admin
from push_admin import messaging
web_push_headers = messaging.WebPushHeader(ttl="100")
web_push_notification = messaging.WebPushNotification(
title="中文推送",
body="中文推送内容中文推送内容中文推送内容中文推送内容中文推送内容中文推送内容中文推送内容中文推送内容中文推送内容",
icon="https://developer-portalres-drcn.dbankcdn.com/system/modules/org.opencms.portal.template.core/\
resources/images/icon_Promotion.png",
actions=[
messaging.WebPushNotificationAction(
action="click",
title="title",
icon="https://developer-portalres-drcn.\
dbankcdn.com/system/modules/org.opencms.portal.template.core/resources/images/icon_Promotion.png",
),
],
badge="badge",
data="data",
dir="auto",
image="image url",
lang="en",
renotify=False,
require_interaction=False,
silent=True,
tag="tag",
timestamp=32323,
vibrate=[1, 2, 3],
)
web_push_config = messaging.WebPushConfig(
headers=web_push_headers,
notification=web_push_notification,
)
def send_push_android_data_message():
"""
a sample to show hwo to send web push message
:return:
"""
message = messaging.Message(
web_push=web_push_config,
# TODO
token=["your 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))
def init_app():
"""init sdk app
The appID & app Secret use the Android's application ID
and Secret under the same project, next version you can use
the web application's own appId & secret!
"""
# TODO
app_id_at = "Your android application's app id"
app_secret_at = "Your android application's app secret"
app_id_push = "Your Web application' app id "
push_admin.initialize_app(app_id_at, app_secret_at, app_id_push)
def main():
init_app()
send_push_android_data_message()
if __name__ == "__main__":
main()