发送 FCM 消息时条件否定

Negation in condition while sending FCM message

我正在订阅一个主题,这是我在 Android 中的应用程序版本。

FirebaseMessaging.getInstance().subscribeToTopic(APP_VERSION);

现在每次启动更新时,我都必须取消订阅旧版本并订阅新版本,这样如果我发送更新通知,只有老用户才能收到。相反,如果我只是否定一个版本,那就太好了。

"condition": "!'2.1.1' in topics",

FCM 中有这样的东西吗?

开箱即用的主题不支持像"people not subscribed to xxx"这样的操作。

但是对于您的用例(向尚未更新的用户发送通知),我建议使用 Firebase Notification(不要与 FCM、firebase 云消息传递混淆),您可以找到文档 here .

关键是在 firebase 控制台中,您可以按应用程序版本过滤目标。请参阅下面 particular part of the doc 的屏幕截图。我自己在生产中对其进行了多次测试,并且效果很好。

您只能根据用户是否订阅某个主题来定位用户,而不是根据他们是否订阅。

由于通配符匹配仅在 Firebase 通知中可用(public API 尚不存在)并且控制台只能发送通知消息(如果该应用程序不在前台),您的选择将仅限于解决方法。

我能想到的几个:

  1. 跟踪每个 user/device ID 安装在 Map<Token, Version> 结构中的应用程序版本。您可以为此使用 Firebase 数据库。

  2. 枚举所有旧版本并以通知为目标。

  3. 向所有人(通过云消息API)发送一条新版本公告的数据消息,然后在 onMessageReceived() 中禁止显示消息,如果他们已经有最新版本。

嗯...事后看来,#3 似乎是一个相当干净的解决方案。您发送过多,但系统可以处理,只要频率不是太高,很可能您的用户不会受到太大影响。

看来 Firebase 现在支持这个了! Firebase send-message

You can include up to five topics in your conditional expression, and parentheses are supported. Supported operators: &&, ||, !. Note the usage for !:

!('TopicA' in topics)

With this expression, any app instances that are not subscribed to TopicA, including app instances that are not subscribed to any topic, receive the message.