如何使用内插字符串作为 Firebase Cloud Functions 和 Typescript 的主题名称

How to use an interpolated string as a topic name for Firebase Cloud Functions and Typescript

我正在尝试编写一个向主题发送通知的云函数。目前,我将主题列为这样的内插字符串:

"topic": `Group: ${groupID}`,

但是,每当调用该函数时,我都会收到此错误:malformed topic name

export const groupMessageReceived = functions.firestore
    .document("Groups/{groupID}/Chat/{message}").onCreate((create, context) => {
      const messageDoc = create.data();
      const groupID = context.params.groupID;
      console.log(`Group: ${groupID}`);
      // const topicName = "Group: " + groupID;
      // sending message to the person who is in the group
      const message = {
        "notification": {
          "title": groupID,
          "body": messageDoc.senderName + ": " + messageDoc.messageContent,
        },
        "topic": `Group: ${groupID}`,
      };

      admin.messaging().send(message)
          .then((response) => {
            // Response is a message ID string.
            console.log("Successfully sent message:", response);
          })
          .catch((error) => {
            console.log("Error sending message:", error);
          });
    });

有人对如何解决这个问题有什么建议吗?我似乎无法弄清楚,也找不到任何帖子。感谢您的任何建议!如果您需要我附上更多源代码,请告诉我!

通过查看错误,我认为您需要仔细检查主题名称并确保它符合要求,如 Firebase docs:

中所述

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