javascript - Firebase Cloud Functions - sendToTopic() 格式错误

标签 javascript firebase firebase-cloud-messaging google-cloud-functions

我的应用程序是一个团队管理系统,如果通过推送通知创建新事件,我想通知某个团队的所有玩家。可以创建两种类型的事件,FixtureTraining。我想为他们每个人都有单独的通知。我正在尝试使用 Firebase Cloud Functions 但我一直收到同样的错误。它说事件已触发,但出现此错误。我是 JavaScript 的新手,所以如果这是一个简单的语法错误但我似乎无法让它工作,请原谅。

索引.js

//import firebase functions modules
const functions = require('firebase-functions');
//import admin module
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

// // Listens for new fixtures being created
exports.newFixture = functions.database.ref('/Fixture/{currentTeam}/{pushId}').onWrite((change, context) => {

    console.log('new fixture event triggered');

    //  Grab the current value of what was written to the Realtime Database.
    var valueObject = change.after.val();

  // Create a notification
    const payload = {
        notification: {
            title: "New" + valueObject.type,
            body: "Details:" + " "+ valueObject.date + " " +valueObject.time + " " + "please open app and confirm availability",
            sound: "default"
        },
    };

  //Create an options object that contains the time to live for the notification and the priority
    const options = {
        priority: "high",
        timeToLive: 60 * 60 * 24
    };

    return admin.messaging().sendToTopic("/topics/{currentTeam}", payload, options);
});


// Listens for new trainings being created
exports.newTraining = functions.database.ref('/Training/{currentTeam}/{pushId}').onWrite((change, context) => {

  console.log('New training event triggered');

  //  Grab the current value of what was written to the Realtime Database.
  var valueObject = change.after.val();

// Create a notification
  const payload = {
      notification: {
          title: "New" + valueObject.type,
          body: "Details:" + " "+ valueObject.date + " " + valueObject.time + " " + "please open app and confirm availability",
          sound: "default"
      },
  };

//Create an options object that contains the time to live for the notification and the priority
  const options = {
      priority: "high",
      timeToLive: 60 * 60 * 24
  };

  return admin.messaging().sendToTopic("/topics/{currentTeam}", payload, options);
});

这里是错误

Error: Topic provided to sendToTopic() must be a string which matches the format "/topics/[a-zA-Z0-9-_.~%]+".
    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 Messaging.validateTopic (/user_code/node_modules/firebase-admin/lib/messaging/messaging.js:925:19)
    at /user_code/node_modules/firebase-admin/lib/messaging/messaging.js:611:19
    at process._tickDomainCallback (internal/process/next_tick.js:135:7)

数据库结构

Training Structure

Fixture Structure

函数日志

FireBase Function LOG

如有任何帮助,我们将不胜感激。

最佳答案

错误信息是:

Topic provided to sendToTopic() must be a string which matches the format "/topics/[a-zA-Z0-9-_.~%]+".

您对 sendToTopic() 的调用是这样的:

sendToTopic("/topics/{currentTeam}", payload, options)

您对大括号的使用违反了错误消息中规定的规则。

如果您打算使用函数路径定义中通配符的 currentTeam 值构建主题字符串,则需要将其从事件上下文中拉出:

const currentTeam = context.params.currentTeam
sendToTopic("/topics/" + currentTeam, payload, options)

关于javascript - Firebase Cloud Functions - sendToTopic() 格式错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49971032/

相关文章:

android - Firebase 推送通知(当我启动应用程序时,服务不会从 list 中调用)

javascript - 如何设置列之间的边距或填充0px?

javascript - 存储在 Session Javascript 中后获取 DOM 事件

javascript - 带动画的 Google map SVG 标记

java - 如何从 Firebase Android 查询中的 StartAt 特定位置和 EndAt 特定位置获取值

python - 主题消息的 Firebase 云消息传递无效注册

javascript - 如果当前日期为 1,(getDate() - 1) 函数将获取值 0

firebase - 在 Firebase 中手动插入数据

javascript - firebase sendEmailVerification 在不存在的电子邮件地址上不会给出错误

Android FCM 向用户发送消息