Reputation: 872
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!
Upvotes: 44
Views: 27382
Reputation: 2768
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.
Upvotes: 0
Reputation: 2979
In my case it was a persistent notification that I have added. Meaning actions won't show if you have a persistent notification showing.
Upvotes: 0
Reputation: 5242
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 :)
Upvotes: 1
Reputation: 25826
In my case, the action buttons didn't show up because I was using a custom view for the notification content with the RemoteViews()
Upvotes: 0
Reputation: 181
Just Do this :::
.setPriority(Notification.PRIORITY_MAX)
.setWhen(0)
full code is :
Notification noti = new Notification.Builder(this)
.setContentTitle("New mail from " + "[email protected]")
.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();
Upvotes: 18
Reputation: 1897
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();
Upvotes: 26
Reputation: 576
Just a reminder for anyone with a similar issue. As per the Android Notifications Guide, notifications can appear in two styles:
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.
Upvotes: 31
Reputation: 3928
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
Upvotes: 133
Reputation: 1882
Just do some changes like this in your code
Notification n = new Notification.Builder(this)
.setSmallIcon(R.drawable.icon)
.setContentTitle("New mail from " + "[email protected]")
.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..
Upvotes: -1