FirebaseMessagingService:如果应用未运行,则intent.putExtra()不起作用 [英] FirebaseMessagingService : intent.putExtra() does not work if app is not running

查看:271
本文介绍了FirebaseMessagingService:如果应用未运行,则intent.putExtra()不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我发现了问题:如果应用程序被杀死,同时发送data notification都不会触发onMessageReceived.如果目标设备是android,则notification需要设置为null.

Ok I found the problem : If the app is killed, sending both data and notification won't trigger onMessageReceived. notification needs to be set to null if the target device is android.

那真是愚蠢的行为.

原始帖子:

启动应用程序后,我可以成功地从notificationBuilder传递给活动的意图中检索捆绑软件.

I can successfully retrieve the bundle from the intent passed by the notificationBuilder to the activity when the app is launched.

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

However, if I kill the app, the notification still works but the GetExtras() from the intent is 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甚至无法写入文件.它不会崩溃,它会生成并显示通知,但是对磁盘所做的任何操作均不起作用.

When the app is killed, the firebaseMessagingService can't even write a file. It does not crash, it builds, and shows the notification, but anything done to the disk does not work.

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

I have replaced the content of the onMessageReceived method as follows:

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

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

When the app is running nothing happens -as expected- but when the app is killed a notification is displayed whith the name app as a title and remoteMessage.getNotification().getBody() as content text.

似乎未触发onMessageReceived.取而代之的是另一个魔术函数...

It seems that onMessageReceived is not fired. Instead another magic function is called...

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

As requested, this is what is sent from the server:

{
    "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().但这确实是

onMessageReceived()方法将不会被调用,如果该应用程序处于后台或已终止,并且发送的消息同时包含DATA和NOTIFICATION有效负载.

当应用未运行时,您仍然会收到通知.但是,如果您想通过onMessageReceived()拦截数据,则必须创建一个自定义应用服务器,该服务器将仅将数据有效载荷发送到fcm端点.

When app is not running you will anyway receive notification. But if you want to intercept data via onMessageReceived() then you will have to create a custom app server which will send only DATA payload to fcm endpoint.

类似这样的东西:

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

希望这可以解决您的问题.

Hope this solves your issue.

编辑:我刚刚意识到,如果您想在应用程序被杀死时和在后台同时发送通知和数据有效载荷,那么意图就可以被拦截时,发起人活动 中的strong>.只需这样做:

I just realized that if you want to send Notification and Data payload both while app is killed and in background then intent can be intercepted in launcher activity when user clicks on notification. By simply doing this:

Intent fcmIntent = getIntent();

但这仅在启动通知单击的启动器活动"中有效. 您可以简单地将其放在onCreate()方法中.

But this will work only in Launcher Activity which the notification click is launching. You can simply put it in onCreate() method.

这篇关于FirebaseMessagingService:如果应用未运行,则intent.putExtra()不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