FCM:设备仅收到多条通知消息中的一条

FCM: Device receives only one of multiple notification messages

我目前正在使用 Flutter 和 Node.js 后端编写 Android 应用程序。

在客户端,我根据不同的主题(一个用户平均订阅了 12 个主题) 按照 firebase_messaging docs to integrate FCM into my app. The logic of my app subscribes to und unsubscribes 的前 3 个步骤设置。

服务器逻辑应该根据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
  }
}

当我 运行 这段代码时,它会记录 Sent 72 messages, 0 failed。因此,我假设消息的发送是有效的。发送的消息数量和相应的主题以及标题至少每天更改一次。 根据我的主题订阅,我实际上应该在 phone 上收到大约 4 个推送通知。然而,我一次只收到一个通知。 在我重新安装应用程序并因此使用新令牌再次订阅主题后,我收到了所有我应该收到的消息。然而,几天后,它又变回了原来的行为,每次服务器发送一批消息时,我都会收到一条消息。

在 BigQuery 的帮助下,我找到了问题所在:所有消息都有相同的 collapse_key(应用程序名称)。因此,如果在服务器发送消息时我无法在我的设备上收到任何消息(例如,当我未连接到 Internet 时),消息将被折叠并且我只会收到一条消息(最后一条消息发送)。此行为似乎是为通知消息而设计的:

Non-collapsible and collapsible messages (FCM Docs):

Except for notification messages, all messages are non-collapsible by default.

显然这个问题没有直接的解决方案。 FCM一次只支持四个不同的collapse_key,所以我不能给每条消息一个不同的collapse_key,从那以后我最多只能收到四个消息。

我可能不得不通过发送数据消息然后在客户端创建适当的推送通知来使用变通方法。


相关: