设备屏幕锁定时未调用Firebase onDataChange

时间:2018-07-17 16:17:19

标签: android firebase firebase-realtime-database

在我的应用中,锁定设备屏幕时未调用onDataChange()

代码

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);  

我需要在弹出窗口中显示这些项目。但是,当设备处于前景或后台时,它可以完美运行。有没有解决此问题的方法?

1 个答案:

答案 0 :(得分:1)

在打ze模式下,操作系统会减少其自身组件和系统上应用程序的功耗。 Firebase数据库客户端依靠与服务器的开放套接字连接来监视更改。操作系统很可能在打ze模式下关闭了此套接字。

典型的解决方案是在这种情况下使用Firebase Cloud Messaging发出信号通知数据库更改。 FCM连接不会受到操作系统的积极限制,因此,在打messages模式下,通过那里的消息会有更好的更改。