FCM:设备仅接收多个通知消息之一 [英] FCM: Device receives only one of multiple notification messages

查看:46
本文介绍了FCM:设备仅接收多个通知消息之一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用Flutter和Node.js后端编写一个Android应用.

I am currently writing an Android app using Flutter and a Node.js backend.

在客户端,我遵循了的前三个步骤.firebase_messaging文档将FCM集成到我的应用中.我的应用程序订阅的逻辑与取消订阅,来自多个主题(一个用户订阅了12个主题,平均).

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.

服务器逻辑应该基于条件:

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
  }
}

当我运行此代码时,它会记录已发送72条消息,其中0条失败.因此,我认为发送消息是可行的.发送的消息数和相应的主题以及标题至少每天更改一次.根据我的主题订阅,实际上我应该会在手机上收到大约4条推送通知.但是,我一次只收到一个通知.重新安装该应用程序并因此使用新令牌再次订阅主题后,我收到了本应收到的所有消息.但是几天后,它又变回了原来的行为,每次服务器发送一批消息时,我都会收到一条消息.

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.

推荐答案

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

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:

不可折叠和可折叠的消息(FCM文档):

除了通知消息外,默认情况下所有消息都是不可折叠的.

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

显然,没有直接解决此问题的方法.FCM一次仅支持四个不同的 collapse_key ,因此我不能只给每个消息一个不同的 collapse_key ,因为那样的话,我最多只能得到四个消息.

Apparently there is no straightforward solution to this problem. FCM only supports four different collapse_keys 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.

相关:如何制作GCM/FCM通知类型的消息不可折叠

这篇关于FCM:设备仅接收多个通知消息之一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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