可以通过firebase函数访问firebase admin
Can access firebase admin through firebase funcition
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.sendNotification = functions.database.ref('/Notifications/{user_id}/{notification_id}').onWrite((change, context) => {
const userId = context.params.user_id
const notificationId = context.params.notification_id
console.log(userId)
if(!change.after.val()){
return console.log('A notification has been deleted in database: ', notificationId);
}
return admin.database.ref('/Users/'+userId).once('value')
.then(function(snapshot){
const token_id = snapshot.child('device_token').val()
console.log(token_id)
})
});
我尝试使用 firebase 函数和 firebase admin 获取存储在 firebase 实时数据库中的设备令牌
这是我的数据库
您需要 admin.database().ref(...)
而不是 admin.database.ref(...)
。请注意 "database" 之后的括号 - 这是一个方法调用。
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.sendNotification = functions.database.ref('/Notifications/{user_id}/{notification_id}').onWrite((change, context) => {
const userId = context.params.user_id
const notificationId = context.params.notification_id
console.log(userId)
if(!change.after.val()){
return console.log('A notification has been deleted in database: ', notificationId);
}
return admin.database.ref('/Users/'+userId).once('value')
.then(function(snapshot){
const token_id = snapshot.child('device_token').val()
console.log(token_id)
})
});
我尝试使用 firebase 函数和 firebase admin 获取存储在 firebase 实时数据库中的设备令牌
这是我的数据库
您需要 admin.database().ref(...)
而不是 admin.database.ref(...)
。请注意 "database" 之后的括号 - 这是一个方法调用。