我只是在有新朋友关注您时发送通知,但是当我尝试在通知中返回用户名时,它只是说“未定义已决定关注您”,而不是用户名。
exports.observeFollowing = functions.database.ref('/following/{uid}/{followingId}').onCreate((snapshot,context) => {
var uid = context.params.uid;
var followingId = context.params.followingId;
console.log('User: ' + uid + 'is following: ' + followingId);
return admin.database().ref('/users/' + followingId).once('value', snapshot => {
var userWeAreFollowing = snapshot.val();
return admin.database().ref('/users/' + uid).once('value', snapshot => {
var userDoingTheFollowing = snapshot.val();
var payload = {
notification: {
title: "Someone new has followed you",
body: userWeAreFollowing.username + " has decided to follow you...",
sound: 'default'
}
}
admin.messaging().sendToDevice(userWeAreFollowing.fcmToken, payload)
.then((response) => {
console.log('Successfully sent message:', response);
return response
})
.catch((error) => {
console.log('Error sending message:', error);
});
})
})
答案 0 :(得分:0)
代替
body: userWeAreFollowing.username + " has decided to follow you...",
不能是以下用户名
body: userDoingTheFollowing.usernames + " has decided to follow you...",
并注意在您的实时数据库中您将用户名另存为usernameS,这可能是问题