Notificação Android App Fechado

2 respostas
L

pessoal, o que está de errado com o codigo abaixo.

Quando o app está fechado não vibra, nao tem som e não agrupa ao receber as notificações, já o app aberto funciona perfeitamente. Obrigado.

String canal = getString(R.string.default_notification_channel_id);

    Uri uriSom = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    Intent intent = new Intent(this, xxxx.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

    NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();
    bigText.bigText(titulo);
    bigText.setBigContentTitle(corpo);

    //Criar notificação
    NotificationCompat.Builder notificacao = new NotificationCompat.Builder(this, canal)
            .setContentTitle(titulo)
            .setContentText(corpo)
            .setSmallIcon(R.drawable.ic_main_calendar)
            .setSound(uriSom)
            .setAutoCancel(true)
            .setStyle(bigText)
            .setContentIntent(pendingIntent);

    notificacao.setVibrate(new long[]{500, 500});
    notificacao.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);

    //Recupera notificationManager
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    //Verifica versão do Android a partir do Oreo para configurar canal de notificação
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(canal, "canal", NotificationManager.IMPORTANCE_DEFAULT);
        notificationManager.createNotificationChannel(channel);
    }

    //Mostra a notificação no dispositivo.
    notificationManager.notify(1, notificacao.build());

2 Respostas

B

não sou programador android…mais acredito que vc tem que deixar ele rodando em segundo plano…

L

Oi,

Absolutamente nada. Para usar notificação no Android quando sua aplicação for fechada, você deverá criar um Serviço e iniciá-lo no método onStop da sua Activity. Algo próximo deste tutorial:

https://www.tutorialspoint.com/send-a-notification-when-the-android-app-is-closed

Tchauzin!

Criado 22 de novembro de 2019
Ultima resposta 30 de dez. de 2019
Respostas 2
Participantes 3