[Huawei Developers] Freemind R:
Now you have a quick app released on HUAWEI AppGallery. Do you want to attract more users and win more traffic for it? HUAWEI Push Kit can help you with that. You can push messages at the right moment to users for them to open your quick app to view more details. This better engages your users and improves activity.

Typical scenarios of push messages are as follows:

l Shopping quick apps can push information about new products and discounts to users to attract more customers for merchants.

l Reading quick apps can push information such as popular books and chapter updates to users, providing up-to-date information that users concern about.

l Food & drink quick apps can push information about food and good restaurants to users, facilitating user's life and bringing more users for the catering industry.

Follow instructions in this document to integrate HUAWEI Push Kit to your quick app to get all these benefits.

Getting Started

The hardware requirements are as follows:

l A computer with Node.js 10 or later and HUAWEI Quick App IDE of the latest version installed

l A Huawei mobile phone running EMUI 8.0 or later (with a USB cable)

Development Guide

Enabling HUAWEI Push Kit

1. Sign in to AppGallery Connect and click My projects.

2. Click the card of the project for which you need to enable the service. Click the Manage APIs tab, and toggle the Push Kit switch.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/025/801/232/5190044000025801232.20200924061549.10344009023236563896285743108336:50511109065958:2800:A6500BCA85A7433718AC5464DFD32DE6DEDEE438E9B4EF8854E4C85F7BA9B9DE.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/025/801/232/5190044000025801232.20200924061549.10344009023236563896285743108336:50511109065958:2800:A6500BCA85A7433718AC5464DFD32DE6DEDEE438E9B4EF8854E4C85F7BA9B9DE.png[/IMG]

3. Click the General information tab. In the App information area, set SHA-256 certificate fingerprint to the SHA-256 fingerprint.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/025/801/232/5190044000025801232.20200924061612.74929883398170274840629441905077:50511109100155:2800:C6EE4835585E7EC78E6E5AF3A02F8573921B6E4EF01CF543AC07CB16D5E17C54.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/025/801/232/5190044000025801232.20200924061612.74929883398170274840629441905077:50511109100155:2800:C6EE4835585E7EC78E6E5AF3A02F8573921B6E4EF01CF543AC07CB16D5E17C54.png[/IMG]

Subscribing to Push Messages

The service process is as follows.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/025/801/232/5190044000025801232.20200924061630.90005939590182641628480279041272:50511109065958:2800:8A61D91B861AAF30380529B7E2657DE0DF941A02B915F39E3CF24989CA147A87.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/025/801/232/5190044000025801232.20200924061630.90005939590182641628480279041272:50511109065958:2800:8A61D91B861AAF30380529B7E2657DE0DF941A02B915F39E3CF24989CA147A87.png[/IMG]

1. Call the push.getProvider API to check whether the current device supports HUAWEI Push Kit. If huawei is returned, the device supports HUAWEI Push Kit. You can call other relevant APIs only when the device supports HUAWEI Push Kit.

2. Call the push.subscribe API to obtain regId (registered ID). The ID is also called a token or push token, which identifies messages sent by HUAWEI Push Kit.

Notice: It is recommended that the preceding APIs be called in app.ux that takes effect globally.

3. Report the obtained regId to the server of your quick app through the Fetch Data API, so the server can push messages to the device based on regId. Generally, the value of regId does not change and does not need to be reported to the server each time after it is obtained.

You are advised to call the Data Storage API to store the regId locally. Each time the regId is obtained, compare the regId with that stored locally. If they are the same, the regId does not need to be reported to the server. Otherwise, report the regId to the server.

The service process is as follows.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/025/801/232/5190044000025801232.20200924061707.67650507372239047185961209575633:50511109065958:2800:7577C9467178443FB3ED605B4ACC791058549A7E7644822CC832CB6E22A2A8D3.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/025/801/232/5190044000025801232.20200924061707.67650507372239047185961209575633:50511109065958:2800:7577C9467178443FB3ED605B4ACC791058549A7E7644822CC832CB6E22A2A8D3.png[/IMG]

The sample code for comparing the obtained regId with that stored locally is as follows:

