java - Android - Firebase onChild添加了如何监听所有 child

标签 java android firebase firebase-realtime-database

我在显示新添加的数据时遇到问题。我的 onChildAdded 正在监听任何新通知。此新通知将保存到 Firebase 中。我的通知包含 2 个子项(消息和日期)。

但是,当我添加新通知时,我只能显示其中一个子项(日期)。如果重新运行应用程序,我可以检索所有内容,但我希望在使用应用程序本身时进行更改。添加新通知时如何显示所有内容?是因为 onChildAdded 只监听前一个 child 吗?如果需要,我可以发布我的适配器和 getter/setter 代码。

示例:我发送了 2 个新通知,监听器仅读取日期。截图如下。

enter image description here

不按顺序。它不显示新添加的通知“消息”。我添加了“Hi2”和“Hi3”。 enter image description here

NotificationFragment.java

public class NotificationFragment extends Fragment {

    private void prepareNotification1() {
        FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
        String userID = user.getUid();

//        mRef.child("customers").child(userID).child("Notification").addListenerForSingleValueEvent(new ValueEventListener() {
//            @Override
//            public void onDataChange(DataSnapshot dataSnapshot) {
//                for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
//                    System.out.println(postSnapshot.getValue());
//                    Notification menu = postSnapshot.getValue(Notification.class);
////                    Notification menu = new Notification(postSnapshot.getValue(String.class));
//                    notificationList.add(menu);
//                }
//                mAdapter.notifyDataSetChanged();
//            }
//
//            @Override
//            public void onCancelled(DatabaseError databaseError) {
//
//            }
//
//        });
//


        mRef.child("customers").child(userID).child("Notification").addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                    Log.d("child", "onChildAdded:" + dataSnapshot.getKey());
                    Log.d("child", "onChildAdded:" + dataSnapshot.getValue());
                    Notification menu = dataSnapshot.getValue(Notification.class);
                mAdapter.notifyDataSetChanged();
            }
        });
    }
}

Firebase消息传送

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    private DatabaseReference mRef;

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        final String uniqueID = UUID.randomUUID().toString();

        mRef.child("customers").child(userID).child("Notification").child(uniqueID).child("Date").setValue(notificationTime);
        mRef.child("customers").child(userID).child("Notification").child(uniqueID).child("Message").setValue(remoteMessage.getData().get("body"));
        createNotification(remoteMessage.getData().get("title"),remoteMessage.getData().get("body"));
}

Notification.java

public class Notification {
    private String Message;
    private String Date;


    public Notification(){
    }

    public Notification(String Message, String Date){
        this.Message = Message;
        this.Date = Date;
    }

    public String getDate() {
        return Date;
    }

    public void setDate(String Date) {
        this.Date = Date;
    }

    public String getMessage() {
        return Message;
    }

    public void setMessage(String Message) {
        this.Message = Message;
    }
}

NotificationAdapter.java

public class NotificationAdapter extends RecyclerView.Adapter<NotificationAdapter.NotificationHolder> {
    private List<Notification> notificationList;
    private Context mContext;

    public NotificationAdapter(Context mContext, List<Notification> notificationList) {
        this.mContext = mContext;
        this.notificationList = notificationList;
    }


    @Override
    public NotificationHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflatedView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_item_notification, parent, false);
        return new NotificationHolder(inflatedView);
    }

    @Override
    public void onBindViewHolder(final NotificationHolder holder, int position) {
        Notification notification = notificationList.get(position);
        holder.body.setText(notification.getMessage());
        holder.time.setText(notification.getDate());
    }

    @Override
    public int getItemCount() {
        return notificationList.size();
    }

    public class NotificationHolder extends RecyclerView.ViewHolder {
        public TextView body;
        public TextView time;

        public NotificationHolder(View view) {
            super(view);
            body = (TextView) view.findViewById(R.id.bodyMsg);
            time = (TextView) view.findViewById(R.id.time);
        }
    }
}

最佳答案

我想我发现了这个问题,仅当将 child 添加到列表中时,onChildAdded 才会被触发,因此当您第一次从远程读取它们或将新通知添加到在线列表时。

在这部分代码中,您首先添加仅设置了 Date 字段的通知对象,然后编辑其 Message

mRef.child("customers").child(userID).child("Notification").child(uniqueID).child("Date").setValue(notificationTime);
mRef.child("customers").child(userID).child("Notification").child(uniqueID).child("Message").setValue(remoteMessage.getData().get("body"));

这就是为什么当 onChildAdded 被触发时,您只能看到 Date 字段,如果您还覆盖 onChildChanged,您将看到 消息

要解决您的问题,您应该将整个对象添加到 firebase 中,如所解释的 here

这应该是遵循文档的结果

Notification newNotification = new Notification(notificationTime, remoteMessage.getData().get("body"))
mRef.child("customers").child(userID).child("Notification").child(uniqueID).setValue(newNotification);

关于java - Android - Firebase onChild添加了如何监听所有 child ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45012369/

相关文章:

android - Apache Cordova : how to block network access in config. xml

javascript - Angular/Firebase - 从 Firebase 返回键和值

java - 无法呈现推文

java - 对使用 UDP 套接字的代码进行单元测试的推荐方法是什么?

java - 如何在不导致窗口泄漏的情况下将 ProgressDialog 与 AsyncTask 一起正确使用?

java - 原始类型导致的代码重复 : How to avoid insanity?

java - 查询数据库以确保新用户不会使用已有的用户名进行注册

java - Java中字符串和整数的空值

android - Android Studio 更新 Bumblebee 后,该项目不是基于 Gradle 的项目

java - Android Crashlytics 空指针异常