我正在使用Firebase的雲端函數向用戶發送通知。我能夠獲得通知,但問題是每個人都收到通知,我正在嘗試將通知發送給特定用戶。我將用戶的設備ID保存在Firebase的數據庫中,然後向該特定人員發送通知。這裏是我的代碼:使用Firebase的雲端函數向特定用戶發送通知
要保存用戶的數據,這實際上是工作的罰款:
DatabaseReference root = FirebaseDatabase.getInstance().getReference();
DatabaseReference groupsRef = root.child("users").child(Settings.Secure
.getString(ctx.getContentResolver(), Settings.Secure.ANDROID_ID));
groupsRef.child("isLogin").setValue(2);
在第一個活動訂閱該主題:
FirebaseMessaging.getInstance().subscribeToTopic("android");
最後javascript代碼(東西我知道甚少):
var functions = require('firebase-functions');
var admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification = functions.database.ref('/users')
.onWrite(event => {
var eventSnapshot = event.data;
var str = "This is notification"
console.log(str);
var topic = "android";
var payload = {
data: {
isLogin: eventSnapshot.child("975af90b767584c5").child("isLogin").val()
}
};
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);
});
});
,而不是「975af90b767584c5」下面哪個是對沒有硬編碼w,我想發送設備ID,我不知道如何在JavaScript中做。或者如果有其他方法。
任何幫助表示讚賞,謝謝。
你必須發送通知到設備TOKEN不ID –
@DivyeshPatel如何在JavaScript中做到這一點?對不起,但當用戶在應用程序中註冊時,我不知道太多的js –
,您必須將設備標記保存在數據庫中,然後從數據庫中獲取該標記(其中userid =?)並將通知發送到該標記。 –