2016-11-08 28 views
0

我有這樣的代碼,應該每次添加新的孩子時都會發出通知,但是爲了通知的發生應該滿足一些條件,但是我不明白的是它不會讀取新添加的孩子,除非我再次開始活動,是否有任何編輯可以做,因此它可以讀取新添加的孩子?Firebase ChildEventListener OnChildAdded不讀取最後一個孩子?

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

變化`

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

到`

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

無論如何,如果你要發送一個通知,當新的數據到達時,你不應該使用服務或活動,您應該使用FCM, 請閱讀這裏:Firebase push notifications update DB

+0

你能解釋一下你的解決方案嗎 –

+0

它的工作原理,但我怎麼能讓它有很多通知? –

相關問題