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
GuidesPush KitAndroidBasic CapabilitiesObtaining Message Content

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.

Collapse
Word wrap
Dark theme
Copy code
  1. <manifest ...>
  2. ...
  3. <application ...>
  4. <service android:name=".DemoHmsMessageService" android:exported="false">
  5. <intent-filter>
  6. <action android:name="com.huawei.push.action.MESSAGING_EVENT"/>
  7. </intent-filter>
  8. </service>
  9. </application>
  10. ...
  11. <queries>
  12. <intent>
  13. <action android:name="com.huawei.hms.core.aidlservice" />
  14. </intent>
  15. </queries>
  16. ...
  17. </manifest>
NOTE
  • 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:

  • If the parameter is set to false, the notification message will be transmitted to your app through the onMessageReceived(RemoteMessage message) method, instead of being displayed through the NC when your app is running in the foreground. The app will then obtain the message content using the methods provided by the RemoteMessage class. In this case, Push Kit does not verify the validity of the message parameters.
  • If this parameter is set to true or left empty, the notification message is displayed in the NC when your app is running in the foreground, and its message content cannot be obtained.
NOTE
  • 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:

Collapse
Word wrap
Dark theme
Copy code
  1. {
  2. "message": {
  3. "notification": {
  4. "title": "message title",
  5. "body": "message body"
  6. },
  7. "android": {
  8. "notification": {
  9. "foreground_show": false,
  10. "click_action": {
  11. "type": 3
  12. }
  13. }
  14. },
  15. "token": ["pushtoken1"]
  16. }
  17. }

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
Kotlin
Collapse
Word wrap
Dark theme
Copy code
  1. @Override
  2. public void onMessageReceived(RemoteMessage message) {
  3. Log.i(TAG, "onMessageReceived is called");
  4. // Check whether the message is empty.
  5. if (message == null) {
  6. Log.e(TAG, "Received message entity is null!");
  7. return;
  8. }
  9. // Obtain the message content.
  10. Log.i(TAG, "get Data: " + message.getData()
  11. + "\n getFrom: " + message.getFrom()
  12. + "\n getTo: " + message.getTo()
  13. + "\n getMessageId: " + message.getMessageId()
  14. + "\n getSentTime: " + message.getSentTime()
  15. + "\n getDataMap: " + message.getDataOfMap()
  16. + "\n getMessageType: " + message.getMessageType()
  17. + "\n getTtl: " + message.getTtl()
  18. + "\n getToken: " + message.getToken());
  19. Boolean judgeWhetherIn10s = false;
  20. // If the message is not processed within 10 seconds, create a job to process it.
  21. if (judgeWhetherIn10s) {
  22. startWorkManagerJob(message);
  23. } else {
  24. // Process the message within 10 seconds.
  25. processWithin10s(message);
  26. }
  27. }
  28. private void startWorkManagerJob(RemoteMessage message) {
  29. Log.d(TAG, "Start new job processing.");
  30. }
  31. private void processWithin10s(RemoteMessage message) {
  32. Log.d(TAG, "Processing now.");
  33. }
Collapse
Word wrap
Dark theme
Copy code
  1. override fun onMessageReceived(message: RemoteMessage?) {
  2. Log.i(TAG, "onMessageReceived is called")
  3. // Check whether the message is empty.
  4. if (message == null)
  5. {
  6. Log.e(TAG, "Received message entity is null!")
  7. return
  8. }
  9. // Obtain the message content.
  10. Log.i(TAG, """getData: ${message.data}
  11. getFrom: ${message.from}
  12. getTo: ${message.to}
  13. getMessageId: ${message.messageId}
  14. getSentTime: ${message.sentTime}
  15. getDataMap: ${message.dataOfMap}
  16. getMessageType: ${message.messageType}
  17. getTtl: ${message.ttl}
  18. getToken: ${message.token}""".trimIndent())
  19. val judgeWhetherIn10s = false
  20. // If the message is not processed within 10 seconds, create a job to process it.
  21. if (judgeWhetherIn10s) {
  22. startWorkManagerJob(message)
  23. } else {
  24. // Process the message within 10 seconds.
  25. processWithin10s(message)
  26. }
  27. }
  28. private fun startWorkManagerJob(message: RemoteMessage?) {
  29. Log.d(TAG, "Start new Job processing.")
  30. }
  31. private fun processWithin10s(message: RemoteMessage?) {
  32. Log.d(TAG, "Processing now.")
  33. }
Search in Guides
Enter a keyword.