I have my DB structure like this:
When someone follows someone else or likes a picture or something, the notification goes into the Notifications
table followed by the users id(who sent the notif) followed by a unique id to separate the notifications. If I don't add that unique id(push()
) only one notification is added and when a new one is supposed to be added only the time and text of the notification changes.
This is how I add the notification:
private void addNotifications(String userid, String postid) {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Notifications").child(userid);
String notificationid = reference.getKey();
mGroupId = reference.push().getKey();
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("dateAdded", System.currentTimeMillis());
hashMap.put("date_time", getTimeDate());
hashMap.put("notificationid", notificationid);
hashMap.put("userid", firebaseUser.getUid());
hashMap.put("text", "liked your post");
hashMap.put("postid", postid);
hashMap.put("ispost", true);
reference.child(mGroupId).setValue(hashMap);
Log.d("Post Key", mGroupId);
}
This shows me in the logcat the correct push key that was just added Log.d("Post Key", mGroupId);
.
I am trying to delete anything older than a day in my notifications table. When I try to access mGroupId
in the notifications fragment I get this error java.lang.NullPointerException: Can't pass null for argument 'pathString' in child()
.
I have been checking answers and comments for days now from experts like Frank Van P. and Alex Mamo and from regular people. I tried downvoted answers and comments and still nothing. I asked this before but the question keeps getting closed as a duplicate even though none of the duplicates help. How can I proceed?
How I try to delete the notifications older than a day:
private void lessThanADayNotification() {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Notifications").child(firebaseUser.getUid());
DatabaseReference freezerItemsRef = reference.child(PostAdapter.mGroupId);
//Log.i("uid", uid);
long cutoff = new Date().getTime() - TimeUnit.MILLISECONDS.convert(12, TimeUnit.SECONDS);
Query oldBug = reference.child("Notifications").orderByChild("dateAdded").endAt(cutoff);
oldBug.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
Notification notification = new Notification();
freezerItemsRef.child(notification.getNotificationid()).removeValue();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}