使用通知样式 Firebase 云消息传递发送任意数据

Sending Arbitrary data with notification style Firebase cloud messaging

我有一个简短的问题。我想从云函数发送自定义值 它监听 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...我该怎么做

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

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.

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

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