Android 通知:onMessageReceived 未调用

Que*_*ner 5 android push-notification firebase firebase-cloud-messaging

首先,这并不是因为应用程序在后台。

通知通过数据消息有效负载发送。在 Play 控制台中,它显示消息已“已确认”,因此它们正在到达设备。对于大多数用户来说,onMessageReceived会调用该方法,但对于少数用户来说,不会调用该方法。为什么会这样呢?

Android 清单:

<service android:name=".push.MyFirebaseInstanceIDService">
    <intent-filter>
        <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
    </intent-filter>
</service>
<service android:name=".push.MyFirebaseMessagingService">
    <intent-filter>
         <action android:name="com.google.firebase.MESSAGING_EVENT"/>
    </intent-filter>
</service>
Run Code Online (Sandbox Code Playgroud)

我的Firebase消息服务:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

...
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
...
}
Run Code Online (Sandbox Code Playgroud)

Mic*_*rla 3

发生这种情况的主要原因之一是特定用户更改了其登录详细信息或在使用应用程序时使用了不同的帐户。在这种特定情况下,特定设备令牌将接收通知,但由于他使用不同的帐户,因此不会显示通知。解决此问题的一种方法是,每次用户注销时清除用户的设备令牌,并在用户再次登录时保存新创建的设备令牌。这样,设备令牌就会保持更新,并且用户将收到通知。