Уведомление Forgroundservice без события щелчка

#android #notifications #android-pendingintent #foreground-service

#Android #уведомления #android-pendingintent #передний план-сервис

Вопрос:

Я создал forgroundservice и отобразил уведомление, когда я нажимаю на уведомление, оно открывает настройки приложения, я хочу избежать этого. Как отключить событие щелчка уведомления о groundservice, так как моя цель-показывать только уведомление, любое событие щелчка не должно здесь происходить.

Код:

 if (Build.VERSION.SDK_INT gt;= Build.VERSION_CODES.O) {    String CHANNEL_ID = "Channel_1";  NotificationChannel channel = new NotificationChannel(CHANNEL_ID,  "Enabled",  NotificationManager.IMPORTANCE_DEFAULT);   ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);   Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)  .setContentTitle("")  .setContentText("").build();   startForeground(1, notification);  }  

Я пробовал несколько решений, но это, похоже, не работает:

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

Комментарии:

1. Удалите отложенное намерение!

2. Я также пробовал без PendingIntent, изначально не было ожидающего намерения, позже я попробовал с pendingintent с пустым/нулевым намерением.

Ответ №1:

Добавьте значок в уведомление, тогда щелчок не произойдет, а также ожидание намерения не требуется.

 Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)  .setSmallIcon(R.drawable.logo)  .setContentTitle("app is running")  .build();