0

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());
    }
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Listeners can't trigger an app while it's not running. You would need to wake the app using Firebase Cloud Messaging to let it know there is new data. – Doug Stevenson Jun 14 '20 at 23:18
  • Thanks for your advice, I have a question, when the app is closed or in the background, the notifications come to the device with Firebase Cloud Messaging? – Click Ec Jun 17 '20 at 15:41

0 Answers0