Search code examples
androidnotificationsandroid-pendingintentforeground-service

Forgroundservice notification without click event


I have created forgroundservice and displayed notification, when I click on notification it opens app settings, I want to avoid this. How to disable forgroundservice notification click event, as my purpose is to only show notification any click event should not happen here.

Code:

if (Build.VERSION.SDK_INT >= 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);
    }

I have tried few solutions but that doesn't seem to work:

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

Solution

  • Add icon to notification then the click will not happen and also Pending intent is not required.

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