云功能推送通知存在问题

时间:2020-09-16 03:25:01

标签: node.js firebase flutter google-cloud-firestore firebase-cloud-messaging

在我扑扑的Firebase应用程序中,我试图配置推送通知,但由于对javascript非常陌生,因此发现它非常具有挑战性。下面的代码,我试图基于用户活动项目字段(评论)发送通知。如果活动项目的评论字段为空,则表示该活动项目的帖子正在被点赞;如果不为空,则表示该活动项目的帖子正在被评论。大多数代码通过获取用户设备令牌,保存令牌甚至是控制台登录数据快照并将消息发送到设备而起作用。但是问题是当云函数触发并且消息正在发送到用户设备时,消息主体似乎为空,我得到的提示是我没有在活动项目的正确值上编写switch语句(评论)。我几个月来一直在对此进行审查,谢谢

//This part of the cloud function code to listens to new document on
// the activity path  
exports.onCreateActivityNotification = functions.firestore
.document('/activities/{userId}/userActivities/{userActivitiesId}')
 .onCreate(async (snapshot, context) => {
 console.log('activity notification created', snapshot.data());
 //getting user connected to the activity
 const userId = context.params.userId;
 const createdActivityItem = snapshot.data();
 const usersRef = admin.firestore().doc(`users/${userId}`);
 const doc = await usersRef.get();
 const androidNotificationToken = 
 doc.data().androidNotificationToken;
  //checks if user has an androidnotification token
 if(androidNotificationToken){
 //sennds notification if users has a token
   sendNotification(androidNotificationToken, createdActivityItem )
  } else {
   console.log('no notification token');
  }
   //function for sending the notification, I am very sure my problem is coming from this 
    //part of the code. I am not writing the switch statement on the right value. I think I 
    //need to get the exact fields of the activity item
 function sendNotification(androidNotificationToken, userActivities)
  {
  let body;
     switch (userActivities){
      case userActivities.comment !== null:
        body = `${userActivities.fromUserId} commented  : 
      ${userActivities.comment}`
        break;
        case userActivities.comment === null:
        body = `${userActivities.fromUserId} liked your punch`
        break;
        default: 
         break;
        }
     //creates message for push notification
    const message = {
    notification: {body: body},
    token: androidNotificationToken,
    data: {recipient: userId},
    };
    //sends message with admin.messaging()
    admin
   .messaging()
   .send(message)
   .then(response => {
   return console.log('message sent', response);
    }).catch(error =>{
    console.log('error sending message', error);
   })
 }

});

       // This is the code to create the activity item for which triggers the cloud function
        // just for reference
      static void addActivityItem(
      {String currentUserId, Post post, String comment}) {
      if (currentUserId != post.authorId) {
        activitiesRef.document(post.authorId).collection('userActivities').add({
         'fromUserId': currentUserId,
          'postId': post.id,
          'seen': '',
           'postImageUrl': post.imageUrl,
          'comment': comment,
        'timestamp': Timestamp.fromDate(DateTime.now()),
       });
}

}

1 个答案:

答案 0 :(得分:1)

尝试像这样更改开关:

 switch (userActivities.comment){
      case null:
      body = `${userActivities.fromUserId} commented  : ${userActivities.comment}`
      break;
     
      default: body = `${userActivities.fromUserId} liked your punch`

      }

或考虑使用if else