Firebase onMessageReceived未在后台应用中调用 [英] Firebase onMessageReceived not called when app in background

查看:1049
本文介绍了Firebase onMessageReceived未在后台应用中调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firebase,并在应用程序处于后台时从我的服务器向我的应用程序发送通知。通知发送成功,它甚至出现在设备的通知中心,但是当通知出现时,或者即使点击它,我的FCMessagingService中的onMessageReceived方法也不会被调用。

当我测试这个时,我的应用程序在前台,onMessageReceived方法被调用,一切工作正常。当应用程序在后台运行时,会发生问题。



这是预期的行为,还是有办法解决这个问题?

这是我的FBMessagingService:
$ b $ pre $ import $ android.util.Log;

导入com.google.firebase.messaging.FirebaseMessagingService;
导入com.google.firebase.messaging.RemoteMessage;

public class FBMessagingService extends FirebaseMessagingService {
$ b @Override
public void onMessageReceived(RemoteMessage remoteMessage){
Log.i(PVL,MESSAGE RECEIVED !!);
if(remoteMessage.getNotification()。getBody()!= null){
Log.i(PVL,RECEIVED MESSAGE:+ remoteMessage.getNotification()。getBody());
} else {
Log.i(PVL,RECEIVED MESSAGE:+ remoteMessage.getData()。get(message));




$ div class =h2_lin>解决方案

这是按预期工作,只有当你的应用程序在前台时,通知消息才会传递给你的onMessageReceived回调。如果您的应用程序处于后台或关闭状态,则会在通知中心显示通知消息,并且来自该消息的所有数据都将传递给由于用户轻击而启动的意图通知。

您可以指定一个click_action来指示当用户点击通知时应该启动的意图。如果没有指定click_action,则使用主要活动。



当意图启动时,您可以使用

  getIntent()getExtras(); 

检索一个包含与通知消息一起发送的任何数据的Set。



有关通知邮件的更多信息,请参阅文档

I'm working with Firebase and testing sending notifications to my app from my server while the app is in the background. The notification is sent successfully, it even appears on the notification centre of the device, but when the notification appears or even if I click on it, the onMessageReceived method inside my FCMessagingService is never called.

When I tested this while my app was in the foreground, the onMessageReceived method was called and everything worked fine. The problem occurs when the app is running in the background.

Is this intended behaviour, or is there a way I can fix this?

Here is my FBMessagingService:

import android.util.Log;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

public class FBMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.i("PVL", "MESSAGE RECEIVED!!");
        if (remoteMessage.getNotification().getBody() != null) {
            Log.i("PVL", "RECEIVED MESSAGE: " + remoteMessage.getNotification().getBody());
        } else {
            Log.i("PVL", "RECEIVED MESSAGE: " + remoteMessage.getData().get("message"));
        }
    }
}

解决方案

This is working as intended, notification messages are delivered to your onMessageReceived callback only when your app is in the foreground. If your app is in the background or closed then a notification message is shown in the notification center, and any data from that message is passed to the intent that is launched as a result of the user tapping on the notification.

You can specify a click_action to indicate the intent that should be launched when the notification is tapped by the user. The main activity is used if no click_action is specified.

When the intent is launched you can use the

getIntent().getExtras();

to retrieve a Set that would include any data sent along with the notification message.

For more on notification message see docs.

这篇关于Firebase onMessageReceived未在后台应用中调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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