Reputation: 31
In my flutter firebase app, I am trying to configure push notification but I am finding it very challenging since I am very new to javascript,. The below codes, I am trying to send a notification based on a user activity item field(comment). If the comment field of the activity item in empty, it means the post of that activity item is being liked and if it is not empty, it means the post of that activity item is being commented on. Most of the code works from getting the user device token, saving it, and even console login snapshot of the data and sending the message to the device. But the problem is when the cloud function triggers and the message is being sent to the user device, the message body appears to be null, I am getting the hint that I am not writing the switch statement on the right value of the activity item(comment). I have been reviewing this some months now, Thank you
//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()),
});
}
}
Upvotes: 0
Views: 73
Reputation: 1305
Try changing your switch like this:
switch (userActivities.comment){
case null:
body = `${userActivities.fromUserId} commented : ${userActivities.comment}`
break;
default: body = `${userActivities.fromUserId} liked your punch`
}
or consider using if else
Upvotes: 1