Em situações específicas, seu aplicativo pode precisar chamar a atenção do usuário com urgência, por exemplo, para um alarme ativado ou uma chamada recebida. É possível que você tenha configurado o aplicativo para essa finalidade iniciando uma atividade enquanto ele estava em segundo plano.
Para oferecer um comportamento semelhante em um dispositivo com o Android Q (API de nível 29), siga as etapas descritas neste guia.
Criar uma notificação de prioridade alta
Ao criar a notificação, inclua um título descritivo e uma mensagem. Como opção, você também pode fornecer um intent de tela cheia.
Um exemplo de notificação aparece no snippet de código a seguir:
Kotlin
val fullScreenIntent = Intent(this, CallActivity::class.java) val fullScreenPendingIntent = PendingIntent.getActivity(this, 0, fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT) val notificationBuilder = NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.drawable.notification_icon) .setContentTitle("Incoming call") .setContentText("(919) 555-1234") .setPriority(NotificationCompat.PRIORITY_HIGH) .setCategory(NotificationCompat.CATEGORY_CALL) // Use a full-screen intent only for the highest-priority alerts where you // have an associated activity that you would like to launch after the user // interacts with the notification. Also, if your app targets Android 10 // or higher, you need to request the USE_FULL_SCREEN_INTENT permission in // order for the platform to invoke this notification. .setFullScreenIntent(fullScreenPendingIntent, true) val incomingCallNotification = notificationBuilder.build()
Java
Intent fullScreenIntent = new Intent(this, CallActivity.class); PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(this, 0, fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.drawable.notification_icon) .setContentTitle("Incoming call") .setContentText("(919) 555-1234") .setPriority(NotificationCompat.PRIORITY_HIGH) .setCategory(NotificationCompat.CATEGORY_CALL) // Use a full-screen intent only for the highest-priority alerts where you // have an associated activity that you would like to launch after the user // interacts with the notification. Also, if your app targets Android 10 // or higher, you need to request the USE_FULL_SCREEN_INTENT permission in // order for the platform to invoke this notification. .setFullScreenIntent(fullScreenPendingIntent, true); Notification incomingCallNotification = notificationBuilder.build();
Exibir a notificação para o usuário
Ao exibir sua notificação para o usuário, ele pode escolher confirmar ou dispensar o alerta ou lembrete do aplicativo. Por exemplo, o usuário pode escolher se aceita ou recusa uma chamada telefônica recebida.
Se sua notificação for contínua, como uma chamada telefônica recebida, associe a notificação a um serviço em primeiro plano. O snippet de código a seguir mostra como exibir uma notificação associada a um serviço em primeiro plano:
Kotlin
// Provide a unique integer for the "notificationId" of each notification. startForeground(notificationId, notification)
Java
// Provide a unique integer for the "notificationId" of each notification. startForeground(notificationId, notification);