目录

From the HMS Core Push SDK 4.0.4, Push Kit provides the capability of sending uplink messages. That is, messages can be sent from apps to your app server.

The Push Kit client supports message caching and resending. That is, when an uplink message fails to be sent by your app to your app server due to a reason such as network unavailability, the message is cached on the client and will be resent at a proper time. You can use the setSendMode(int sendMode) method in RemoteMessage.Builder to set sendMode. If this parameter is set to 1, messages need to be cached and resent. (By default, messages cannot be cached and resent.) If the message cache time exceeds the set time to live (TTL) or the number of cached messages has reached 20, the message is discarded and an error code is reported to the app. To prevent cached and resent messages from being overwritten, ensure that messageId is unique. The cached messages may be resent in a random sequence if you do not specify any sequence.

The Push Kit client supports the message receipt. That is, if an uplink message is successfully sent by your app to your app server and the server responds to the message, a receipt will be sent to your app through the onMessageDelivered() method of the HmsMessageService class. To enable the message receipt, use the setReceiptMode(int receiptMode) method in RemoteMessage.Builder to set receiptMode. If this parameter is set to 1, the message receipt is enabled. (By default, the message receipt is disabled).

Notice:

To support the message cache and resending as well as the message receipt, the HMS Core (APK) version must be 4.0.4 or later, and the HMS Core Push APK version must be 10.1.1.400 or later. If the preceding functions are not required, the HMS Core (APK) version must be 3.0.0 or later for devices running EMUI 10.0 or later, and must be 4.0.3 or later for devices running EMUI earlier than 10.0.

For details about how to connect your app server to the Push Kit server, please refer to Server Development Guide. The following figure shows the process of sending uplink messages.

Sending uplink messages is developed as follows:

1. Enable the uplink message receiving through HTTPS in AppGallery Connect.

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

    b. Find your app project, and click the app for which the uplink message receiving needs to be enabled.

    c. Go to Growing>Push Kit. On the Configuration tab page, find Receive uplink message and click Enable.

    d. Set Destination URL, and HTTPS certificate (in PEM format).

2. Your app carries a specified URI to call the HmsMessaging.send() method to send an uplink message.

  1. String messageId = String.valueOf(System.currentTimeMillis());
  2. RemoteMessage remoteMessage = new RemoteMessage.Builder("push.hcm.upstream")
  3.        .setMessageId(messageId)
  4.        .addData("key1", "data1")
  5.        .addData("key2", "data2")
  6.        .build();
  7. try {
  8.    HmsMessaging.getInstance(this).send(remoteMessage);
  9.    Log.i(TAG, "sending...");
  10. } catch (Exception e) {
  11.    Log.e(TAG, "send exception:" + e);
  12. }

3. Your app processes the uplink message sending result.

Your app inherits the HmsMessageService class and overrides the onMessageSent and onSendError methods to check the uplink message is sent successfully. If the message fails to be sent, onSendError is called to return a BaseException object with an error code. You can call the getErrorCode method to obtain the error code. For example, if the size of the message body exceeds the maximum (1 KB), error code 907122041 will be returned.

  1. public class DemoHmsMessageService extends HmsMessageService {
  2.    @Override
  3.    public void onMessageSent(String msgId) {
  4.        Log.i(TAG,"message sent ok");
  5.    }
  6.    @Override
  7.    public void onSendError(String msgId, Exception exception) {
  8.        Log.e(TAG,"message sent err, msgid " + msgId + ", exception " + exception.getMessage());
  9.    }
  10. }

4. Your app server receives and verifies the uplink message.

When receiving the uplink message, the Push Kit server combines the receiving timestamp and a colon (:) with the uplink message into a character array (for example, 123456789:your_data) to be encrypted. The character array is encrypted in HMAC-SHA256 mode using the HMAC signature verification key, and then encoded in Base64 mode to generate a signature. The signature is passed to your app server using the X-HW-SIGNATURE field in the HTTPS request header. In the same request header, the uplink message receiving timestamp is passed to your app server using the X-HW-TIMESTAMP field. Your app server needs to use the HTTPS request header and HMAC signature key to verify the validity of the uplink message.

5. Your app server sends an uplink message response. For details, please refer to Sending Uplink Message Through HTTPS.