Im trying to implement the whatsapp like functionality of message being delivered when the receiver's app is in background state and the wifi is on.
I want to call the function to update the message document status from 'sent' to 'delivered'. But cant find where to call it from. I tried calling it inside FirebaseMessaging.onMessage.listen but it is still not working on background.
static setDeliveredStatus(
{required String senderId,
required String receiverId,
required String receiverName}) async {
print('setting = ${receiverName receiverId}');
print('${currentUser!.displayName!} ${senderId}');
QuerySnapshot query = await _firestore
.collection(CollectionKeys.messages)
.doc(receiverName receiverId)
.collection(currentUser!.displayName! senderId)
.where('status', isEqualTo: describeEnum(MessageStatus.sent))
.get();
query.docs.forEach((doc) async {
await doc.reference
.update({'status': describeEnum(MessageStatus.delivered)});
});
}
This above function is currently being called inside initState of my home.dart screen in a onMessage Listener:
FirebaseMessaging.onMessage.listen((message) {
if (message.notification != null) {
LocalNotificationService.createAndDisplayNotificationChannel(message);
FirebaseServices.setDeliveredStatus(
senderId: message.data['senderId'],
receiverId: message.data['receiverId'],
receiverName: message.data['receiverName']);
}
});
CodePudding user response:
The FCM SDK has an onBackgroundMessage
function where you can configure a callback function when receiving messages while the app is in the background.
FirebaseMessaging.onBackgroundMessage(_onBackgroundHandler);
See this document for reference. In the call back method you could put the logic to update the document.