node.js - 通过条件将 Firebase 云消息传递 (FCM) 发送到多个主题的正确语法是什么

标签 node.js firebase firebase-cloud-messaging google-cloud-functions

我只是想使用 Google Cloud Functions 向订阅主题组合的设备发送通知。

文档说:

"'TopicA' in topics && ('TopicB' in topics || 'TopicC' in topics)"

我尝试做的是:

var topicsConditions = `'${type}' in topics && ('${area}' in topics || '${city}' in topics)`;

// Send a message to devices subscribed to the provided topic.
admin.messaging().sendToCondition(topicsConditions, notificationPayload)
  .then(function(response) {
    // See the MessagingTopicResponse reference documentation for the
    // contents of response.
    console.log("Successfully sent message:", response);
  })
  .catch(function(error) {
    console.log("Error sending message:", error);
  });

但我不断收到此错误:

Error sending message: { Error: Invalid argument provided. Raw server response: "Invalid "condition" field: only support 'topics' conditions ". Status code: 400. at FirebaseMessagingError.Error (native) at FirebaseMessagingError.FirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:39:28) at FirebaseMessagingError.PrefixedFirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:85:28) at new FirebaseMessagingError (/user_code/node_modules/firebase-admin/lib/utils/error.js:241:16) at /user_code/node_modules/firebase-admin/lib/messaging/messaging-api-request.js:115:23 at process._tickDomainCallback (internal/process/next_tick.js:135:7) errorInfo: { code: 'messaging/invalid-argument', message: 'Invalid argument provided. Raw server response: "Invalid "condition" field: only support \'topics\' conditions\n". Status code: 400.' }, codePrefix: 'messaging' }

任何人都可以指导我正确的语法吗?

编辑:主题的日志输出为:

Topic conditions = 'MyType+' in topics && ('Giza, Egypt ' in topics || 'القاهرة الكبرى' in topics)

最佳答案

主题名称中可以使用的字符仅限于:

  • 小写字母az
  • 大写字母AZ
  • 数字09
  • 字符- _ . ~ %

您的主题名称包含无效字符 +、空格和阿拉伯语。

更多详细信息请参见 the documentation :

Developers can choose any topic name that matches the regular expression: "[a-zA-Z0-9-_.~%]+"

有效条件字符串的示例是:

var topicsConditions = "'Aswan' in topics && ('Giza' in topics || 'Cairo' in topics)";

我在对 admin.messaging().sendToCondition() 的调用中成功使用了此字符串

关于node.js - 通过条件将 Firebase 云消息传递 (FCM) 发送到多个主题的正确语法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48652101/

相关文章:

node.js - Sequelize 和在关联中指定外键的问题

javascript - 前端如何加载本地脚本?

node.js - UDP 和网络套接字的数据丢失

javascript - Firestore数据结构: Storing An Order in Firestore

firebase - FCM 到多个设备( token )列表

node.js - 将 Heroku App 连接到 Atlas MongoDB 云服务

ios - 如何在 Firebase 中检索特定的 child ?

swift - 在闭包外部访问 Firebase 变量

node.js - Firebase 通知无法通过 HTTP.post 创 build 备组

javascript - 如何使用 firebase 云函数增加徽章计数