Teach you how to access the fast application Push

  After more and more CPs have launched fast apps on AGC, they hope to attract more users and increase the activity of the apps, thereby bringing more traffic and ultimately realizing their commercial value. Quick app provides standard Push capabilities. Developers can push messages to users, such as pushing a good book, pushing a gourmet meal, etc. Click on the message to launch the quick app.

Applicable scene

  • Scene 1: Shopping

    Shopping mall-type quick apps connect to Push to send users notifications of price reductions for their favorite products.

    • Scene 2: Reading

    Reading fast apps can recommend good books and chapter updates to users by connecting to Push, so that users can get the latest progress in the first time.

    • Scene 3: Food

    Fast food apps connect to Push to recommend delicious things to users and recommend good restaurants, which is convenient for users and brings popularity and commercial benefits to the catering industry.

Introduction to Push

  • Huawei Push is a message push platform provided for developers. It has established a message push channel from the cloud to the mobile phone, allowing applications to notify users of the latest information in a timely manner, thereby building a good user relationship and enhancing user perception and activity degree.

  • The Quick Application Push function is supported on mobile phones with EMUI8.0 and above.

  • The process of sending PUSH messages is shown in the following figure:

Insert picture description here
Push interface introduction:

Module name: service.push

Module introduction: import push from'@service.push' or var push = require("@service.push")

Supported interfaces:
Insert picture description here

Development preparation

  Hardware requirements

  • A computer (Quick App IDE needs to be installed).

  • A Huawei mobile phone (with USB data cable), used to run the developed application, EMUI 8.0 and above.

  Software requirements

  • Install Node.js 10 or higher on PC

  • Install the latest version of Quick App IDE on the PC

  other requirements

  • To apply for PUSH service in the Huawei Developer Alliance, please refer to: Activate Push Service for specific operations.

  Required knowledge

  • Javascript+css

Development steps

  1. Call the push.getProvider interface to determine whether the current device supports Huawei's PUSH service. The interface returns huawei to indicate support, otherwise it means not. Subsequent Push other interface calls must be made on the premise that the device supports the PUSH service.

    1. Call the push.subscribe interface to get the regId. regId is also called token or push token, and is an identifier used to send push messages.

      Note: The above interface is recommended to be called in the global app.ux.

The sample code is as follows:

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");
                }
            })
        },
  1. The regId obtained in 2 is reported to the fast app's own business server through the fast app fetch interface for subsequent server to send push messages to the fast app. Generally, the regId will not change, and it does not need to be reported to the server after each acquisition.

    Recommended practice : Use the fast app storage interface to store the regId locally, and compare it with the local storage each time the regId is obtained. If it is equal, it will not be reported to the business server. If it is not equal, it means that there is a change and need to be reported to the business server. The flowchart is shown in the following figure:
    Insert picture description here
    The code logic of Push token and local cache comparison 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) {
                       //上报到自己的业务服务器
                         that.uploadToken(subscribeToken);
                        that.saveToken(subscribeToken);
                    }
                },
                fail: function (data, code) {
                    console.log("handling fail, code = " + code);
                }
            })
        },

The sample code uploaded to the developer business server is as follows:

uploadToken(subscribeToken) {
            console.log("uploadToken");
            var that = this;
            var body = {
                xx: subscribeToken
            };
            fetch.fetch({
              url: 'https://xxx',        
              method: 'POST',
              data: body,
              success: function (data) {
                 console.log("uploadToken code: " + data.code);
                 console.log("uploadToken data: " + data.data);    
                },
                fail: function (data, code) {
                 console.log("handling fail, code = " + code);
                }
            })
        },

Note : The parameters in the fetch interface call in the above sample code need to replace the business's own server address. For more information on the fetch interface, please refer to the official website API fetch.

Save Pushtoken to the local code 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);
             }
            })
        },

Push test

  The client development process has ended, isn’t it easy? Next, how to test push messages on mobile phones? At this time, pushtoken is very important. It is a symbol that uniquely identifies the device. With it, the push service can accurately send messages to the correct mobile phone and application. To receive notification bar messages on your mobile phone, one of the following conditions must be met:

  1. Quick application with shortcuts on the desktop

  2. Join the quick app of "My quick app"

  3. Quick app with usage history

  4. Quick app is running

Currently, Push messages can be sent in the following two ways:

  • Send in Huawei Developer Union: It is applicable to select some target users to send, and supports fast apps pushed to the live network (already on the app market) and fast apps running on the loader. For specific steps, please refer to the official website https://developer.huawei.com/consumer/cn/doc/development/quickApp-Guides/quickapp-access-push-kit

  • Sending through the server interface: It is suitable for sending to a large number of users, and supports the fast application that is pushed to the live network and the fast application running on the loader. The server interface sending involves the accessToken interface and the push message interface. Please refer to the official website for the interface protocol format.

    1) First, access the accessToken interface to obtain access_token, which is used in the push message interface.

    2) Then visit the send push message interface and send the push message. The sample code screenshot (js code) of Push notification bar message body is as follows:
    Insert picture description here

FAQ

Q1: Can push push not fill in the regid, can all users push?

No, the push is based on the regid. A regid represents a user.

Q2: When sending a Push message on AGC, it will disappear after filling in the token. How to deal with it?

After filling in the token, add a comma to end.

Q3: When the server interface sends, it prompts that the token is illegal. What is the reason?

Please check whether the package name and signature fingerprint used by Huawei to apply for the Push service are consistent with the package name and signature fingerprint in the debugging application.

Please check whether pushtoken is the token returned by Huawei Quick App Engine.

Q4: How do I know whether the pushed message has arrived on the user's mobile phone?

  • When the Push service is activated on the AGC, you can fill in the return receipt url, and you can see the push status returned by the Huawei Push service in the return receipt result. For example, Push sends the request result as follows:

  {"code":"80000000","msg":"Success","request_id":"159963843932289603004301"}

  • The results of the receipt are as follows:

  {"appid":"102580573","biTag":"d1c4b1c9cf624bfcb400e76da1446ad6","status":102,"timestamp":1599638444727,"token":"AMaY1irGfU2uV2LwcW65m9NgRc9MgMYLA9v_w2s-MhLtxceDiBHVUXXX9ifS2S14mYbvzqsQwLl9NTcF7R-DdBr_33x_V4BzVJFSrLwZjWWYfGz_AGQpWnkzLpeXellUzw"}

The request result is returned successfully, but the status of the receipt result is 102. In fact, the message did not arrive on the mobile phone. The “status”: 102 in the receipt result indicates that the message is frequency controlled.

For more details, please refer to:

Quick application development guide documents:https://developer.huawei.com/consumer/cn/doc/development/quickApp-Guides/quickapp-whitepaper

Quick application Push access:https://developer.huawei.com/consumer/cn/doc/development/quickApp-Guides/quickapp-access-push-kit


Original link:https://developer.huawei.com/consumer/cn/forum/topicview?tid=0202381629952320300&fid=18
Author: Drum Chao

Guess you like

Origin blog.51cto.com/14772288/2542657