如何在 FCM 中设置“topic”属性?

编程入门 行业动态 更新时间:2024-09-26 02:19:43

如何在 FCM 中设置“topic”<a href=https://www.elefans.com/category/jswz/34/1771332.html style=属性?"/>

如何在 FCM 中设置“topic”属性?

当我尝试在 Node.js 中发送 FCM(Firebase Cloud Messaging)消息时,遇到错误,指出“topic”属性无效。然而,即使阅读了官方文档,我也找不到解决方案。如何正确设置“topic”属性?

const message = {
  data: {
    score: '850',
    time: '2:45'
  },
  notification: {
    title: 'title',
    body: 'body',
    badge: "1",
    sound: "default"
 },
  topic: 'topic'
};

  admin.messaging().sendToDevice(token, message, option)
  .then(pushResponse => {
    return { text: token };
  })
  .catch(error => {
    throw new functions.https.HttpsError('unknown', error.message, error);
  });

错误文字:

Exception from a finished function: Error: Messaging payload contains an invalid "topic" property. Valid properties are "data" and "notification".

我尝试了以下方法,但没有成功。

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

错误文字:

Exception from a finished function: ReferenceError: getMessaging is not defined

  admin.messaging().sendToTopic(topic,message,option)
  .then(pushResponse => {
    return { text: token };
  })
  .catch(error => {
    throw new functions.https.HttpsError('unknown', error.message, error);
  });

我能够发送 FCM,但 Flutter 应用程序在收到 FCM 后立即崩溃。

回答如下:

我就是这样做的,你可以试试

{
  "notification": {
  "title": "title notification",
  "body": "content"
  },
  "to": "/topics/name_of_topic",
}

更多推荐

如何在 FCM 中设置“topic”属性?

本文发布于:2024-05-31 07:25:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1771415.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   如何在   FCM   topic

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!