Firebase云消息传递功能返回未定义

时间:2018-10-30 12:39:07

标签: javascript firebase firebase-cloud-messaging google-cloud-functions

我只是在有新朋友关注您时发送通知,但是当我尝试在通知中返回用户名时,它只是说“未定义已决定关注您”,而不是用户名。

 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);
        });
  })
})

Firebase database

1 个答案:

答案 0 :(得分:0)

代替

body: userWeAreFollowing.username + " has decided to follow you...",

不能是以下用户名

body: userDoingTheFollowing.usernames + " has decided to follow you...",

并注意在您的实时数据库中您将用户名另存为usernameS,这可能是问题