activity 被杀死时未调用 MyFirebaseMessagingService

MyFirebaseMessagingService not called when activity is killed

当应用程序处于活动状态时,一切都正常 运行ning 但是当没有 activity 是 运行ning 时,我仍然会收到 app_name 的通知Body 通过 firebase 通知并打开 MainActivity。
这是我的 MyFirebaseMessagingService class:-
我看到了 similar question 但它对我的情况没有帮助。

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    if(remoteMessage.getNotification()!=null){
        if(DictionarySscWords.send_notifications) { //tried removing this condition but same result.
            sendnoti(remoteMessage.getNotification().getBody());
        }
    }
}

private void sendnoti(String body) {
   Intent i1=new Intent(this,MainActivity.class);
    i1.putExtra("word", body);
    i1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pi=PendingIntent.getActivity(this,0,i1,PendingIntent.FLAG_CANCEL_CURRENT);
    Uri ns=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder nb=new NotificationCompat.Builder(this).setSmallIcon(R.drawable.heart)
            .setContentTitle("Word Of the Day")
            .setContentText(body).setSmallIcon(R.drawable.heart)
            .setAutoCancel(true)
            .setSound(ns)
            .setContentIntent(pi);
    NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0,nb.build());
}

}

当应用程序处于活动状态时



when no activity is running

还尝试通过 AVD 运行 但没有错误。
我很困惑如何在不通过此 class 的情况下创建通知,好像这被称为不同的值将在此处填充。
谢谢

正如 Firebase Documentation 上所说:

Notification messages are delivered to the notification tray when the app is in the background. For foreground apps, messages are processed by these callbacks:

DidReceiveRemoteNotification: on iOS OnMessageReceived () on Android. The notification key in the data packet contains the notification.

如果您使用的是基于 message 的通知,那么只有当您的应用程序在前台时,您才会处理它们,否则,Firebase 会处理它们。

基于message的通知示例:

{
    "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
    "notification" : {
      "body" : "great match!",
      "title" : "Portugal vs. Denmark",
      "icon" : "myicon"
    }
  }

如果您想处理所有通知,独立于应用程序是在前台还是后台,您将需要开始发送基于 data 的推送通知。例如:

{
   "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
   "data" : {
     "Nick" : "Mario",
     "body" : "great match!",
     "Room" : "PortugalVSDenmark"
   },
 }