如何从Firebase数据库中获取Android的嵌套推送密钥

huangapple go评论74阅读模式
英文:

How to get nested push key from Firebase database Android

问题

我有这样的数据库结构:

如何从Firebase数据库中获取Android的嵌套推送密钥

当有人关注其他人、喜欢图片或其他内容时,通知会进入“通知”表,后面跟着用户ID(发送通知的用户),然后是一个唯一ID,用于区分通知。如果我不添加那个唯一ID(使用push()方法),只会添加一个通知,当应该添加新的通知时,只有通知的时间和文本会改变。

这是我添加通知的方式:

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

这会在日志中显示刚刚添加的正确推送键Log.d("Post Key", mGroupId);

我正在尝试删除通知表中一天前的任何内容。当我尝试在通知片段中访问mGroupId时,我会收到此错误java.lang.NullPointerException: Can't pass null for argument 'pathString' in child()

我已经查看了像Frank Van P.和Alex Mamo等专家以及普通人的答案和评论好几天了。我尝试了被踩的答案和评论,但仍然没有结果。我以前问过这个问题,但问题一直被关闭为重复,尽管没有一个重复的问题有用。我该怎么继续?

我如何尝试删除一天前的通知:

private void lessThanADayNotification() {
    DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Notifications").child(firebaseUser.getUid());
    DatabaseReference freezerItemsRef = reference.child(PostAdapter.mGroupId);
    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) {
        }
    });
}
英文:

I have my DB structure like this:

如何从Firebase数据库中获取Android的嵌套推送密钥

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(&quot;Notifications&quot;).child(userid);

    String notificationid = reference.getKey();

    mGroupId = reference.push().getKey();

    HashMap&lt;String, Object&gt; hashMap = new HashMap&lt;&gt;();
    hashMap.put(&quot;dateAdded&quot;, System.currentTimeMillis());
    hashMap.put(&quot;date_time&quot;, getTimeDate());
    hashMap.put(&quot;notificationid&quot;, notificationid);
    hashMap.put(&quot;userid&quot;, firebaseUser.getUid());
    hashMap.put(&quot;text&quot;, &quot;liked your post&quot;);
    hashMap.put(&quot;postid&quot;, postid);
    hashMap.put(&quot;ispost&quot;, true);

    reference.child(mGroupId).setValue(hashMap);
    Log.d(&quot;Post Key&quot;, mGroupId);
}

This shows me in the logcat the correct push key that was just added Log.d(&quot;Post Key&quot;, 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&#39;t pass null for argument &#39;pathString&#39; 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(&quot;Notifications&quot;).child(firebaseUser.getUid());
    DatabaseReference freezerItemsRef = reference.child(PostAdapter.mGroupId);
    //Log.i(&quot;uid&quot;, uid);

    long cutoff = new Date().getTime() - TimeUnit.MILLISECONDS.convert(12, TimeUnit.SECONDS);
    Query oldBug = reference.child(&quot;Notifications&quot;).orderByChild(&quot;dateAdded&quot;).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) {
        }
    });
}

答案1

得分: 2

我之前在你删除的问题中已经提到过:无法从“Notifications”节点上的“dateAdded”属性进行查询。

Firebase查询在查询的节点上直接操作子节点的平面列表。因此,在您的数据模型中,您可以查询特定用户的通知,但无法跨所有用户查询通知。

如果您想要查询所有用户的通知,您需要将它们存储为平面列表,并在这种情况下,将UID存储为每个通知节点的属性。

参考链接:

英文:

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:

huangapple
  • 本文由 发表于 2020年10月24日 09:50:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/64509217.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定