Firebase MessagingService获取onMessageReceived中的推送ID

时间:2018-03-16 03:58:59

标签: firebase firebase-realtime-database firebase-cloud-messaging

我正在使用FirebaseMessagingService。我正在使用NodeJS推送数据,如下所示:

exports.pushData = functions.database.ref('/Datas/{pushId}').onWrite( event => {
console.log('Push data event triggered');
/* Grab the current value of what was written to the Realtime Database */
    var valueObject = event.data.val();
/* Create a notification and data payload. They contain the notification information, and message to be sent respectively */
    const data = {
        data: {
            from_user: valueObject.from_user,
            to_user: valueObject.to_user
        }
    };

此数据存储在Firebase中,如:

-L7grmlClxCd4ebzaSdo
    from_user: "Joe"
    to_user: "Jack"

我想在某些情况下删除此数据,但我无法访问onMessageReceived侦听器中的push Id。我可以使用

获取数据
remoteMessage.getData();

但是,此处没有密钥(推送ID)值。只提取from_user和to_user。我需要在列表器中获取此数据的密钥以便稍后删除此数据。我怎么能有这个?

1 个答案:

答案 0 :(得分:1)

触发路径中通配符的值可在event.params中找到。

将该值添加到您要发送的数据对象中:

const data = {
    data: {
        from_user: valueObject.from_user,
        to_user: valueObject.to_user,
        pushId: event.params.pushId // <= ADD
    }
};