FirebaseMessagingService :如果应用不是 运行,intent.putExtra() 将不起作用

FirebaseMessagingService : intent.putExtra() does not work if app is not running

EDIT3: 好吧,我发现了问题:如果应用程序被杀死,同时发送 data notification 不会触发 onMessageReceived。如果目标设备为 android.

,则 notification 需要设置为 null

这真是愚蠢的行为。

原文post:

当应用程序启动时,我可以从 notificationBuilder 传递给 activity 的 Intent 中成功检索包。

但是,如果我终止该应用程序,通知仍然有效,但意图中的 GetExtras() 为空。

在我的FirebaseMessagingServicechildclass:

@Override
public void onMessageReceived(RemoteMessage remoteMessage)
{
     Intent intent  = new Intent(this, MainActivity.class);        
     intent.putExtra("key_1","value");
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    //also tried with  PendingIntent.FLAG_CANCEL_CURRENT


     Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("title")
            .setContentText("messageBody")
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, notificationBuilder.build());

}

当应用程序被杀死时,firebaseMessagingService 甚至无法写入文件。它不会崩溃,它会构建并显示通知,但对磁盘所做的任何操作都不起作用。

编辑: 我已经将 onMessageReceived 方法的内容替换如下:

@Override
public void onMessageReceived(RemoteMessage remoteMessage)
{
     // Do nothing
}

当应用程序 运行 没有任何反应 - 正如预期的那样 - 但当应用程序被终止时会显示一条通知,其中名称应用程序作为标题,remoteMessage.getNotification().getBody() 作为内容文本。

看来onMessageReceived没有被解雇。相反,另一个神奇的功能被称为...

EDIT2: 根据要求,这是从服务器发送的内容:

{
    "data":
     {
          "form_id":"33882606580812",
          "recipientUserId":10500
     }
     ,
     "to":"e0bU2jIVO9g:APA91bHsS-s3G1gQLcTFtRUC77pJUWcZvD7h9NfUgFLD-bFam1S0dddngVcmrQlXR5i6DfTsc69T8JbIrSuzyF1iv0c4ac1gmkwvGVMwZo_yA4KwOh82Nx-weYeL79r79si4qH3QBEgs",
     "notification":
     {
          "body":"you have a new notification",
          "badge":"1",
          "sound":"default"
     },
     "delay_while_idle":false
}

第二次编辑后,我们可以看到您向 FCM 发送了 notificationdata 负载。如果您检查 FCM Documentation,您可以看到应用程序在前台或后台的行为方式。 (让您的应用程序在后台被杀死与 运行 相同。)

[Exception 2] Messages with both notification and data payload, both background and foreground. In this case, the notification is delivered to the device’s system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.

这意味着您的 onMessageReceived 方法永远不会在此状态下调用。 FCM 服务在内部创建一个通知,启动您的主要 activity 并自动传递意图包 中的 data 有效负载

您应该正确处理这种情况,或者仅使用 data 有效负载来选择退出内置行为。

我对你的问题的第一个回答我最终删除了,因为我们都觉得在你的情况下 onMessageReceived() 被调用了。但事实就是如此

onMessageReceived()方法不会被调用如果应用程序在后台或被杀死,并且如果发送的消息同时包含数据和通知负载。

当应用程序不是 运行 时,您无论如何都会收到通知。但是,如果您想通过 onMessageReceived() 拦截数据,那么您将必须创建一个自定义应用程序服务器,该服务器将仅向 fcm 端点发送数据有效载荷

像这样:

    {
   "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
   "data" : {
     "Title" : "Title for Notification",
     "body" : "Notification body can go here",
     "Room" : "Something extra"
   },
 }

希望这能解决您的问题。

编辑: 我刚刚意识到,如果你想发送 通知和数据有效载荷两者 当应用程序被杀死并在后台时 启动器activity用户点击通知时,意图可以被拦截。只需这样做:

Intent fcmIntent = getIntent();

但这仅适用于通知点击启动的启动器 Activity。 你可以简单地把它放在onCreate()方法中。