云功能推送通知出现问题



在我的flutter firebase应用程序中,我试图配置推送通知,但我发现这非常具有挑战性,因为我对javascript非常陌生,。在下面的代码中,我试图发送一个基于用户活动项字段(注释(的通知。如果活动项的评论字段为空,则表示该活动项的帖子正在被点赞;如果不为空,表示该活动项目的帖子正在受到评论。大多数代码的工作原理是获取用户设备令牌、保存令牌,甚至是数据的控制台登录快照,并将消息发送到设备。但问题是,当云功能触发并将消息发送到用户设备时,消息正文显示为null,我得到的提示是,我没有在活动项(注释(的正确值上编写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()),
});
}

}

试着这样更改开关:

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

default: body = `${userActivities.fromUserId} liked your punch`
}

或者考虑使用if else

相关内容

  • 没有找到相关文章