es.davy.ai

Preguntas y respuestas de programación confiables

¿Tienes una pregunta?

Si tienes alguna pregunta, puedes hacerla a continuación o ingresar lo que estás buscando.

Notificación de servicio en primer plano sin evento de clic.

He creado el servicio en primer plano y he mostrado una notificación, cuando hago clic en la notificación, se abren los ajustes de la aplicación, quiero evitar esto.
¿Cómo desactivar el evento de clic en la notificación del servicio en primer plano, ya que mi objetivo es mostrar solo la notificación y no debería ocurrir ningún evento de clic aquí?

Código:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

        String CHANNEL_ID = "Channel_1";
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
                "Habilitado",
                NotificationManager.IMPORTANCE_DEFAULT);

        ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);

        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("")
                .setContentText("").build();

        startForeground(1, notification);
    }

He probado algunas soluciones, pero no parecen funcionar:

PendingIntent pendIntent = PendingIntent.getActivity(this, 0, new Intent(), 0);
        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("").setContentIntent(pendIntent)
                .setContentText("").build();
Tags:  , , ,

Answer

  1. Avatar for davy.ai

    Para desactivar el evento de clic de la notificación en primer plano, simplemente puede eliminar la línea de código que establece la intención de contenido de la notificación.

    De:

    PendingIntent pendIntent = PendingIntent.getActivity(this, 0, new Intent(), 0);
    Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
            .setContentTitle("").setContentIntent(pendIntent)
            .setContentText("").build();
    

    A:

    Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
            .setContentTitle("")
            .setContentText("").build();
    

    Al eliminar la línea setContentIntent, la notificación seguirá siendo mostrada, pero al hacer clic en ella no se activará ninguna acción.

Comments are closed.