我正在使用FCM(firebase云消息传递)向IOS应用程序发送"自定义"数据通知.据我所知,当您希望FCM处理代表您的应用程序显示通知时,我们会使用通知消息.当您只想在应用中处理消息时,我们会使用数据消息.这是设计的.
我面临的问题是,Device/InstandID令牌对于已安装的应用程序是唯一的,而不是登录应用程序的用户.因此,要解决此问题,我将在数据中发送预期的用户标记,以便它成为数据消息.由于应用程序处理数据通知,因此只有在打开应用程序并且仅显示通知时才会触发didReceiveRemoteNotification()回调,而不会立即显示通知.
我的问题是,我是否可以发送自定义数据通知消息,即使应用程序关闭也会立即显示.
这是我发送给FCM的有效负载:
{
registeration_ids : [<id_1>, <id_2>],
data : {
title : message_title,
body : message_body,
intended_user : message_user
}
}
Run Code Online (Sandbox Code Playgroud)
在Android FirebaseMessagingService.onMessageReceived()中调用即使应用程序在后台但在ios didReceiveRemoteNotification()中仅在启动应用程序时调用,因此如果发送数据消息则不会显示后台消息.
我目前正在使用 Flutter 和 Node.js 后端编写 Android 应用程序。
在客户端,我按照firebase_messaging 文档的前 3 个步骤将 FCM 集成到我的应用程序中。我的应用程序的逻辑根据不同的设置订阅和取消订阅多个主题(一名用户平均订阅 12 个主题) 。
服务器逻辑应该根据条件发送各种通知消息:
const firebase = require('firebase-admin')
firebase.initializeApp({
credential: firebase.credential.applicationDefault(),
databaseURL: 'https://<PROJECT>.firebaseio.com'
})
const title = ...
const msgList = [...]
const notifications = []
for (let msg of msgList) {
let condition = `'${msg.topicA}' in topics && '${msg.topicB}' in topics`
if (msg.topicX !== '') {
condition = `${condition} && !('${msg.topicX}' in topics)`
}
notifications.push(buildMessage(title, msg.body, condition)) …Run Code Online (Sandbox Code Playgroud)