【问题标题】:Is there any way how to get data from push notification using FirebaseMessagingService in background?有什么方法可以在后台使用 FirebaseMessagingService 从推送通知中获取数据?
【发布时间】:2019-12-22 17:37:52
【问题描述】:

我正在使用FirebaseMessagingService 进行推送通知。它在应用程序中按预期工作。 onMessageReceived 被调用,我可以获得通知标题和正文 + 数据负载,并使用LocalBroadcastManager 将取决于负载数据的响应发送到我的Activity。

但是后台推送通知有问题。这只会在后台显示通知,但不会在onMessageReceived 中创建,因为如果应用程序在后台,则无法调用此函数。

根据文档,推送通知的数据部分存储在extras中,可以在Activity恢复时找到。我已经为此功能及其工作。但问题是我没有标题和消息,因为它没有发送到onMessageReceived,我无法捕捉到这些信息。

有什么方法可以获取吗?因为我需要在应用程序内的对话窗口中显示它。推送通知不仅仅是文本和标题,它不仅仅是用户的信息,它还会做一些动作。

在FirebaseMessagingService中接收通知标题、正文和有效负载:

override fun onMessageReceived(msg: RemoteMessage?) {
        val pNotification = msg?.notification
        val payload = msg?.data
        if (pNotification != null){
            val ntitle = pNotification.title
            val nMsg = pNotification.body
        }
        //working with payload - creating notification and Intent for LocalBroadcastmanager
}

从 onResume() 内部的额外内容中捕获有效负载

private fun maybeFindBackgroundPayload(extras: Bundle?){
        if (extras != null){
            extras.keySet().forEach { key->
                val keyValue = extras[key]
                App.log("BackgroundKeyValues: $keyValue")
                if (key.equals("gcm.notification.body", ignoreCase = true) && keyValue != null){
                    ...
                    //Working with payload data key value - dont have title and body
                }
            }
        }
    }

【问题讨论】:

  • 无法通过 firebase,因为当应用程序在后台时,firebase 会处理收到的通知并显示它,用户应该点击它,然后只有数据可以传递到您的应用程序。
  • 我认为你应该在数据负载中传递相同的信息......通知标题和正文不存储在附加文件中。

标签: android firebase firebase-cloud-messaging android-service android-push-notification


【解决方案1】:

不幸的是,它不可能按照您希望的方式工作。

如果推送通知在负载中包含通知部分并且您的应用程序在后台,则永远不会调用 onMessageReceived。通知由 firebase 自动创建和显示。没有办法拦截它并修改通知或为此执行任何其他操作。只有点击通知后,您的应用才会获得控制权。

当应用程序处于前台时,如您所述,将始终调用 onMessageReceived。

确保在所有情况下(前台、后台和应用被终止)都调用 onMessageReceived 的唯一方法是从 firebase 发送仅数据负载。也就是说,删除 firebase 有效负载正文的通知部分,并将内容(标题、正文等)放入有效负载的数据部分。并在您的 onMessageReceived 中将此标题、正文等分配给您正在创建的通知。

以下是来自 firebase 的有效负载的需要:

{
"registration_ids":[
  "firebase_token"
],
"data":{
    "body":"body of notification",
    "title":"title of notification",
    "otherdatakey":"otherdatavalue"
}
}

然后在您的 onMessageReceived 函数中,您需要显式提取标题和正文并将其分配给通知(当然还有您想要执行的任何其他操作):

@Override
public void onMessageReceived(RemoteMessage message) {
Map<String, String> dataMap = message.getData();
String title = dataMap.get("title");
String body = dataMap.get("body");
String otherdatavalue = dataMap.get("otherdatakey");
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setContentTitle(title)
            .setContentText(body)
            .setContentIntent(pendingIntent);//To handle click of notification

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-27
    • 2021-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多