adenizc
adenizc

Reputation: 421

HmsMessageService onMessageReceived not called

I implemented Huawei Push Kit. onNewToken is called after app started. I want to use onMessageReceived.

When I send push notification to client, push notification appears on android but onMessageReceived is not triggered.

I added below codes to Android Manifest

    <service
        android:name=".HmsMessageSrv"
        android:enabled="true" android:exported="false">
        <intent-filter>
            <action android:name="com.huawei.push.action.MESSAGING_EVENT" />
        </intent-filter>
    </service>

    <meta-data
        android:name="push_kit_auto_init_enabled"
        android:value="true" />

My Simple Class is;

public class HmsMessageSrv extends HmsMessageService {

    @Override
    public void onNewToken(String s) {
        super.onNewToken(s);
        Log.i("HmsMessageService", s);
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        Log.i("HmsMessageService", "onMessageReceived");
    }
}

How can it be triggered onMessageReceived function when push notification is received.

Upvotes: 8

Views: 2966

Answers (3)

Wooz12345
Wooz12345

Reputation: 179

You can get notification message from push notification while your app is running in background or is killed. In order to do this you need to set foreground_show as false in notification body.

If this value is true or is not set, your message is displayed by NC. If this value is false, your message is transmitted to the your app instead of displaying by NC.

For more details please refer: https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides-V5/android-fgrd-show-0000001050040126-V5

Upvotes: 1

zhangxaochen
zhangxaochen

Reputation: 34017

A notification message is directly displayed, and therefore the onMessageReceived method will not be called.

The method is called only when a data message is received. For details, please refer to Receiving Data Messages. However, a data message cannot reach your app after the process of your app is killed. To display a notification message after the process of your app is killed, or receive a data message through onMessageReceived when your app is running, please refer to Notification Message Display on UI.

Upvotes: 0

Abdurrahim Cillioglu
Abdurrahim Cillioglu

Reputation: 331

onMessageReceived function is only used to receive data message. So, push notifications don't trigger the onMessageRecevied. You can use the data message and create notification yourself. You can access the sample code from this link.

Upvotes: 6

Related Questions