我正在使用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 =?)并将通知发送到该标记。 –