目录

1. Scenario Description

HUAWEI Push Kit provides the capability of sending messages to web apps (browsers) via a WebPush agent.For details about WebPush-related standards, please refer to RFC8030.

The following figure shows a message that is sent to a web app via the WebPush agent.


Supported browsers:

Device   Type

Browser   Type

Supported   (Yes/No)

Version

PC

Chrome

Yes

78 or later

Firefox

Yes

56 or later

Edge

Yes

44.18362.1.0 or later

2. Preparations

Currently, messages are pushed to a web app via the WebPush agent of an Android app created in AppGallery Connect. For details about how to create an Android app, please refer to the Preparations for Android apps.

2.1 Enabling the WebPush Agent

After enabling HUAWEI Push Kit, enable the WebPush agent.

2.2 Generating a Key Pair for the WebPush Agent

Click Generate key pair to generate a valid key pair.

You can also run a command to generate your key, and then remove all linefeeds when importing the key.

1. Run the openssl ecparam -name prime256v1 -genkey -noout -out vapid_private.pem command to generate a VAPID key.

2. Create a Base64-encoded key string in DER format.

openssl ec -in ./vapid_private.pem -outform DER|tail -c +8|head -c 32|base64|tr -d '=' |tr '/+' '_-' >> private_key.txt

openssl ec -in ./vapid_private.pem -pubout -outform DER|tail -c 65|base64|tr -d '=' |tr '/+' '_-' >> public_key.txt

3. Development Guide

3.1 Project Configuration

Copy the code snippets configured for enabling the WebPush agent and paste them to the end of the <body> tag in your project. Develop the code in the red box by yourself.

3.2 Receiving Messages (Foreground)

Develop the messaging.onMessage(payload) callback function for receiving messages when the browser is running in the foreground.

Sample code:

  1. messaging.onMessage((payload) => {
  2.    console.log('Message received. ', payload);
  3. });

3.3Receiving Messages (Background)

Develop the messaging.setBackgroundMessageHandler(payload) callback function of the background process serviceworker, for receving messages when the browser is running in the background.

Sample code:

  1. messaging.setBackgroundMessageHandler(function (payload) {
  2.  console.log('[hms-messaging-sw.js] Received background message ', payload);
  3.  const notificationTitle = 'Background Message Title';
  4.  const notificationOptions = {
  5.    body: 'Background Message body.',
  6.    icon: '/hms-logo.png'
  7.  };
  8.  return self.registration.showNotification(notificationTitle,
  9.    notificationOptions);
  10. });

3.4 Applying for a Token

Call the getToken method to apply for a token.

Sample code:

  1. messaging.getToken().then((currentToken) => {
  2.    if (currentToken) {
  3.        console.log('Current token: ', currentToken);
  4.    } else {
  5.        console.log('No Instance ID token available. Request permission to generate one.');
  6.    }
  7. }).catch((err) => {
  8.    console.log('An error occurred while retrieving token. ', err);
  9. });

3.5 Sending Messages

Use the token to call the API provided by the HUAWEI Push Kit server to send messages. For details, please refer to Sending Messages.For a data message, the orignData parameter in the received message specifies the message content.

Sample code for sending messages from the app server:

  1. {
  2.    "validate_only": false,
  3.    "message": {
  4.        "notification": {
  5.            "title": "Big News",
  6.            "body": "This is a Big News!"
  7.        },
  8.        "data": "this is data",
  9.        "webpush": {
  10.            "headers": {
  11.                "ttl": "990",
  12.                "urgency": "low",
  13.                "topic": "12313"
  14.            },
  15.             "notification": {
  16.                "title": "notication string",
  17.                "body": "web push body",
  18.                "actions": [
  19.                    {
  20.                        "action": "",
  21.                        "icon": "",
  22.                        "title": "string"
  23.                    }
  24.                ],
  25.                "badge": "string",
  26.                "dir": "auto",
  27.                "icon": "string",
  28.                "image": "string",
  29.                "lang": "string",
  30.                "renotify": true,
  31.                "require_interaction": true,
  32.                "silent": true,
  33.                "tag": "string",
  34.                "timestamp": 1545201266,
  35.                "vibrate": [100,200,300]
  36.            },
  37.            "hms_options": {
  38.                "link": "https://example.com"
  39.            }
  40.        },
  41.        "token": [
  42.            "your token1",
  43.            "your token2"
  44.        ]
  45.    }
  46. }