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

时间:2017-07-10 12:41:04

标签: java android firebase firebase-realtime-database

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

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

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

enter image description here

不按顺序。它不会显示新添加的通知"消息"。我添加了#34; 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 :(得分:1)

我想我发现了这个问题,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 :(得分:0)

看起来您的属性名称与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;
    }
}