【问题标题】:Firebase ChildEventListener OnChildAdded doesnt read the last child?Firebase Child EventListener OnChildAdded 没有读取最后一个孩子?
【发布时间】:2016-11-08 05:27:15
【问题描述】:

我有这段代码应该在每次添加新孩子时发出通知,但是为了通知发生应该满足一些条件,但我不明白的是它不会读取新添加的孩子除非我重新开始活动,否则我可以进行任何编辑以便它可以读取新添加的孩子吗?

root.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
        String id = dataSnapshot.child("FA_ID").getValue().toString();
        String Bystander_id = dataSnapshot.child("Bystander_ID").getValue().toString();
        String location = dataSnapshot.child("Location").getValue().toString();
        String is_done  = dataSnapshot.child("Done").getValue().toString();


        if(uid.compareTo(id)==0 && is_done.compareTo("false")==0){
        Toast.makeText(HomeScreenFirstAid.this,is_done, Toast.LENGTH_SHORT).show();
        NotificationCompat.Builder builder = new NotificationCompat.Builder(HomeScreenFirstAid.this);
        builder.setSmallIcon(R.drawable.ic_launcher);
        builder.setAutoCancel(true);
        String header = "REPORT RECEIVED:";
        builder.setContentTitle(header);
        String body = "Location:"+location;
        builder.setContentText(body);

        Intent intent = new Intent("ph.edu.upm.agila.extendthelife.controller.rescuer.MainScreenRescuer");
        intent.putExtra("Bystander_id",Bystander_id);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(HomeScreenFirstAid.this);
        stackBuilder.addParentStack(MainScreenRescuer.class);
        stackBuilder.addNextIntent(intent);
        PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(pendingIntent);
        NotificationManager NM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        NM.notify(0,builder.build());
        }


}

【问题讨论】:

    标签: android firebase firebase-realtime-database


    【解决方案1】:

    改变`

    NM.notify(0,builder.build());
    

    到`

    NM.notify((int) System.currentTimeMillis(),builder.build());
    

    无论如何,如果你想在新数据到达时发送通知,你不应该使用 Service 或 Activity,你应该使用 FCM, 在这里阅读:Firebase push notifications update DB

    【讨论】:

    • 你能解释一下你的解决方案吗
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-27
    相关资源
    最近更新 更多