I am using Android 4.1.1 version
I got a notification when I start the android app if anyone Child exits
I want only notification when a New Child is added to my database or the existed child changes some info.
This is the Code for notification
private void notification() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("n", "n", NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "n")
.setContentText("My Channel")
.setSmallIcon(R.drawable.icon_service)
.setAutoCancel(true)
.setContentText("New Service is Added");
NotificationManagerCompat managerCompat = NotificationManagerCompat.from(this);
managerCompat.notify(1064, builder.build());
}
here I called this notification function in on create method:
databaseRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
notification();
}
@Override
public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
notification();
}
@Override
public void onChildRemoved(@NonNull DataSnapshot snapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
Thank you in advanced
question from:
https://stackoverflow.com/questions/65559652/got-new-notification-when-a-new-child-added-in-realtime-database-or-changed-some 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…