目录

Obtaining Message Content

Scenario Description

To obtain notification or data message content, you need to implement the onMessageReceived method. After the message is received, it is your app that processes the message.

Register your service under application in the AndroidManifest.xml file. This service inherits from the HmsMessageService class and implements the methods in the class. The following uses the service with the DemoHmsMessageService class as an example. You can customize the class name as needed.

<manifest ...>
    ...
    <application ...>
        <service android:name=".DemoHmsMessageService" android:exported="false">
            <intent-filter>
                <action android:name="com.huawei.push.action.MESSAGING_EVENT"/>
            </intent-filter>
        </service>
    </application>
    ...
    <queries>
        <intent>
            <action android:name="com.huawei.hms.core.aidlservice" />
        </intent>
    </queries>
    ...
</manifest>
  • The exported attribute must be set to false to prevent components of other apps from enabling the service.
  • Since Push SDK 6.1.0.300, the <queries> element is preset. In this case, you are advised to integrate the Push SDK of the latest version.
  • In Android 11, the way for an app to query other apps on the user device and interact with them has been changed. If targetSdkVersion is 30 or later for your app, you need to add the <queries> element to manifest in the AndroidManifest.xml file, so that your app can access HMS Core (APK). The <queries> element requires the following:
    • Your Android Studio version is 3.6.1 or later.
    • Your Android Gradle plugin version is 3.5.4 or later.

Obtaining Notification Message Content

When your app is running in the foreground, you need to set the message.android.notification.foreground_show parameter to false, and use the REST API to send a notification message. Then implement the onMessageReceived method to obtain the message content.

The parameter is described as follows:

  • This function is supported on devices that run EMUI 9.1.0 or later and have apps integrated with Push SDK 4.0 or later.
  • What the app obtains actually is the notification message ID through getMessageId from the data obtained by onMessageReceived.

Sample code:

{
    "message": {
        "notification": {
            "title": "message title",
            "body": "message body"
        },
        "android": {
            "notification": {
                "foreground_show": false,
                "click_action": {
                    "type": 3
                }
            }
        },
        "token": ["pushtoken1"]
    }
}

Obtaining Data Message Content

Regardless of whether your app is running in the foreground or background, if you override the onMessageReceived method in the DemoHmsMessageService class, your app can obtain the data message content as long as you send a data message.

Java
@Override
public void onMessageReceived(RemoteMessage message) {
    Log.i(TAG, "onMessageReceived is called");

    // Check whether the message is empty.
    if (message == null) {
        Log.e(TAG, "Received message entity is null!");
        return;
    }

    // Obtain the message content.
    Log.i(TAG, "get Data: " + message.getData()
            + "\n getFrom: " + message.getFrom()
            + "\n getTo: " + message.getTo()
            + "\n getMessageId: " + message.getMessageId()
            + "\n getSentTime: " + message.getSentTime()
            + "\n getDataMap: " + message.getDataOfMap()
            + "\n getMessageType: " + message.getMessageType()
            + "\n getTtl: " + message.getTtl()
            + "\n getToken: " + message.getToken());

    Boolean judgeWhetherIn10s = false;
    // If the message is not processed within 10 seconds, create a job to process it.
    if (judgeWhetherIn10s) {
        startWorkManagerJob(message);
    } else {
        // Process the message within 10 seconds.
        processWithin10s(message);
    }
}
private void startWorkManagerJob(RemoteMessage message) {
    Log.d(TAG, "Start new job processing.");
}
private void processWithin10s(RemoteMessage message) {
    Log.d(TAG, "Processing now.");
}
Kotlin
override fun onMessageReceived(message: RemoteMessage?) {
    Log.i(TAG, "onMessageReceived is called")
    
        // Check whether the message is empty.
    if (message == null)
    {        
        Log.e(TAG, "Received message entity is null!")
        return    
    }    
    
        // Obtain the message content.
    Log.i(TAG, """getData: ${message.data}        
        getFrom: ${message.from}        
        getTo: ${message.to}        
        getMessageId: ${message.messageId}
        getSentTime: ${message.sentTime}           
        getDataMap: ${message.dataOfMap}
        getMessageType: ${message.messageType}   
        getTtl: ${message.ttl}        
        getToken: ${message.token}""".trimIndent())

    val judgeWhetherIn10s = false   
    // If the message is not processed within 10 seconds, create a job to process it.
    if (judgeWhetherIn10s) {        
        startWorkManagerJob(message)    
    } else {     
        // Process the message within 10 seconds.               
        processWithin10s(message)    
    }
}
private fun startWorkManagerJob(message: RemoteMessage?) {    
    Log.d(TAG, "Start new Job processing.")
}
private fun processWithin10s(message: RemoteMessage?) {    
    Log.d(TAG, "Processing now.")
}