尝试使用两个uid检索与示例项目相似的两个用户,并从一个中获取fullName值,并从另一个中获取fcmToken值。数据库的结构是/ users / {uid} / fcmToken和/ users / {uid} / fullName。我收到未处理的异常,并且无法以空值读取fullName:
exports.sendFriendRequestPushNotification = functions.https.onRequest((req, res) => {
// Grab the parameters
const requestingUid = req.query.requestingUid; //User sending the request
const requestedUid = req.query.requestedUid; //User receiving the request
const getRequestingProfilePromise = admin.database()
.ref(`/users/${requestingUid}`).once('value');
const getRequestedProfilePromise = admin.database()
.ref(`/users/${requestedUid}`).once('value');
return Promise.all([getRequestingProfilePromise, getRequestedProfilePromise]).then(results => {
const requestingName = results[0].val().fullName;
const fcmToken = results[1].val().fcmToken;
console.log('requestingName: ', requestingName, 'fcmToken: ', fcmToken);
// Notification details.
const payload = {
notification: {
title: 'Looks like you\'re popular',
body: requestingName + ' wants to be friends!',
notificationType: "friendRequest"
},
data : {
notificationType : "friendRequest"
}
};
// Send notification
return admin.messaging().sendToDevice(fcmToken, payload);
})
});