Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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).

like image 809
user1209216 Avatar asked Apr 17 '18 06:04

user1209216


People also ask

How do I add an action button to my notifications?

Adding an action button: It is similar to setting the tap action by creating a pending intent only change is that we set it using addAction() to attach it to the notification. We can create intents for both activities or broadcast receivers as shown below: You can create your own BroadcastReciever.

How to add action button in notification in Android?

You can create an intent for the action (in this case stop playing) and then add it as an action button to your notification. Intent snoozeIntent = new Intent(this, MyBroadcastReceiver. class); snoozeIntent. setAction(ACTION_SNOOZE); snoozeIntent.


1 Answers

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

like image 60
Supto Avatar answered Oct 20 '22 01:10

Supto