I am trying to update firebase realtime database
from FirebaseMessagingService
but it never update when updating from FirebaseMessagingService
.
So logic is when User A send Message to User B (app in background) it calls FirebaseMessagingService
after saving message to local database i need to update message status to delivered so User A will get notify that his message has reached to User B. User B getting notifications and i can see in logs that User B log got executed before setting that value and its listener never get called for success or failure. I also cross checked database reference that is correct.
i tried with setValue and updateChildren method but failed to update data.I removed the listener for clean code.
1:
DatabaseReference messageRef = databaseReference.child(messageNodeKey);
Map<String, Object> update = new HashMap<>();
update.put("Status", msgstatus);
messageRef.updateChildren(update);
2:
DatabaseReference messageRef = databaseReference.child(messageNodeKey).child("Status");
messageRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
Log.d(TAG, "onDataChange: "+snapshot+" "+snapshot.exists());
if (snapshot.exists()) {
messageRef.setValue(msgstatus)
}
}
}
I can update the value when device is in foreground but not when it's calling from FirebaseMessagingService
. Can anyone help me to resolve this issue.
Update: After enable debugging and trying with setvalue
DatabaseReference messageRef = databaseReference.child(messageNodeKey).child("Status");
messageRef.setValue(msgstatus)
Log after enable firebase debugging
RepoOperation D set: /Chat/xxx-aaa/messages/-O5eF3_t0HESRIzjS4HE/Status
DataOperation D set: /Chat/xxx-aaa/messages/-O5eF3_t0HESRIzjS4HE/Status Delivered
RepoOperation D Aborting transactions for path: /Chat/xxx-aaa/messages/-O5eF3_t0HESRIzjS4HE/Status. Affected:
WM-WorkerWrapper I Worker result SUCCESS for Work [ id=40fbeba3-c39c-49dc-8c09-deda65767395, tags={ com.elaxer.Settings_Fragmets.Notification_WorkManager, Notification } ]
System W Ignoring header X-Firebase-Locale because its value was null.
LocalRequestInterceptor W Error getting App Check token; using placeholder token instead. Error: com.google.firebase.FirebaseException: No AppCheckProvider installed.
2024-09-01 02:34:05.829 FirebaseAuth D Notifying id token listeners about user ( xxx ).
Most likely Android closes the connection after a few minutes when the app is backgrounded. From the documentation:
On Android, Firebase automatically manages connection state to reduce bandwidth and battery usage. When a client has no active listeners, no pending write or
onDisconnect
operations, and is not explicitly disconnected by thegoOffline
method, Firebase closes the connection after 60 seconds of inactivity.
If you enable debug logging, you might be able to confirm that.
If that is indeed the cause, you can (detect this connection state and then) toggle the Firebase client offline and then online to get back to a connected state.