43

This is my code to set up a notification with buttons.

Intent receiverIntent = new Intent(ctx, ResponsivePrefsActivity.class);
        PendingIntent pReceiverIntent = PendingIntent.getActivity(ctx, 1, receiverIntent, 0);
        Intent clearIntent = new Intent(ctx, ResponsivePrefsActivity.class);
        clearIntent.setAction("clear");
        PendingIntent pClearIntent = PendingIntent.getActivity(ctx, 1, clearIntent, 0);

        Intent colorsIntent = new Intent(ctx, ResponsivePrefsActivity.class);
        colorsIntent.setAction("colors");
        PendingIntent pColorsIntent = PendingIntent.getActivity(ctx, 1, colorsIntent, 0);

        Intent animationIntent = new Intent(ctx, ResponsivePrefsActivity.class);
        animationIntent.setAction("animation");
        PendingIntent pAnimation = PendingIntent.getActivity(ctx, 1, animationIntent, 0);

        Notification.Builder builder;
        builder = new Notification.Builder(ctx).setSmallIcon(R.drawable.ic_launcher).setAutoCancel(false)
                .setContentTitle("Draw Me: A Live Wallpaper").setContentText("Never get bored again!")
                .setContentIntent(pReceiverIntent).addAction(R.raw.ic_menu_close_clear_cancel, "Clear", pClearIntent)
                .addAction(R.raw.ic_menu_edit, "Colors", pColorsIntent).addAction(R.raw.ic_menu_play_clip, "Animation", pAnimation);
        Notification notification = builder.build();

        NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0, notification);

Notification is showing up but buttons don't. My device has Android 4.1.1 I set up this notification in a Fragment. What am I doing wrong? Thanks!

DraganescuValentin
  • 842
  • 2
  • 10
  • 16

9 Answers9

132

Let me tell you something which is really awkward. If you have anything in your Ongoing Notification, You wont see the buttons. Typically it happens when you have phone connected to PC via USB. Hope this solves your problem

CommonMan
  • 3,868
  • 2
  • 24
  • 35
  • 8
    The answer of the year....I blamed the last OS update I've made (4.2.2) but isn't its direct fault.... – Alexandru Circus Oct 12 '13 at 15:01
  • 18
    great answer. I also got around this using @Captain Blammo answer by calling .setPriority(Notification.PRIORITY_MAX) on my noticiation builder. This way you can still see the buttons even while you're connected via USB – Jake May 25 '14 at 14:20
  • 14
    You can always swipe downwards on your notification to expand it and show the buttons even when there are other notifications around it. :) – Erlend D. Sep 02 '14 at 11:17
  • 2
    If you have ANYTHING IN YOUR NOTIFICATION? what does that even mean? – frankelot Dec 15 '14 at 17:37
  • 2
    "ANYTHING IN YOUR NOTIFICATION" means any other notfication i.e anything :-) – CommonMan Apr 07 '15 at 21:34
  • oh, I see, you're right!, Thanks, sorry if I sounded a bit harsh – frankelot Jul 22 '15 at 13:19
  • I'm not entirely sure why this is the accepted answer and has tons of upvotes. It's easily fixed! I've put an answer down below that works. It only has 9 upvotes, and isn't accepted despite actually working. – Captain Blammo Sep 01 '15 at 21:20
  • It works, thanks. But it is weird that the notification action button will not show when in landscape orientation even the USB is not connected. – Weiyi Jan 18 '17 at 03:50
30

Just a reminder for anyone with a similar issue. As per the Android Notifications Guide, notifications can appear in two styles:

  • Normal View, where the action buttons don't appear by default (and the user has to expand the notification, in order for them to appear)
  • Big View, which is visible if the notification is first in the notification list, or the user has expanded the notification.

Therefore in order to force the notification to appear in Big View, all we have to do is simply place it on the top of the Notifications list. That can be done by setting the When property to 0 which makes it the oldest among notifications! (Sometimes though we may not want this). So call

setWhen(0)

to your notification and you're done.

paulahniuk
  • 556
  • 3
  • 8
25

The buttons won't appear when there is any Ongoing Notification present in the list, such as a media player control, or the IME switcher options when you are editing text.

Luckily, this can be overcome simply by setting the priority of the notification nice and high. I've only ever used Notification.PRIORITY_MAX to get around this, but PRIORITY_HIGH seems to work as well. Set it like this:

Notification notification = new Notification.Builder(myContext)
.setContentTitle(res.getString(R.string.my_title))
.setPriority(Notification.PRIORITY_MAX)
//The rest of your options
.build();
Captain Blammo
  • 1,897
  • 20
  • 31
18

Just Do this :::

.setPriority(Notification.PRIORITY_MAX)
.setWhen(0)

full code is :

Notification noti = new Notification.Builder(this)
            .setContentTitle("New mail from " + "test@gmail.com")
            .setContentText("Subject").setSmallIcon(R.drawable.ic_launcher)
            .setContentIntent(pIntent)
            .setPriority(Notification.PRIORITY_MAX)
            .setWhen(0)
            .addAction(R.drawable.ic_launcher, "Call", pIntent)
            .addAction(R.drawable.ic_launcher, "More", pIntent)
            .addAction(R.drawable.ic_launcher, "And more", pIntent).build();
user2190487
  • 181
  • 1
  • 2
1

If you wonder why the "buttons" do not show up with the assigned icons (only the text gets displayed - not even as a button, no visible border - material design says so i guess) its probably because android decided to ditch showing the icons, while not documenting this deprecation anywhere in the addAction-Method.

At least none of the accepted answers above (back from 7 years ago) made any icons appear for me on sdk-28 (android 10) - so i figure it must be a undocumented design decision until proven otherwise :)

icyerasor
  • 4,973
  • 1
  • 43
  • 52
0

In my case, the action buttons didn't show up because I was using a custom view for the notification content with the RemoteViews()

s-hunter
  • 24,172
  • 16
  • 88
  • 130
0

In my case it was a persistent notification that I have added. Meaning actions won't show if you have a persistent notification showing.

hiddeneyes02
  • 2,562
  • 1
  • 31
  • 58
0

I faced this issue in NotificationCompat.Builder, I have done below points,

added below lines:

.setPriority(NotificationCompat.PRIORITY_HIGH)

.setWhen(0)

removed below line:

.setContentIntent(pendingIntent)

After these changes the buttons were shown in the Notifications.

Thirumalvalavan
  • 2,660
  • 1
  • 30
  • 32
-1

Just do some changes like this in your code

Notification n = new Notification.Builder(this)
                .setSmallIcon(R.drawable.icon)
                .setContentTitle("New mail from " + "test@gmail.com")
                .setContentText("Subject")
                .setContentIntent(pIntent).setAutoCancel(true)
                .setStyle(new Notification.BigTextStyle().bigText(longText))
                .build();

        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        // Hide the notification after its selected

        notificationManager.notify(0, n);

Add what you need in this.. I hope it will helps you..

Make it Simple
  • 1,832
  • 5
  • 32
  • 57
  • I already have that as it can be seen. I don't need the setStyle method because I want small buttons. What I learned though is that buttons were not showing when tablet was connected to pc through USB. When I disconected the tablet the buttons showed up. This has been happening on E-BODA Impresspeed E200 and Samsung Galaxy Note Tab 10.1 both with JB. This is just crazy, half a day wasted! Anyone else got this problem? – DraganescuValentin Aug 20 '13 at 21:25
  • It's a bug in pretty much all Samsung Android devices. So, you're not the only one facing this issue. As usual, Samsung breaks Android functionality with its TouchWiz crap!... – ChuongPham Jan 26 '14 at 20:39