如何使用Cloud Functions for Firebase向特定用户发送推送通知

我使用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); }); }); 

尝试通过本教程。 它将解决你的许多疑问https://aaronczichon.de/2017/03/13/firebase-cloud-functions/

由于Firebase云计算function非常新,您可能无法在互联网上find相关资料,但本教程将帮助您了解许多function。 我希望它有帮助。

快乐的编码