API Reference

Huawei Admin SDK for Python.

class push_admin.App(appid_at, app_secret_at, appid_push, token_server='https://oauth-login.cloud.huawei.com/oauth2/v3/token', push_open_url='https://push-api.cloud.huawei.com')[source]

Bases: object

Application for HW Cloud Message(HCM).

Parameters:
  • appid_at – Huawei App ID

  • app_secret_at – Huawei App Secret

  • appid_push – Huawei App ID

  • token_server – token server

  • push_open_url – push open url

JSON_ENCODER = <push_admin._message_serializer.MessageSerializer object>
query_subscribe_list(token)[source]

Query subscribe list from Huawei Cloud Messaging (HCM).

Parameters:

token – The specific token

Returns:

send(message, validate_only, **kwargs)[source]

Sends the given message Huawei Cloud Messaging (HCM).

Parameters:
  • message – JSON format message

  • validate_only – validate message format or not

  • kwargs – verify_peer: HTTPS server identity verification, use library ‘certifi’

Returns:

response dict: response body dict

Raise:

ApiCallError: failure reason

subscribe_topic(topic, token_list)[source]

Subscribe topic to Huawei Cloud Messaging (HCM).

Parameters:
  • topic – The specific topic

  • token_list – The token list to be added

Returns:

unsubscribe_topic(topic, token_list)[source]

Unsubscribe topic from Huawei Cloud Messaging (HCM).

Parameters:
  • topic – The specific topic

  • token_list – The token list to be deleted

Returns:

push_admin.get_app(appid=None)[source]

Get app instance.

Parameters:

appid – appid parameters obtained by developer alliance applying for Push service

Returns:

app instance

Raise:

ValueError

push_admin.initialize_app(appid_at, appsecret_at, appid_push=None, token_server='https://oauth-login.cloud.huawei.com/oauth2/v3/token', push_open_url='https://push-api.cloud.huawei.com')[source]

Initializes and returns a new App instance.

Parameters:
  • appid_at – appid parameters obtained by developer alliance applying for Push service

  • appsecret_at – appsecret parameters obtained by developer alliance applying for Push service

  • appid_push – the application Id in the URL

  • token_server – Oauth server URL

  • push_open_url – push open API URL

class push_admin.messaging.APNsAlert(title=None, body=None, loc_key=None, loc_args=None, title_loc_key=None, title_loc_args=None, action_loc_key=None, launch_image=None, custom_data=None)[source]

Bases: object

An alert that can be included in messaging.Aps.

Parameters:
  • title – Title of the notification (optional).

  • body – Body of the notification (optional).

  • loc_key – Localized key to body (optional).

  • loc_args – Localized args to body (optional).

  • title_loc_key – Localized key to title (optional).

  • title_loc_args – Localized args to title (optional).

  • action_loc_key – Localized key to action (optional).

  • launch_image – Image url of the notification (optional).

  • custom_data – Custom data (optional).

class push_admin.messaging.APNsAps(alert=None, badge=None, sound=None, content_available=None, category=None, thread_id=None, mutable_content=None, custom_data=None)[source]

Bases: object

APNs aps definition.

https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/PayloadKeyReference.html#//apple_ref/doc/uid/TP40008194-CH17-SW1

one sample is as follows:

{
    "aps" : {
        "alert" : {
            "title" : "Game Request",
            "body" : "Bob wants to play poker",
            "action-loc-key" : "PLAY"
            "loc-key" : "GAME_PLAY_REQUEST_FORMAT",
            "loc-args" : [ "Jenna", "Frank"],
            "content-available" : 1
        },
        "badge" : 5,
        "sound" : "bingbong.aiff",
    },
    "acme1" : "bar",
    "acme2" : [ "bang",  "whiz" ]
}
class push_admin.messaging.APNsConfig(headers=None, payload=None, apns_hms_options=None)[source]

Bases: object

Please refer to the Apple APNS API reference.

https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingwithAPNs.html

class push_admin.messaging.APNsHMSOptions(target_user_type=None)[source]

