[Huawei Developers] Martin Bieber:
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.

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

Subscribing to Push Messages

The service process is as follows.

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.

checkPushIsSupported(){

let provider= push.getProvider();

console.log("checkPush provider= "+provider);

if(provider==='huawei'){

this.pushsubscribe();

}

},

pushsubscribe() {

console.log("pushsubscribe start");

var that=this;

push.subscribe({

success: function (data) {

console.log("push.subscribe succeeded, result data=" + JSON.stringify(data));

that.dataApp.pushtoken=data.regId;

},

fail: function (data, code) {

console.log("push.subscribe failed, result data=" + JSON.stringify(data) + ", code=" + code);

},

complete: function () {

console.log("push.subscribe completed");

}

})

},

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.

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.

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.