En situaciones específicas, es posible que tu app necesite llamar la atención del usuario rápidamente, como cuando suena una alarma o entra una llamada. Es posible que hayas configurado previamente tu app para que inicie una actividad en segundo plano.
Para proporcionar un comportamiento similar en un dispositivo con Android 10 (API nivel 29) o posterior, completa los pasos que se describen en esta guía.
Cómo crear una notificación de prioridad alta
Cuando crees la notificación, asegúrate de incluir un título y un mensaje descriptivos. También podrás proporcionar un intent de pantalla completa.
En el siguiente fragmento de código, aparece un ejemplo de notificación:
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();
Cómo mostrar la notificación al usuario
Cuando se muestre la notificación al usuario, este puede elegir aceptar o descartar la alerta o el recordatorio de tu app. Por ejemplo, puede elegir aceptar o rechazar una llamada entrante.
Si tu notificación es constante, como una llamada entrante, asóciala con un servicio en segundo plano. El siguiente fragmento de código ejemplifica cómo mostrar una notificación asociada con un servicio en segundo 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);