最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

网站源码admin25浏览0评论

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

如何在 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",
}
发布评论

评论列表(0)

  1. 暂无评论