Bases: object

Options for features provided by the FCM SDK for iOS.

Parameters:

target_user_type – Developer or Commercial enviroment

class push_admin.messaging.APNsHeader[source]

Bases: object

  • authorization

  • apns-id

  • apns-expiration

  • apns-priority

  • apns-topic

  • apns-collapse-id

HEAD_APNs_COLLAPSE_ID = 'apns-collapse-id'
HEAD_APNs_EXPIRATION = 'apns-expiration'
HEAD_APNs_ID = 'apns-id'
HEAD_APNs_PRIORITY = 'apns-priority'
HEAD_APNs_TOPIC = 'pns-topic'
HEAD_AUTHORIZATION = 'authorization'
class push_admin.messaging.APNsPayload(aps, **kwargs)[source]

Bases: object

APNs payload definition.

class push_admin.messaging.AndroidBadgeNotification(add_num=None, set_num=None, clazz=None)[source]

Bases: object

A BadgeNotification that can be included in a message.android.notification.

Parameters:
  • add_num – message number of badge notification in the android.notification (optional).

  • set_num – set the specific number of badge notification (optional).

  • clazz – message class of badge notification in the android.notification (optional).

class push_admin.messaging.AndroidClickAction(action_type=None, intent=None, action=None, url=None)[source]

Bases: object

A ClickAction that can be included in a message.android.notification.

Parameters:
  • action_type – type of the android.notification (optional).

  • intent – intent of the android.notification (optional).

  • url – url of the android.notification (optional).

  • action – action definition for push message 1: to specific activity of application 2: specific URL 3: to specific application

class push_admin.messaging.AndroidConfig(collapse_key=None, urgency='NORMAL', ttl=None, bi_tag=None, fast_app_target=None, notification=None, data=None, category=None)[source]

Bases: object

Android-specific options that can be included in a message.

Parameters:
  • collapse_key – An identifier for a group of messages (optional).

  • urgency – A string indicating how important the message is. The value must be one of “NORMAL” or “HIGH”.

  • ttl – The time-to-live duration of the message in seconds (optional).

  • bi_tag – A developer-defined message identifier (optional).

  • fast_app_target – A boolean indicating if the message should be sent as a fast app launch (optional).

  • notification – A messaging.AndroidNotification to be included in the message (optional).

  • data – A messaging.AndroidData to be included in the message (optional).

  • category – A string indicating the category of the message (optional).

HIGH_PRIORITY = 'HIGH'
NORMAL_PRIORITY = 'NORMAL'
class push_admin.messaging.AndroidLightSettings(color=None, light_on_duration=None, light_off_duration=None)[source]

Bases: object

A LightSettings that can be included in a message.android.notification.

Parameters:
  • color – light color of the android.notification (optional).

  • light_on_duration – light on duration of the android.notification (optional).

  • light_off_duration – light off duration of the android.notification (optional).

"light_settings": {
    "color":{
        "alpha":0,
        "red":0,
        "green":1,
        "blue":1
    },
    "light_on_duration":"3.5",
    "light_off_duration":"5S"
}
class push_admin.messaging.AndroidLightSettingsColor(alpha=None, red=None, green=None, blue=None)[source]

Bases: object

A LightSettingsColor that can be included in a message.android.notification.

Parameters:
  • alpha – light color alpha of the android.notification (optional).

  • red – light color red of the android.notification (optional).

  • green – light color green of the android.notification (optional).

  • blue – light color blue of the android.notification (optional).

"color": {
        "alpha":0,
        "red":0,
        "green":1,
        "blue":1
}
class push_admin.messaging.AndroidNotification(title=None, body=None, icon=None, color=None, sound=None, default_sound=None, tag=None, click_action=None, body_loc_key=None, body_loc_args=None, title_loc_key=None, title_loc_args=None, multi_lang_key=None, channel_id=None, notify_summary=None, image=None, style=None, big_title=None, big_body=None, auto_clear=None, notify_id=None, group=None, badge=None, ticker=None, auto_cancel=None, when=None, importance=None, use_default_vibrate=True, use_default_light=True, vibrate_config=None, visibility=None, light_settings=None, foreground_show=False)[source]

