Guides & References

Access comprehensive guides and references for implementing the Push Kit in Unity with HMS Unity Plugin.

Enabling Push Kit

In HUAWEI Developer AppGallery Connect, go to My projects> [your project] > Manage APIs and enable Push Kit on this page.

Select the “Push Kit” from the menu on the left and activate the service by clicking the Enable Now button.

Then enable the push kit from the plugin's panel in Unity Editor.

Obtaining a Push Token

Scenario Description

A token uniquely identifies an app on a device. An app can call the getToken method to request a token from the Push Kit server. If no token is returned by getToken, the onNewToken method can be used to obtain one. We are handling getToken in the init method. Therefore you need to set the OnTokenSuccess action with your own action and then call the init method.

The token is only relevant to your app ID and is irrelevant to the HUAWEI ID. Therefore, the token will not be changed after HUAWEI ID change.

Generally, the token changes only in the following scenarios. Therefore, do not request tokens frequently, especially for apps running in the background.

  • The app is re-launched after app data is cleared (when the app is reinstalled, when the device is restored to its factory settings, or in other scenarios).

  • The app explicitly calls the method for deleting a push token and then calls the getToken method.

NOTE

To improve security, Push Kit updated token encoding rules in November 2020. If a user updates the Push Service app to the new version, after the user reinstalls your app and launches it, the token that your app requests are in the latest encoding format.

Precautions

  • Do not use push tokens to trace and mark users.

  • Do not let your app verify the push token length because it is variable.

  • Set the OnTokenSuccess action with your own action in the code to ensure that the push token can be returned.

  • We recommend you to init the push kit in LateStart because of the problems that may occur while init process with other kits at the same time.

Procedure

Deleting a Push Token

Scenario Description

After a user rejects the user agreement and privacy statement of your app, you can call the deleteToken method to delete the corresponding push token from the Push Kit server. After doing so, your app on the user's device will no longer receive push messages from your app server.

You are advised not to call the deleteToken method to delete the push token. Instead, it is recommended that you invalidate push tokens for users who reject your app's user agreement and privacy statement and control your app server not to send messages to your app with invalid push tokens. This can reduce the frequency of your app server requesting the Push Kit server.

NOTE

  • Do not delete push tokens frequently.

  • To delete a push token, ensure that the HMS Core (APK) version is 3.0.0 or later.

  • If you do not want your app to receive notification messages, you can call the turnOffPush method to disable the function of displaying notification messages.

  • The user agreement and privacy statement of your app must include and comply with the HUAWEI Push Service Agreement and SDK Privacy and Security Statement.

HMSPushKitManager.Instance.DeleteToken();

Sending a Notification Message

  1. Sign in to AppGallery Connect, click My projects, and find and click the desired project in the project list. Then, go to Grow > Push Kit > Notifications and click Add notification to create a task.

  2. On the page displayed, set the message name, title, and body, and set the message type to the notification message. For details about other parameters, please refer to Scenario Description.

  3. Click Test effect, enter the obtained push token and click OK.

Sending a Data Message

  1. Sign in to AppGallery Connect, click My projects, and find and click the desired project in the project list. Then, go to Grow > Push Kit > Notifications and click Add notification to create a task.

  2. On the page displayed, set related parameters, including the message name, type (data message), and key-value pair or custom parameter (that is used to carry message data).

  3. Select your app and enter the push token in the Push scope area, and click Submit in the upper right corner.

CallBacks

Action ParametersDescription

OnTokenSuccess

string

Called after the Push Kit server updated the token.

OnTokenFailure

Exception

Called when a token fails to be obtained.

OnTokenBundleSuccess

string, Bundle

Called when the Push Kit server updates the sender token in the multi-sender scenario.

OnTokenBundleFailure

Exception, Bundle

Called when a token fails to be obtained in the multi-sender scenario.

OnMessageSentSuccess

string

Called after an uplink message is successfully sent.

OnSendFailure

string, Exception

Called after an uplink message fails to be sent.

OnMessageDeliveredSuccess

string, Exception

Sends the response from your server to your app after an uplink message reaches your server if the receipt is enabled.

OnMessageReceivedSuccess

Receives data messages.

OnNotificationMessage

Called if the notification is clicked while the game is in the foreground.

NotificationMessageOnStart

Called if the game is opened by clicking on notifications when the game is closed

Last updated