Skip to content
Advertisement

NotificationCompat – how to add action without icon?

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, channelId)
                .setSmallIcon(R.drawable.schedule)
                .addAction(R.drawable.icon,"action test",pi)
                .setStyle(new NotificationCompat.BigTextStyle().bigText(body))
                .setContentTitle(title)
                .setContentText(body);

Above code creates notification and adds one action (button) to it. I want my button to be without icon displayed, but I don’t know how to do that, because icon in parameter addAction is required and not nullable.

Is it even possible to add action button to notification without any icon (btw, icons on action buttons seems to be not even shown on Nougat an Oreo).

Advertisement

Answer

Use NotificationCompat.Action instead. And set 0 as the value for icon

NotificationCompat.Action action =
            new NotificationCompat.Action.Builder(
                    0, "action test", pi
            ).build();

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, channelId)
            .setSmallIcon(R.drawable.schedule)
            .addAction(action)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(body))
            .setContentTitle(title)
            .setContentText(body);

Worked all the devices as far I have tested

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement