-1

I have my DB structure like this:

My DB

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) {
        }
    });
}
halfer
  • 19,824
  • 17
  • 99
  • 186
terrabytes
  • 11
  • 7
  • 1
    You've included a picture of the JSON tree in your question. Please replace that with the actual JSON as text, which you can easily get by clicking the Export JSON link in the overflow menu (⠇) of [your Firebase Database console](https://console.firebase.google.com/project/_/database/data/). Having the JSON as text makes it searchable, allows us to easily use it to test with your actual data and use it in our answer and in general is just a Good Thing to do. – Frank van Puffelen Oct 24 '20 at 02:13

1 Answers1

2

I've said this before on the question that you since deleted: there is no way to query on your dateAdded property from the Notifications node.

Firebase queries operate on a flat list of the child nodes directly on the node where you query. So in your data model you can query notifications for a specific user, but you can't query notifications across all users.

If you want to query across the notifications of all users, you will need to store them as a flat list, with the UID in that case stored as a property of each notification node.

See:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807