【问题标题】:Firebase cloud messaging function returning undefinedFirebase 云消息传递函数返回未定义
【发布时间】:2018-10-30 12:39:06
【问题描述】:

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

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

【问题讨论】:

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


    【解决方案1】:

    代替

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

    不应是以下用户名

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-28
      • 1970-01-01
      • 2019-08-07
      • 2020-06-21
      • 2020-01-17
      • 1970-01-01
      • 1970-01-01
      • 2016-10-09
      相关资源
      最近更新 更多