简体   繁体   English

Firebase Cloud函数-sendToTopic()格式错误

[英]Firebase Cloud Functions - Error with sendToTopic() format

My app is a team management system and I want to notify all the players of a certain team if a new event is create via a push notification. 我的应用程序是一个团队管理系统,如果要通过推送通知创建新事件,我想通知某个团队的所有玩家。 There are two types of events that can be created, a Fixture and a Training . 可以创建两种类型的事件: FixtureTraining I want to have separate notifications for each of them. 我希望每个人都有单独的通知。 I am trying to use Firebase Cloud Functions but I keep getting the same error. 我正在尝试使用Firebase Cloud FunctionsFirebase Cloud Functions出现相同的错误。 It says that that the event is triggered but the this error appears. 它说事件已触发,但出现此错误。 I am quite new to JavaScript so please excuse it if it is a simple syntax error but I can't seem to get it to work. 我对JavaScript很陌生,所以如果它是一个简单的语法错误,请原谅,但我似乎无法使其正常工作。

Index.js Index.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);
});

Here is the Error 这是错误

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)

DB Structure 数据库结构

培训结构

治具结构

Functions Log 功能日志

FireBase功能日志

Any help would be greatly appreciated. 任何帮助将不胜感激。

The error message is: 错误消息是:

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

Your call to sendToTopic() is this: 您对sendToTopic()的调用是这样的:

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

Your use of curly braces is violating the rule stated in the error message. 您使用大括号违反了错误消息中所述的规则。

If you meant to build a topic string with the value of currentTeam from the wildcard in your function path definition, you'll need to pull it out of the event context: 如果要在函数路径定义中使用通配符从currentTeam的值构建主题字符串,则需要将其从事件上下文中拉出:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM