We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All

Documents from this version have been archived, and will not continue to be maintained. Please use the latest version.

HMS Core ReferencesPush KitAndroid Client APIs (Archived)com.huawei.hms.push (Archived)HmsMessageService (Archived)

HmsMessageService

CLASS info
public class HmsMessageService extends Service

Basic class for receiving downlink messages or updated tokens in HUAWEI Push Kit.

To receive messages pushed by the Push Kit server or updated tokens, you need to declare the implementation class of HmsMessageService in the AndroidManifest.xml file of the app. In the implementation class, you need to override all methods in the base class to process the data returned by the HMS Core Push SDK. You ensure that all callbacks in HmsMessageService must complete processing within 10 seconds. If the processing time exceeds 10 seconds, you need to create a job for processing.

The AndroidManifest.xml file must contain the following information:

Collapse
Word wrap
Dark theme
Copy code
  1. <service
  2.      android:name=".YourHmsMessageService"
  3.      android:exported="false">
  4.      <intent-filter>
  5.            <action android:name="com.huawei.push.action.MESSAGING_EVENT" />
  6.      </intent-filter>
  7. </service>


Public Method Summary

Return typeMethod name
voidonMessageReceived(RemoteMessage message)
Receives data messages.
void

onMessageSent(String msgId)

Called after an uplink message is successfully sent.

void

onSendError(String msgId, Exception exception)

Called after an uplink message fails to be sent.

voidonNewToken(String token)

Called after the Push Kit server updates the token.

voidonTokenError(Exception exception)

Called when a token fails to be applied for.

voidonMessageDelivered(String msgId, Exception exception)

Sends the response from the app server to the app after an uplink message reaches the app server if the receipt is enabled.

Public Methods

Method
public void onMessageReceived(RemoteMessage message)

Receives data messages pushed by the app server.

Parameters

Parameter nameParameter desc
messageData message.
Method
public void onMessageSent(String msgId)
Called after an uplink message is successfully sent. After the app calls the send(RemoteMessage message) method in HmsMessaging to send an uplink message, the app class extends the HmsMessageService class and overrides the onMessageSent(String msgId) method to receive the sending result. If the onMessageSent(String msgId) method is called, the message is sent successfully.

Parameters

Parameter nameParameter desc

msgId

Message ID.

Method
public void onSendError(String msgId, Exception exception)

Called after an uplink message fails to be sent. After the app calls the send(RemoteMessage message) method in HmsMessaging to send an uplink message, the app class extends the HmsMessageService class and overrides the onSendError(String msgId, Exception exception) method to receive the sending result.

Parameters

Parameter nameParameter desc

msgId

Message ID.

exception

Error code obtained by getErrorCode() in BaseException and the description obtained by getMessage().

Method
public void onNewToken(String token)
When an app calls the getToken method to apply for a token from the server, if the server does not return the token during current method calling, the server can return the token through this method later. The main scenarios are as follows:
1. If the getToken method fails to be called, HUAWEI Push Kit automatically calls the method again, and the requested token is returned through the onNewToken method.
2. If the server determines that the requested token has expired, the updated token will be returned through the onNewToken method.
3. If the EMUI version of a Huawei device is earlier than 10.0, the token is returned through the onNewToken method.

Parameters

Parameter nameParameter desc
tokenToken applied for by an app from the HUAWEI Push Kit server through the getToken method.
Method
public void onTokenError(Exception exception)
Callback method used when a token fails to be applied for.

Parameters

Parameter nameParameter desc
exceptionException information returned when a token fails to be applied for.
Method
public void onMessageDelivered(String msgId, Exception exception)

Sends the response from the app server to the app after an uplink message reaches the app server if the receipt is enabled.

Parameters

Parameter nameParameter desc

msgId

ID of a sent message.

exception

Exception information contained in a receipt.

This page may contain third-party content. For details, click here.
Search in References
Enter a keyword.