Java OnChildListener每次都执行
我正在使用Java OnChildListener每次都执行,java,android,firebase-realtime-database,android-notifications,Java,Android,Firebase Realtime Database,Android Notifications,我正在使用ChildEventListener方法在实时数据库中获取新添加的数据,只有在那时才会弹出通知。但每当我退出执行ChildEventListener的活动,然后再返回时。此时会出现一个通知。这不是我需要的 我需要的是,只有当数据添加到实时数据库时,即使应用程序已关闭,也会显示通知 我的一些代码: DatabaseReference refrence2 = FirebaseDatabase.getInstance().getReference().child("Orders&q
ChildEventListener
方法在实时数据库中获取新添加的数据,只有在那时才会弹出通知。但每当我退出执行ChildEventListener
的活动,然后再返回时。此时会出现一个通知。这不是我需要的
我需要的是,只有当数据添加到实时数据库时,即使应用程序已关闭,也会显示通知
我的一些代码:
DatabaseReference refrence2 = FirebaseDatabase.getInstance().getReference().child("Orders").child(getIntent().getStringExtra("storeid"));
refrence2.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
Intent intent = new Intent(MainOrder.this, Splash.class);
PendingIntent contentIntent = PendingIntent.getActivity(MainOrder.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder b = new NotificationCompat.Builder(MainOrder.this,"neworder");
b.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.totoic)
.setLargeIcon(BitmapFactory.decodeResource(MainOrder.this.getResources(), R.drawable.totoic))
.setTicker("1111")
.setPriority(2)
.setContentTitle("طلب جديد!!")
.setContentText("! "+previousChildName+"لديك طلب جديد من")
.setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_SOUND)
.setContentIntent(contentIntent)
.setContentInfo("Info");
NotificationManager notificationManager = (NotificationManager) MainOrder.this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(100, b.build());
}
@Override
public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot snapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
我注意到的另一件事是childEventListener
中的previouschildname
总是返回null
有什么想法吗?请试试这个。根据,此回调针对每个现有子级触发一次,然后在每次将新子级添加到指定路径时再次触发。