Node.js 如何使用Firebase的云功能向特定用户发送推送通知

Node.js 如何使用Firebase的云功能向特定用户发送推送通知,node.js,firebase,firebase-realtime-database,firebase-cloud-messaging,google-cloud-functions,Node.js,Firebase,Firebase Realtime Database,Firebase Cloud Messaging,Google Cloud Functions,我正在使用Firebase作为我的Android应用程序的后端,我对Firebase使用云功能非常陌生,我想知道在事件发生时如何向特定用户发送推送通知 例如,在数据库的adminName节点上发生写入时,如何在下面的代码中向用户发送uId推送通知: exports.sendNotification = functions.database.ref('/users/{uId}/groups/{adminName}') .onWrite(event => { // Grab

我正在使用Firebase作为我的Android应用程序的后端,我对Firebase使用云功能非常陌生,我想知道在事件发生时如何向特定用户发送推送通知

例如,在数据库的adminName节点上发生写入时,如何在下面的代码中向用户发送uId推送通知:

exports.sendNotification = functions.database.ref('/users/{uId}/groups/{adminName}')
    .onWrite(event => {

    // Grab the current value of what was written to the Realtime Database.
    var eventSnapshot = event.data;
    var str1 = "Author is ";
    var str = str1.concat(eventSnapshot.child("author").val());
    console.log(str);

    var topic = "android";
    var payload = {
        data: {
            title: eventSnapshot.child("title").val(),
            author: eventSnapshot.child("author").val()
        }
    };

    // Send a message to devices subscribed to the provided topic.
    return admin.messaging().sendToTopic(topic, payload)
        .then(function (response) {
            // See the MessagingTopicResponse reference documentation for the
            // contents of response.
            console.log("Successfully sent message:", response);
        })
        .catch(function (error) {
            console.log("Error sending message:", error);
        });
    });
您必须拥有该特定用户的注册令牌并使用。