【发布时间】:2019-06-01 06:22:38
【问题描述】:
我正在尝试通过发出 https 请求从客户端应用程序调用函数来从应用程序中获取当前用户 ID。它正在工作,我正在获取 uid。问题是每当触发 onWrite() 函数时,我都需要向特定用户 ID 下的特定用户发送通知。如何将我从 https 函数获得的 uid 传递给触发函数?
我刚刚使用 https.onCall() 从客户端应用程序中检索了当前用户的用户 ID。 然后在触发函数中,我尝试使用 expo 推送通知 API 向按 uid 分类的特定用户发送通知。
函数/index.js
const functions = require('firebase-functions');
var fetch = require('node-fetch')
const admin = require('firebase-admin')
admin.initializeApp(functions.config().firebase)
exports.getUid = functions.https.onCall((data, context) => {
uid = data.text
console.log(data.text)
})
exports.makerOrders = functions.database.ref('orders')
.onWrite((snapShot, context) => {
console.log('functions is triggered :)')
return admin.database().ref('Notifications').child(uid)
.once('value')
.then((shot) => {
var message = []
var tokens = shot.val().expoTokens;
if (tokens) {
message.push({
"to": tokens,
"body": "Notifications are working fine :)"
})
}
return Promise.all(message)
}).then(message => {
fetch('http://exp.host/--/api/v2/push/send', {
method: "POST",
headers: {
"Accept": "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify(message)
})
return Promise.all(message)
})
})
App.js
var uid = firebase.auth().currentUser.uid
var getUid = firebase.functions().httpsCallable('getUid')
getUid({text: uid}).then(result => {
var msg = result.data
console.log(msg)
console.log('Called successfully :)')
}).catch(error => {
console.log('Error :( in sending the requests')
});
我希望从应用程序中获取当前用户的 uid 值,以便向该特定用户发送推送通知。
我无法检索当前经过身份验证的用户的 uid。
【问题讨论】:
-
不是 100% 清楚你想做什么。
makerOrders何时触发?谁写入orders节点?如果是用户为什么不把用户的uid添加到orders节点呢?
标签: javascript react-native firebase-realtime-database google-cloud-functions