Bases: object

Android-specific notification parameters.

Parameters:
  • title – The notification’s title (optional).

  • body – The notification’s body text (optional).

  • icon – The name of a drawable resource that provides the notification icon (optional).

  • color – The color of the notification icon in ARGB format (optional).

  • sound – The sound to play when the device receives the notification (optional).

  • default_sound – A boolean indicating if the notification should use the default sound (optional).

  • tag – The notification tag (optional).

  • click_action – The action associated with a user click on the notification (optional).

  • body_loc_key – The key to the body string in the app’s string resources (optional).

  • body_loc_args – An array of resource keys that will be used in place of the format specifiers in body_loc_key (optional).

  • title_loc_key – The key to the title string in the app’s string resources (optional).

  • title_loc_args – An array of resource keys that will be used in place of the format specifiers in title_loc_key (optional).

  • multi_lang_key – The key to the title string in the app’s string resources (optional).

  • channel_id – The notification’s channel ID (optional).

  • notify_summary – The summary of the notification (optional).

  • image – The URL of the image to display in the notification (optional).

  • style – The style of the notification (optional).

  • big_title – The big title of the notification (optional).

  • big_body – The big body of the notification (optional).

  • auto_clear – The auto clear duration of the notification (optional).

  • notify_id – The notification ID (optional).

  • group – The notification group (optional).

  • badge – The notification badge (optional).

  • ticker – The notification ticker (optional).

  • auto_cancel – The auto cancel duration of the notification (optional).

  • when – The notification’s timestamp (optional).

  • importance – The notification’s importance (optional).

  • use_default_vibrate – A boolean indicating if the notification should use the default vibrate (optional).

  • use_default_light – A boolean indicating if the notification should use the default light (optional).

  • vibrate_config – The vibrate configuration (optional).

  • visibility – The visibility of the notification (optional).

  • light_settings – The light settings (optional).

  • foreground_show – A boolean indicating if the notification should be shown in the foreground (optional).

PRIORITY_DEFAULT = 'NORMAL'
PRIORITY_HIGH = 'HIGH'
PRIORITY_LOW = 'LOW'
PRIVATE = 'PRIVATE'
PUBLIC = 'PUBLIC'
SECRET = 'SECRET'
VISIBILITY_UNSPECIFIED = 'VISIBILITY_UNSPECIFIED'
exception push_admin.messaging.ApiCallError(message, detail=None)[source]

Bases: Exception

Represents an Exception encountered while invoking the HCM API.

Attributes :param message: A error message string. :param detail: Original low-level exception (optional).

class push_admin.messaging.BaseTopicResponse(json_rsp=None)[source]

Bases: object

The response received from an topic subscribe request to the HCM API.

Parameters:

json_rsp – received http response body text from HCM.

{
   "msg": "Success",
   "code": "80000000",
   "requestId": "157466304904000004000701"
}
property code

errcode

property msg

the description of errcode

property requestId

A message ID string that uniquely identifies the message.

class push_admin.messaging.Message(data=None, notification=None, android=None, apns=None, web_push=None, token=None, topic=None, condition=None)[source]

Bases: object

A message that can be sent Huawei Cloud Messaging.

Parameters:
  • data – A string value.

  • notification – An instance of messaging.Notification (optional).

  • android – An instance of messaging.Android (optional).

  • apns – APSN related message definition

  • web_push – Web Push related message definition

  • token – token list, must be tuple (optional).

  • topic – message topic, must be string (optional).

  • condition – message condition, must be string (optional).

class push_admin.messaging.Notification(title=None, body=None, image=None)[source]

Bases: object

A notification that can be included in a message.

Parameters:
  • title – Title of the notification (optional).

  • body – Body of the notification (optional).

  • image – Image url of the notification (optional).

