问题
I am currently writing an Android app using Flutter and a Node.js backend.
On the client side, I followed the first 3 steps of the firebase_messaging docs to integrate FCM into my app. The logic of my app subscribes to und unsubscribes from several topics (one user is subscribed to 12 topics on average) based on different settings.
The server logic is supposed to send various notification messages based on conditions:
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))
}
new Promise((resolve, reject) => {
return firebase.messaging().sendAll(notifications).then(resp => {
console.log(`Sent ${resp.successCount} messages, ${resp.failureCount} failed`)
resolve()
})
}).then(() => {
firebase.app().delete()
}).catch(err => {
console.error(err)
})
function buildMessage(title, body, condition) => {
return message = {
notification: { title, body },
android: {
ttl: 24 * 60 * 60 * 1000, // 1d
notification: {
sound: 'default',
vibrate_timings: [
'0s', '0.1s',
]
}
},
condition
}
}
When I run this code, it logs Sent 72 messages, 0 failed
. Therefore, I assume that the sending of the messages worked. The number of messages sent and the corresponding topics as well as the title change at least daily. Based on my topic subscriptions I should actually receive about 4 push notifications on my phone. However, I only receive exactly one notification at a time. After I had reinstalled the app and therefore subscribed to the topics again with a new token, I received all the messages I was supposed to receive. After a few days, however, it has changed back to the old behavior and I receive exactly one message every time the server sends a batch of messages.
回答1:
With the help of BigQuery I found out where the problem lies: All messages have an identical collapse_key
(the app name). Thus, if I can't receive any messages on my device at the time the server sends the messages (for example, when I'm not connected to the Internet), the messages are collapsed and I only get one message (the last message sent). This behavior seems to be intented for notification messages:
Non-collapsible and collapsible messages (FCM Docs):
Except for notification messages, all messages are non-collapsible by default.
Apparently there is no straightforward solution to this problem. FCM only supports four different collapse_key
s at a time, so I can't just give each message a different collapse_key
, since then I'd only get four messages at most.
I probably have to use a workaround by sending data messages and then creating the proper push notifications on the client side.
Related: How to make a GCM / FCM notification-type message non-collapsible
来源:https://stackoverflow.com/questions/59765657/fcm-device-receives-only-one-of-multiple-notification-messages