2
我使用Firebase作爲我的Android應用的後端,並且對於使用Firebase的雲端功能非常新,我想知道如何發送特定用戶的推送通知當事件發生時。如何使用Firebase的雲端功能向特定用戶發送推送通知
例如如何將我的UID推送通知時寫時發送用戶在下面的代碼在adminName節點數據庫上:
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);
});
});
你必須有*登記令牌*爲特定用戶和使用['sendToDevice'(https://firebase.google.com/docs/reference/admin/node/admin。 messaging.Messaging#sendToDevice)。 –