0

In my app, onDataChange() is not called when the device screen is locked.

code

mTicketCountReference = FirebaseDatabase.getInstance().getReference("private/" + EmployeeApp.getApp().getCurrentEmployee().getFirebaseUuid()+"/");
    mTicketCountListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            mAcceptCount = dataSnapshot.child("ticket_counts/accept").getValue() != null ? Integer.valueOf(dataSnapshot.child("ticket_counts/accept").getValue().toString()) : 0;
            mAcceptEscalatedCount = dataSnapshot.child("ticket_counts/accept_escalated").getValue() != null ? Integer.valueOf(dataSnapshot.child("ticket_counts/accept_escalated").getValue().toString()) : 0;
            mAcceptUnreadCount = dataSnapshot.child("ticket_counts/accept_unread").getValue() != null ? Integer.valueOf(dataSnapshot.child("ticket_counts/accept_unread").getValue().toString()) : 0;
            mEscalateCount = dataSnapshot.child("ticket_counts/escalate").getValue() != null ? Integer.valueOf(dataSnapshot.child("ticket_counts/escalate").getValue().toString()) : 0;
            mEscalateUnreadCount = dataSnapshot.child("ticket_counts/escalate_unread").getValue() != null ? Integer.valueOf(dataSnapshot.child("ticket_counts/escalate_unread").getValue().toString()) : 0;
            mEscalateEscalatedCount = dataSnapshot.child("ticket_counts/esclate_escalated").getValue() != null ? Integer.valueOf(dataSnapshot.child("ticket_counts/esclate_escalated").getValue().toString()) : 0;
            mMyTeamCount = dataSnapshot.child("ticket_counts/my_team").getValue() != null ? Integer.valueOf(dataSnapshot.child("ticket_counts/my_team").getValue().toString()) : 0;
            mMyTeamEscalatedCount = dataSnapshot.child("ticket_counts/my_team_escalated").getValue() != null ? Integer.valueOf(dataSnapshot.child("ticket_counts/my_team_escalated").getValue().toString()) : 0;
            mMyTeamUnreadCount = dataSnapshot.child("ticket_counts/my_team_unread").getValue() != null ? Integer.valueOf(dataSnapshot.child("ticket_counts/my_team_unread").getValue().toString()) : 0;
            mMyTicketCount = dataSnapshot.child("ticket_counts/my_ticket").getValue() != null ? Integer.valueOf(dataSnapshot.child("ticket_counts/my_ticket").getValue().toString()) : 0;
            mNewCount = dataSnapshot.child("ticket_counts/new").getValue() != null ? Integer.valueOf(dataSnapshot.child("ticket_counts/new").getValue().toString()) : 0;
            mNewUnreadCount = dataSnapshot.child("ticket_counts/new_unread").getValue() != null ? Integer.valueOf(dataSnapshot.child("ticket_counts/new_unread").getValue().toString()) : 0;
            mPreOrderCount = dataSnapshot.child("ticket_counts/pre_order").getValue() != null ? Integer.valueOf(dataSnapshot.child("ticket_counts/pre_order").getValue().toString()) : 0;
            mRejectCount = dataSnapshot.child("ticket_counts/reject").getValue() != null ? Integer.valueOf(dataSnapshot.child("ticket_counts/reject").getValue().toString()) : 0;
            mRejectedEscalatedCount = dataSnapshot.child("ticket_counts/rejected_escalated").getValue() != null ? Integer.valueOf(dataSnapshot.child("ticket_counts/rejected_escalated").getValue().toString()) : 0;
            mRejectedUnreadCount = dataSnapshot.child("ticket_counts/rejected_unread").getValue() != null ? Integer.valueOf(dataSnapshot.child("ticket_counts/rejected_unread").getValue().toString()) : 0;
            mAlertTimeStamp = Objects.requireNonNull(dataSnapshot.child("User_F_TS").getValue()).toString();
            mAlertId = Integer.parseInt(Objects.requireNonNull(dataSnapshot.child("alert_id").getValue()).toString());
            Log.d(TAG, "onMessageReceived: new Time Stamp: " + mAlertTimeStamp);
            Log.d(TAG, "onMessageReceived: old Time Stamp: " + mSharedPreferences.getString(EmployeeApp.USER_TIME_STAMP, ""));  
}

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    };
    mTicketCountReference.addValueEventListener(mTicketCountListener);  

I need to display these items in a pop-up window. But it's working perfectly when the device is in foreground or background. Is there any workaround for this issue?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
John
  • 3
  • 3

1 Answers1

1

In doze mode the OS reduces power usage of both its own components and apps on the system. The Firebase database client relies on having an open socket connection to the server to monitor for changes. It seems likely that this socket is closed by the OS in doze mode.

The typical solution is to use Firebase Cloud Messaging to signal database changes in such a case. The FCM connection is less aggressively throttled by the OS, so messages there have a better change of making it through during doze mode.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • The scenario is initially a firebase push will occur. Then it's received by the app in standby mode. Then it will lookup for any change in firebase database. At this time onDataChange() is calling. How can I overcome this issue – John Jul 18 '18 at 05:57
  • Is it because of 'addValueEventListener' ? – John Jul 18 '18 at 14:25
  • I'm having a hard time understanding what the problem is. At first I though you were asking why you don't receive the changes when the device is in doze mode, which is what I tried to explain in my answer. But now on your comment I have a feeling you're talking about something else, and I'm not sure I understand what that is. – Frank van Puffelen Jul 18 '18 at 15:32
  • That brings it back to my answer above: it seems like the OS might close the Firebase Database connection in doze mode. Firebase Cloud Messaging is more likely to make it through in that case. – Frank van Puffelen Jul 18 '18 at 18:48
  • Ok, thanks for answering. Can you show me an example or a link – John Jul 18 '18 at 19:00
  • There is no canned example that you can simply copy/paste. But the approach has been discussed before in https://stackoverflow.com/a/42210878, https://stackoverflow.com/a/42611634 and https://www.google.com/search?q=site:stackoverflow.com+firebase+database+android+background – Frank van Puffelen Jul 19 '18 at 03:41