【问题标题】:Sending Arbitrary data with notification style Firebase cloud messaging使用通知样式 Firebase 云消息发送任意数据
【发布时间】:2017-12-31 07:08:59
【问题描述】:

我有一个简短的问题。我想从云函数发送自定义值 它监听 Firebase 数据库中的 onWrite 事件。 我的云函数代码:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.pushNotification = functions.database.ref('/chats/notifications/{pushId}').onWrite( event => {
  var values = event.data.val();

const payload = {
    notification: {
        title: values.username,
        body: values.message,
        sound: "default",
        displayName: values.username,
        UiD:  values.recieverUiD
    },
};
const options = {
    priority: "high"
};
return admin.messaging().sendToTopic(values.recieverUiD, payload, options);});

现在数据库结构是这样的:

聊天

-通知

-一些ID

-用户名:fdsdf

-uid: 9043554

如何将 UiD 发送到我的设备?代码中的方式不适用于 displayName...我应该怎么做

【问题讨论】:

  • 我不是在数据库结构上作弊,也不是在作弊
  • 我通过使用我的 UID 订阅主题来发送通知。在节点脚本中,我将其发送到主题。并且知道我只想知道如何使用 remoteMessage.getData.get("userUiD"); 发送自定义值并在 FirebaseMessagingService 中接收它们;

标签: android firebase firebase-cloud-messaging google-cloud-functions


【解决方案1】:

您可以通过在有效负载中添加与通知键相同级别的数据键来在通知消息中传递任意键值对:

{"notification": {...},
"data" : {
      "uid": 9043554
    }
}

【讨论】:

  • 谢谢。我只是忘记了,无法在 Google 上找到它
【解决方案2】:

您需要在数据库中保存每个用户的通知令牌。

Here the documentation to get this notification token.

Here a great sample to send Push Notifications with Firebase Cloud Functions.

自定义您的有效负载:

{
  "to": "APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...",
  "notification": {
    "body": "great match!",
    "title": "Portugal vs. Denmark",
    "icon": "myicon"
  },
  "data": {
    "Nick": "Mario",
    "Room": "PortugalVSDenmark"
  }
}

Data message FCM documentation.

【讨论】:

  • 这工作正常。但是如何将自定义值发送到我的设备?例如 userUiD,...
  • 你知道如何从 Firebase 脚本中删除数据库中的父级吗??
  • @KarlWolf 你必须使用 Firebase 函数,“通知”和“数据”的好处是可以使用 Firebase Web 控制台发送,但是要小心行为可能很复杂。这就是它在 Android 上的工作方式,以防它对您有所帮助: 如果应用程序在后台,则组合推送,则默认情况下是视觉通知,并且在用户单击时可以访问数据,如果它在前台,则视觉通知是自定义的和数据随时可用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-18
  • 1970-01-01
  • 2019-12-30
  • 1970-01-01
  • 2017-10-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多