I’m trying to achieve push notification feature to my app using Firebase Cloud Messaging with AWS SQS combination. So far I already implemented producer and consumer for SQS in my NestJS backend with this package @ssut/nestjs-sqs and for deleting im using @aws-sdk/client-sqs. Problem is when consumer get message from queue and send that message to firebase after a while (visibility timout value) same push notification is sent again and again until I purge my queue from console.
My suspect is Amazon Elastic Beanstalk’s with ASG’s instances are consume same message and all of them sent again causing prevent deletion of message.
My producer function is:
async pushToQueue(
devices: any[],
title: string,
type: string,
additionalData: any,
body: string
) {
try {
for (const device of devices) {
await this.sqsService.send('notification', {
id: device.id,
body: {
token: device.token,
body,
title,
type,
additionalData,
},
});
}
} catch (error) {
console.log(error);
}
}
Consumer is:
@SqsMessageHandler('notification', false)
public async handleNotification(message: Message): Promise<void> {
try {
const {
Body,
ReceiptHandle,
} = message;
const data = JSON.parse(Body);
const {
token,
body,
title,
type,
additionalData,
} = data;
await firebase
.messaging()
.send({
token,
data: {
type,
additionalData: JSON.stringify(additionalData)
},
notification: {
body,
title
},
});
await this.sqsService.sqs.deleteMessage({
QueueUrl: process.env.AWS_NOTIFICATION_QUEUE_URL,
ReceiptHandle
});
} catch (error) {
console.error(JSON.stringify(error, null, 2));
}
}