I am developing an app in Android Studio with Java, as a database I am using Firebase, I would like to receive a notification when a child is created within the database of my firebase but when the app is closed, with the code that I show Below you will only receive notifications when the application is open or is in the background.
private void refreshBadgeMesero()
{
final DatabaseReference ordenRef1 = FirebaseDatabase.getInstance().getReference().child("Ordenes_Mesero").child("20");
ordenRef1.addChildEventListener(new ChildEventListener()
{
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s)
{
bottomNavigationView = findViewById(R.id.boton_Navigation_View);
menucesta = bottomNavigationView.findViewById(R.id.Menu_Mesero);
View notificationBadge = LayoutInflater.from(getBaseContext()).inflate(R.layout.notificacion_badge_1, menucesta,false);
badge = notificationBadge.findViewById(R.id.badge_counter);
menucesta.addView(notificationBadge);
badge.isSoundEffectsEnabled();
playNotificationSound();
createNotificationChannel();
Notificacion_Situ();
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s)
{
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
private void playNotificationSound()
{
try {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.ID_COLUMN_INDEX);
Ringtone r = RingtoneManager.getRingtone(getBaseContext(), notification);
r.play();
} catch (Exception e) {
e.printStackTrace();
}
}
private void createNotificationChannel()
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
CharSequence name = "Notificacion";
NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, name, NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(notificationChannel);
}
}
public void Notificacion()
{
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID);
builder.setSmallIcon(R.drawable.ic_icono_superior);
builder.setContentTitle("Pedidos RIO APP");
builder.setContentText("Revisa tu aplicativo y verifica tus nuevos pedidos.");
builder.setColor(Color.BLUE);
builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
builder.setLights(Color.MAGENTA, 1000, 1000);
builder.setVibrate(new long[]{1000,1000,1000,1000,1000});
builder.setDefaults(Notification.DEFAULT_ALL);
Intent intent = new Intent(Pagina_Principal_Activity.this, Pagina_Principal_Activity.class);
PendingIntent intentPendiente = PendingIntent.getActivity(Pagina_Principal_Activity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(intentPendiente);
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(getApplicationContext());
notificationManagerCompat.notify(NOTIFICACION_ID, builder.build());
}