[英]Android - Firebase onChildAdded how to listen for all child
我在顯示新添加的數據時遇到問題。 我的onChildAdded
正在偵聽任何新通知。 此新通知將保存到Firebase。 我的通知包含2個孩子(消息和日期)。
但是,添加新通知時,我只能顯示一個孩子(日期)。 如果我重新運行該應用程序,則可以檢索所有內容,但我希望在使用該應用程序時進行更改。 添加新通知時如何顯示所有內容? 是否因為onChildAdded僅偵聽前一個孩子? 如果需要,我可以發布適配器和getter / setter代碼。
示例:我發送了2條新通知,並且偵聽器僅讀取日期。 屏幕截圖如下。
順序不對。 它不會顯示新添加的通知“消息”。 我添加了“ Hi2”和“ Hi3”。
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);
}
}
}
我認為我發現了這個問題,僅當將孩子添加到列表中時才會觸發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中
這應該是結果,遵循文檔
Notification newNotification = new Notification(notificationTime, remoteMessage.getData().get("body"))
mRef.child("customers").child(userID).child("Notification").child(uniqueID).setValue(newNotification);
似乎您的屬性名稱與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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.