Firebase ChildEventListener OnChildAdded没有读取最后一个孩子?

时间:2016-11-08 05:27:15

标签: android firebase firebase-realtime-database

我有这个代码,每次添加一个新的孩子时都应该发出通知,但是为了通知发生应该满足的条件,但我不明白的是它没有读取新添加的孩子除非我重新开始活动,是否有任何编辑我可以这样做,它可以读取新添加的孩子?

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


}

1 个答案:

答案 0 :(得分:1)

改变`

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

到`

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

无论如何,如果你想在新数据到达时发送通知,你不应该使用服务或活动,你应该使用FCM, 请在此处阅读:Firebase push notifications update DB