Akl*_*kli 5 android android-intent firebase firebase-notifications
EDIT3:好的,我发现了问题:如果应用程序被杀死,则同时发送data 和 发送都notification不会触发onMessageReceived。notification如果目标设备是android,则需要将其设置为null。
这确实是一种愚蠢的行为。
原始帖子:
notificationBuilder启动应用程序后,我可以成功地从传递给活动的意图中检索捆绑包。
但是,如果我终止了该应用程序,则通知仍然有效,但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());
}
Run Code Online (Sandbox Code Playgroud)
当应用被杀死时,firebaseMessagingService甚至无法写入文件。它不会崩溃,它会生成并显示通知,但是对磁盘所做的任何操作均不起作用。
编辑:我已替换onMessageReceived方法的内容,如下所示:
@Override
public void onMessageReceived(RemoteMessage remoteMessage)
{
// Do nothing
}
Run Code Online (Sandbox Code Playgroud)
当应用程序运行时,没有发生任何事情(如预期的那样),但是当该应用程序被终止时,会显示一条通知,其中名称为标题和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
}
Run Code Online (Sandbox Code Playgroud)
我对你的问题的第一个回答我最终删除了,因为我们都觉得在你的情况下调用了 onMessageReceived() 。但这就是它的真实情况
\n\n如果应用程序处于后台或被终止,并且发送的消息同时包含数据和通知负载,则onMessageReceived() \xc2\xa0method\xc2\xa0 将不会被调用。
\n\n当应用程序未运行时,您无论如何都会收到通知。但是,如果您想通过 onMessageReceived()\xc2\xa0 拦截数据,那么您必须创建一个自定义应用程序服务器,该服务器将 \xc2\xa0仅将 \xc2\xa0DATA\xc2\xa0payload 发送到 fcm 端点。
\n\n像这样的东西:
\n\n {\n "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",\n "data" : {\n "Title" : "Title for Notification",\n "body" : "Notification body can go here",\n "Room" : "Something extra"\n },\n }\nRun Code Online (Sandbox Code Playgroud)\n\n希望这能解决您的问题。
\n\n编辑:我刚刚意识到,如果您想在应用程序被终止时和在后台发送通知和数据有效负载,那么当用户单击通知时,可以在启动器活动中拦截意图。只需这样做:
\n\nIntent fcmIntent = getIntent();\nRun Code Online (Sandbox Code Playgroud)\n\n但这仅适用于通知单击正在启动的启动器活动。您只需将其放在 onCreate() 方法中即可。
\n