class push_admin.messaging.SendResponse(response=None)[source]

Bases: object

The response received from an send request to the HCM API.

Parameters:

response – received http response body text from HCM.

property code

errcode

property reason

the description of errcode

property requestId

A message ID string that uniquely identifies the message.

class push_admin.messaging.TopicQueryResponse(json_rsp=None)[source]

Bases: BaseTopicResponse

{
  "msg": "success",
  "code": "80000000",
  "requestId": "157466350121600008000701",
  "topics": [
      {
          "name": "sports",
          "addDate": "2019-11-25"
      }
  ]
}
property topics

The list of topics.

class push_admin.messaging.TopicSubscribeResponse(json_rsp=None)[source]

Bases: BaseTopicResponse

{
  "msg": "Success",
  "code": "80000000",
  "requestId": "157466304904000004000701",
  "successCount": 2,
  "failureCount": 0,
  "errors": []
}
property errors

The list of errors.

property failureCount

The number of failed subscriptions.

property successCount

The number of successful subscriptions.

class push_admin.messaging.WebPushConfig(headers=None, data=None, notification=None, hms_options=None)[source]

Bases: object

Web push-specific options that can be included in a message.

For Web Push Specification Reference: https://tools.ietf.org/html/rfc8030#section-5

For mozilla implementation: https://developer.mozilla.org/en-US/docs/Web/API/notification

Parameters:
  • headers – A dictionary of headers (optional). Refer Web push Specification for supported headers.

  • notification – A messaging.WebPushNotification to be included in the message (optional).

  • hms_options – A WebPushHMSOptions instance to be included in the message(optional).

TOPIC_HEADER = 'topic'
TTL_HEADER = 'ttl'
URGENCY_HEADER = 'urgency'
data

Refer to WebPushNotification

headers

message deliver to the end application directly

notification

Refer to WebPushHMSOptions

class push_admin.messaging.WebPushHMSOptions(link=None)[source]

Bases: object

Optional link option.

Parameters:

link – The link to be included in the message.

class push_admin.messaging.WebPushHeader(ttl=None, urgency=None, topic=None)[source]

Bases: object

Web Push Header.

refer to: https://tools.ietf.org/html/rfc7240

class push_admin.messaging.WebPushNotification(title=None, body=None, icon=None, actions=None, badge=None, data=None, dir=None, image=None, lang=None, renotify=None, require_interaction=None, silent=None, tag=None, timestamp=None, vibrate=None)[source]

Bases: object

Web Push Notification.

body

Refer to WebPushNotificationAction

class push_admin.messaging.WebPushNotificationAction(action=None, title=None, icon=None)[source]

Bases: object

The action for web push notification.

Parameters:
  • action

  • title

  • icon

push_admin.messaging.list_topics(token, app_id=None)[source]

Queries the subscription list of the given token.

Parameters:
  • token – The token to be queried

  • app_id – application ID

push_admin.messaging.send_message(message, validate_only=False, app_id=None, verify_peer=False)[source]

Sends the given message Huawei Cloud Messaging (HCM).

Parameters:
  • message – An instance of messaging.Message.

  • validate_only – A boolean indicating whether to run the operation in dry run mode (optional).

  • app_id – app id parameters obtained by developer alliance applying for Push service (optional).

  • verify_peer – (optional) Either a boolean, in which case it controls whether we verify the server’s TLS certificate, or a string, in which case it must be a path to a CA bundle to use. Defaults to True.

Returns:

SendResponse

Raises:

ApiCallError: If an error occurs while sending the message to the HCM service.

push_admin.messaging.subscribe_topic(topic, token_list, app_id=None)[source]

Subscribes the given token list to the given topic.

Parameters:
  • topic – The specific topic

  • token_list – The token list to be added

  • app_id – application ID

push_admin.messaging.unsubscribe_topic(topic, token_list, app_id=None)[source]

Unsubscribes the given token list from the given topic.

Parameters:
  • topic – The specific topic

  • token_list – The token list to be deleted

  • app_id – application ID