简体   繁体   English

Android:通知Firebase实时数据库中具体数值变化

[英]Android: Notify on specific value change in Firebase real-time database

Following is screenshot of my Firebase realtime database.以下是我的 Firebase 实时数据库的截图。 How can I notify a user whenever the item worker_id is changed?每当更改项目worker_id时,如何通知用户?

在此处输入图像描述

I've tried the following code, but it notifies about each type of data change.我尝试了以下代码,但它会通知每种类型的数据更改。 I want notification specific to change in worker_id :我想要特定于worker_id更改的通知:

private void CheckDataChange(){
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference("posts");
    Query query = myRef.orderByChild("workType");
    ValueEventListener valueEventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Log.d("Firebase","onDatachange");
    
            for(DataSnapshot ds : dataSnapshot.getChildren()) {
                try {
                    if(!ds.child("worker_id").getValue(String.class).equals("0")) {

                        Toast.makeText(MainActivity.this, "Request accepted", Toast.LENGTH_SHORT).show();
                    }

                }catch (Exception ex){
                    Log.e("Firebase",ex.getMessage()+ex.getCause());
                }
            }

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            Log.d("Firebase", databaseError.getMessage());
        }
    };

    query.addValueEventListener(valueEventListener);
}

There is nothing built into Firebase to raise an event only when the worker_id in your current structure changes. Firebase 中没有内置任何内容来仅在当前结构中的worker_id更改时引发事件。

The most common way to implement something like that would be to create a map mapping ds.getKey() to the value of worker_id when onDataChange is first called, and then comparing the value you get against that for any updates.实现类似功能的最常见方法是在首次调用worker_id时创建一个onDataChange映射ds.getKey()到 worker_id 的值,然后将您获得的值与任何更新的值进行比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 DropdownButtonFormField 值保存到 Firebase 实时数据库? - How to save DropdownButtonFormField value to Firebase real-time database? Replit 上的 Python Firebase(实时数据库) - Python Firebase (Real-time database) on Replit 如何将Firebase实时数据库迁移到localhost数据库 - How to move Firebase real-time database to localhost database 循环遍历来自 Firebase 的节点 Android Studio 中的实时数据库与 Kotlin - Looping through nodes from Firebase Real-time database in Android Studio with Kotlin 在 Firebase Firestore 中获取特定嵌套数据的实时更新 - Get real-time updates on specific nested data in Firebase Firestore 显示来自firebase实时数据库的用户资料信息 - Display user profile information from firebase real-time database Firebase - 我可以将现有的 JSON 设置()到实时数据库中吗? - Firebase - Can I set() existing JSON into Real-Time Database? Firebase Web 从实时数据库读取 - Firebase Web Read from Real-Time Database 将多个 gps 位置保存到同一个 firebase 实时数据库 - Saving multiple gps locations to same firebase real-time database Firebase 防止实时数据库恶意更新 - Firebase prevent malicious update in real-time database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM