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!
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
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.
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();
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();
In my case, the action buttons didn't show up because I was using a custom view for the notification content with the RemoteViews()
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 :)
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..