【问题标题】:Messaging payload contains an invalid "android" property. Valid properties are "data" and "notification"消息负载包含无效的“android”属性。有效属性是“数据”和“通知”
【发布时间】:2019-08-27 14:33:24
【问题描述】:

我正在尝试使用具有平台特定配置的 Firebase Cloud Functions 发送推送通知。我从https://firebase.google.com/docs/cloud-messaging/send-message得到了以下配置

var message = {
  notification: {
    title: '$GOOG up 1.43% on the day',
    body: '$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.',
  },
  data: {
    channel_id: threadId,
  },
  android: {
    ttl: 3600 * 1000,
    notification: {
      icon: 'stock_ticker_update',
      color: '#f45342',
    },
  },
  apns: {
    payload: {
      aps: {
        badge: 42,
      },
    },
  },
};

admin.messaging().sendToDevice(deviceToken, message) 出现错误

Messaging payload contains an invalid "android" property. Valid properties are "data" and "notification"

知道这里有什么问题吗?或者也许是一些适用于 iOS/Android 平台的正确配置示例?

【问题讨论】:

  • 请编辑问题以显示您用于发送消息的完整代码,而不仅仅是有效负载。我们应该能够跟随您的代码,并复制它正在做的事情。
  • @DougStevenson 我理解你的意思,但我不能这样做。
  • “原因”是什么?如果您不显示确切的代码,人们将很难提供帮助。
  • 我没有 :) 我的队友已经下班了,要到星期一才能有空。而且复制整个系统会耗费太多精力。所以希望有人指出我错误地使用文档信息的地方,或者提供具有平台特定配置的sendToDevice函数的有效使用示例。
  • 问题可能与配置无关。它可能与代码有关。没有看到它,我们就不知道。

标签: firebase push-notification firebase-cloud-messaging google-cloud-functions


【解决方案1】:

sendToDevice() 是一个使用旧版 FCM HTTP 端点的函数。旧端点不提供特定于平台的字段。为了获得该功能,您可以通过 send() 函数使用新端点。您可能需要更新您的 Admin SDK 版本。您可以在documentation here 中查看示例。

例如,对于您提供的代码,您可以发送如下消息:

let message = {
  notification: {
    title: '$GOOG up 1.43% on the day',
    body: '$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.',
  },
  data: {
    channel_id: threadId,
  },
  android: {
    ttl: 3600 * 1000,
    notification: {
      icon: 'stock_ticker_update',
      color: '#f45342',
    },
  },
  apns: {
    payload: {
      aps: {
        badge: 42,
      },
    },
  },
  token: deviceToken,
};

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);
  });

请注意,设备令牌现在位于 message 对象中。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-08
  • 1970-01-01
  • 1970-01-01
  • 2017-04-14
  • 1970-01-01
相关资源
最近更新 更多