编辑3:好的我发现问题:如果应用程序被杀死,发送数据和通知都不会触发onMessageReceived.如果目标设备是
android,则通知需要设置为null.
这真是一种愚蠢的行为.
原帖:
我可以在启动应用程序时成功从notifyBuilder传递给活动的意图中检索捆绑包.
但是,如果我终止该应用程序,通知仍然有效,但intent中的GetExtras()为null.
在我的FirebaseMessagingService子类中:
@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
}
最佳答案 我的第一个回答是你最后删除的问题,因为我们都觉得在你的情况下调用了onMessageReceived().但这就是它的真实含义
如果应用程序处于后台或被杀死,并且发送的消息包含DATA和NOTIFICATION有效负载,则不会调用onMessageReceived()方法.
当应用程序未运行时,您仍会收到通知.但是如果你想通过onMessageReceived()拦截数据,那么你将不得不创建一个自定义应用服务器,它只将DATA有效负载发送到fcm端点.
这样的事情:
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data" : {
"Title" : "Title for Notification",
"body" : "Notification body can go here",
"Room" : "Something extra"
},
}
希望这能解决您的问题.
编辑:我刚刚意识到,如果您想要在应用程序被终止时在后台发送通知和数据有效负载,那么当用户点击通知时,可以在启动器活动中拦截意图.只需这样做:
Intent fcmIntent = getIntent();
但这仅适用于启动通知单击启动的Launcher活动.你可以简单地把它放在onCreate()方法中.