I use Firebase Cloud Functions which listen for Firestore changes and send push notifications accordingly. However these notifications are very slow. They arrive to end user with almost a minute delay. For chat application which I'm creating is not so good and should be instantly.
This is the code from Cloud function which does listening and sending the notification:
admin.firestore()
.collection('users')
.where('id', '==', idFrom)
.get()
.then(querySnapshot2 => {
querySnapshot2.forEach(userFrom => {
console.log(`Found user from: ${userFrom.data().nickname}`)
const payload = {
notification: {
title: `You have a message from "${userFrom.data().nickname}"`,
body: contentMessage,
priority: 'high',
badge: '1',
sound: 'mytone'
}
}
// push to the target device
admin
.messaging()
.sendToDevice(userTo.data().pushToken, payload)
.then(response => {
console.log('Successfully sent message:', response)
})
.catch(error => {
console.log('Error sending message:', error)
})
})
})
Is there a way to improve this?