checkToken() {
var subscribeToken=this.$app.$def.dataApp.pushtoken;
console.log("checkToken subscribeToken= "+subscribeToken);
var storage = require("@system.storage");
var that=this;
storage.get({
key: 'token',
success: function (data) {
console.log("checkToken handling success data= "+data);
if (subscribeToken != data) {
// Report regId to your service server.
that.uploadToken(subscribeToken);
that.saveToken(subscribeToken);
}
},
fail: function (data, code) {
console.log("handling fail, code = " + code);
}
})
},
The sample code for saving regId locally is as follows:
saveToken(subscribeToken){
console.log("saveToken");
var storage = require("@system.storage");
storage.set({
key: 'token',
value: subscribeToken,
success: function (data) {
console.log("saveToken handling success");
},
fail: function (data, code) {
console.log("saveToken handling fail, code = " + code);
}
})
},

Testing Push Message Sending

You can test the push function by sending a push message to your test device. In this case, regId (push token) uniquely identifies your device. With it, your service server can accurately identify your device and push the message to your quick app on the device. Your device can receive the pushed message in any of the following conditions:

l The quick app has been added to the home screen.

l The quick app has been added to MY QUICK APP.

l The quick app has been used before.

l The quick app is running.

You can use either of the following methods to send a push message to a quick app that has been released on AppGallery or run by Huawei Quick App Loader.

l Select some target users in AppGallery Connect and send the push message.

l Call the specific server API to send the push message to users in batches.

Sending a Push Message Through AppGallery Connect

This method is simple. You only need to configure the recipients in AppGallery Connect. However, the target user scope is smaller than that through the API.

1. Sign in to AppGallery Connect and click My apps.

2. Find your app from the list and click the version that you want to test.

3. Go to Operate > Promotion > Push Kit. Click Add notification and configure the push message to be sent.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/025/801/232/5190044000025801232.20200924061725.11574775887808825308433349259096:50511109065958:2800:54C6E5BA54820BD8F9E9D3ACBF949DFE2544AA5C1E9E04D19C42AE91DEDFFD9F.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/025/801/232/5190044000025801232.20200924061725.11574775887808825308433349259096:50511109065958:2800:54C6E5BA54820BD8F9E9D3ACBF949DFE2544AA5C1E9E04D19C42AE91DEDFFD9F.png[/IMG]

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/025/801/232/5190044000025801232.20200924061733.30615858069275915670897359138587:50511109065958:2800:7C77F60ACE9D9873345D44ABA01E933462530090D51B5C5E82C2D9793BADDE1A.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/025/801/232/5190044000025801232.20200924061733.30615858069275915670897359138587:50511109065958:2800:7C77F60ACE9D9873345D44ABA01E933462530090D51B5C5E82C2D9793BADDE1A.png[/IMG]

Sending a Push Message by Calling the API

1. Call the accessToken API to obtain the access token, which will be used in the push message sending API.

2. Call the push message sending API. The sample code of the push message body is as follows:

var mData = {
"pushtype": 0, // The value 0 indicates a notification message.
"pushbody": {
"title": "How can young people make a living? ",
"description": "Why did he choose to be a security guard after college graduation? ",
"page": "/", // Path of the quick app page that is displayed when a user taps a notification message. This parameter is valid only when pushtype is set to 0. /" Go to the app home page.
"params": {
"key1": "test1",
"key2": "test2"
},
"ringtone": {
"vibration": "true",
"breathLight": "true"
}
}
};
var formatJsonString = JSON.stringify(mData);
var messbody = {
"validate_only": false,
"message": {
"data": formatJsonString, // The data type is JsonString, not a JSON object.
"android": {
"fast_app_target": 1, // The value 1 indicates that a push message is sent to a quick app running by Huawei Quick App Loader. To send a push message to a quick app from AppGallery, set this parameter to 2.
"collapse_key": -1,
"delivery_priority": "HIGH",
"ttl": "1448s",
"bi_tag": "Trump",
},
"token": ['pushtoken1','pushtoken2'], // Target users of message pushing.
}
};

For details, please refer to Accessing HUAWEI Push Kit.