首页 文章

Android - Firebase onChildAdded如何监听所有孩子

提问于
浏览
0

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

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

示例:我发送了2个新通知,而侦听器只读取日期 . 截图如下 .

enter image description here

不合适 . 它不显示新添加的通知"Message" . 我添加了"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();
            }
        });
    }
}

FirebaseMessaging

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

2 回答

  • 0

    我想我发现了这个问题, 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 你会看到 Message

    要解决你的问题,你应该向firebase添加整个对象,就像它解释here

    这应该是结果,遵循文档

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

    看起来您的属性名称与JSON不匹配 . 具体来说:Firebase遵循JavaBean约定,这意味着getter / setter getName()/setName() 定义属性 name (带小写 n ) .

    我建议您更改JSON数据以符合此命名约定 . 但即使使用当前的JSON,您也可以使用一些注释来完成工作:

    public class Notification {
        private String Message;
        private String Date;
    
    
        public Notification(){
        }
    
        public Notification(String Message, String Date){
            this.Message = Message;
            this.Date = Date;
        }
    
        @PropertyName("Date")
        public String getDate() {
            return Date;
        }
    
        @PropertyName("Date")
        public void setDate(String Date) {
            this.Date = Date;
        }
    
        @PropertyName("Message")
        public String getMessage() {
            return Message;
        }
    
        @PropertyName("Message")
        public void setMessage(String Message) {
            this.Message = Message;
        }
    }
    

相关问题