0

I have this code that should make a notification every time a new child is added, however there are conditions that should be met in order for the notification to occur but what I do not understand is that it doesnt read the newly added child unless I start the activity all over again, is there any edit that I can do so it can read that newly added child?

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


}
adjuremods
  • 2,938
  • 2
  • 12
  • 17
Reub
  • 665
  • 2
  • 18
  • 35

1 Answers1

1

Change `

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

to `

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

Anyway, if you want to send an notification when new data arrive, you shouldn't use Service or Activity, you should use FCM, Read here: Firebase push notifications update DB

Community
  • 1
  • 1
Nirel
  • 1,855
  • 1
  • 15
  • 26