Intelligent Assistant
Chat with our virtual assistant to get answers promptly.
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.
HMS Core
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>
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:
Sample code:
- {
- "message": {
- "notification": {
- "title": "message title",
- "body": "message body"
- },
- "android": {
- "notification": {
- "foreground_show": false,
- "click_action": {
- "type": 3
- }
- }
- },
- "token": ["pushtoken1"]
- }
- }
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.
- @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.");
- }
- 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.")
- }
Intelligent Assistant
Chat with our virtual assistant to get answers promptly.
Quick start
Helps you find desired resources with ease.