Android notification not showing more than 3 actions - android

I want to display a notification with 5 actions, but it ony displays 3 of them. This is the code I'm using for displaying it
Notification notification =
new android.support.v7.app.NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My notification")
.setContentText("Hello World!")
.addAction(new NotificationCompat.Action.Builder(R.drawable.action1, null, pendingIntent1).build())
.addAction(new NotificationCompat.Action.Builder(R.drawable.action2, null, pendingIntent2).build())
.addAction(new NotificationCompat.Action.Builder(R.drawable.action3, null, pendingIntent3).build())
.addAction(new NotificationCompat.Action.Builder(R.drawable.action4, null, pendingIntent4).build())
.addAction(new NotificationCompat.Action.Builder(R.drawable.action5, null, pendingIntent5).build())
.setAutoCancel(false)
.build();
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(MEETING_NOTIFICATION_ID, notification);

you can only show max 3 actions as mentioned in the Notification.Builder addAction (Notification.Action action) too
A notification in its expanded form can display up to 3 actions, from
left to right in the order they were added.
Alternatively you create custom RemoteViews and use setCustomContentView
Reference
Read Custom Notification Layouts
Adding button action in custom notification

Notifications can only display 3 actions.
A notification in its expanded form can display up to 3 actions, from
left to right in the order they were added.
Source, the offical Notification.Builder documentation: https://developer.android.com/reference/android/app/Notification.Builder.html#addAction(android.app.Notification.Action)
If you absolutely need more than 3 actions, you'll have to opt for a solution using a custom view to display in the notification.

Related

Multiline Notification title

I need to show the notification title in two lines but am not able to do so. I have tried to use setStyle() for this but doesn't seem to be working.
My Notification builder code ::
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setSmallIcon(R.mipmap.icon_not);
mBuilder.setColor(getResources().getColor(R.color.colorPrimary));
mBuilder.setContentTitle("")
.setStyle(new NotificationCompat.BigTextStyle().setBigContentTitle(notTitle))
.setContentText(notBodyInt)
.setAutoCancel(true)
.setContentIntent(resultPendingIntent);
Still the notification title is getting wrapped up in a single line.

how to put expand icon in notification in android

i'm trying to put expand icon in notification on my developing app. I already done notification RemortView collapse and expand Layout helping with this site.Below pitcher is my current status of my notification.
My Code given below:
NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext);
builder.setContentIntent(intent);
builder.setTicker(mContext.getResources().getString(R.string.custom_notification));
builder.setSmallIcon(R.drawable.ic_notification_small_basket);
builder.setAutoCancel(true);
builder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
builder.setCustomBigContentView(expandedView);
builder.setCustomContentView(contentView);
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
Actual my need is how to put expand icon and how to get that clicking event from notification RemortView using for expand and collapse the notification.
Look like below pitcher.
This pitcher is just a example for that expand icon.
please assist me.
I know its an old question, but for the sake of the nature of Stack Overflow, I'll give you a proper answer.
Instead of implementing your own header including an arrow to expand/collapse the notification, you might want to decorate your custom notifiction with Androids default header.
The only thing you should take care about is to use the builder methods in the proper order to accomplish your task.Fist you have to set the remote views for collapsed and expanded RemoteViews (what you call RemortViews), and THEN you wrap the NotificationCompat.DecoratedCustomViewStyle around it.
notificationBuilder.setCustomContentView(contentView)
.setCustomBigContentView(expandedView)
.setStyle(NotificationCompat.DecoratedCustomViewStyle()) // <- That's what you want
val notification = notificationBuilder.build()
Do not forget to delete your custom header stuff as it does not make sense anymore.
Click here for the documentation
OK now that we have code I tried this and it worked for me you will need to do some adjustments on where and what is stored
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(),
R.mipmap.ic_launcher))
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
android.app.NotificationManager notificationManager =
(android.app.NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());

How to make notification stay on top?

I have a notification which I always want to stay on top.
I have already given it a priority of 2
Mostly it stays on top but, suppose there is a open WiFi network, then my notification goes down and the notification of open WiFi network comes on the top position.
But when I looked at the notification of vlc(for Android) paying a song, the notification of vlc stay on top and the notification of open WiFi network goes down.
I notification is ongoing and with priority 2.
What should I do. I am a beginner so please bear with me.
And thanks in advance.
have you tried this
NotificationCompat.Builder builder =
(NotificationCompat.Builder) new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.notify_icon)
.setContentTitle("Notification")
.setContentText("ON Top")
.setPriority(Notification.PRIORITY_MAX);
Priority Levels --> PRIORITY_MAX / PRIORITY_HIGH /PRIORITY_DEFAULT /PRIORITY_LOW /PRIORITY_MIN
read
https://material.google.com/patterns/notifications.html#correctly_set_and_manage_notification_priority
https://developer.android.com/reference/android/app/Notification.html#priority
and as StenSoft said
Android: How to create an "Ongoing" notification?
I think you can refer to this code:
Intent push = new Intent();
push.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
push.setClass(this, NotificationDemo.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, push, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationManager nm=(NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
Notification updateNotification = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_face_black_24dp)
.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.mipmap.ic_launcher))
.setContentTitle("Priority Max Notification Title")
.setContentText("Priority Max Notification Message")
.setAutoCancel(false)
.setDefaults(Notification.DEFAULT_SOUND)
.setPriority(Notification.PRIORITY_MAX)
.setFullScreenIntent(pi,true)
.build();
nm.notify("priorityMaxTest",2,updateNotification);

Notification using setFullScreenIntent() for BigTextStyle opening Activity automatically

I have a very strange issue, I am working on Push Notification and it was successfully implemented but when i have used BigTextStyle in Notification to show a long message in notification area with setFullScreenIntent() method then the issue coming up the Notification opening the Activity automatically which is set in PendingIntent.
If I don't use setFullScreenIntent() then notification won't opening Activity automatically the user has to tap or click on Notification to open the Activity set in PendingIntent.
So there are two codes
Without setFullScreenIntent() working fine and not opening Activity automatically:
notification = new NotificationCompat.Builder(context)
.setContentTitle("Title")
.setContentIntent(resultPendingIntent)
.setContentText(message)
.setStyle(
new NotificationCompat.BigTextStyle()
.bigText(message))
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(1, notification.build());
With setFullScreenIntent() also working fine but opening Activity automatically:-
notification = new NotificationCompat.Builder(context)
.setContentTitle("Title")
.setContentIntent(resultPendingIntent)
.setContentText(message)
.setStyle(
new NotificationCompat.BigTextStyle()
.bigText(message))
.setSmallIcon(R.drawable.ic_launcher)
.setFullScreenIntent(resultPendingIntent, true) //Whether true or false same result
.setAutoCancel(true);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(1, notification.build());
public NotificationCompat.Builder setFullScreenIntent (PendingIntent intent, boolean highPriority)
An intent to launch instead of posting the notification to the status
bar. Only for use with extremely high-priority notifications demanding
the user's immediate attention, such as an incoming phone call or
alarm clock that the user has explicitly set to a particular time. If
this facility is used for something else, please give the user an
option to turn it off and use a normal notification, as this can be
extremely disruptive.
On some platforms, the system UI may choose to display a heads-up
notification, instead of launching this intent, while the user is
using the device.
Parameters
intent: The pending intent to launch.
highPriority: Passing
true will cause this notification to be sent even if other
notifications are suppressed.
Found here. As you can see it immediately launches the intent. I don't really know in what case you wanted to use setFullScreenIntent()?
A notification won't automatically expand when a static notification is displayed on top (could be custom bar with wifi, bluetooth and sound control)
pass setFullScreenIntent and setContentIntent with different pending intents.
Worked for me. click on Notif will work and autolaunch will stop

non removable notification

In my app, there is service running on background. I want to notify user that the service is running. But I need that user cannot delete the notification - by pressing clear button or by swipe it out, in notification bar
It means I need to show my notification above Notification area
This is possible but the way you implement it depends on the API level you develop for.
For API levels below 11, you can set Notification.FLAG_NO_CLEAR. This can be implemented like this:
// Create notification
Notification note = new Notification(R.drawable.your_icon, "Example notification", System.currentTimeMillis());
// Set notification message
note.setLatestEventInfo(context, "Some text", "Some more text", clickIntent);
// THIS LINE IS THE IMPORTANT ONE
// This notification will not be cleared by swiping or by pressing "Clear all"
note.flags |= Notification.FLAG_NO_CLEAR;
For API levels above 11, or when using the Android Support Library, one can implement it like this:
Notification noti = new Notification.Builder(mContext)
.setContentTitle("Notification title")
.setContentText("Notification content")
.setSmallIcon(R.drawable.yourIcon)
.setLargeIcon(R.drawable.yourBigIcon)
.setOngoing(true) // Again, THIS is the important line
.build();
To Create Non removable notification just use setOngoing (true);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_service_launcher)
.setContentTitle("My title")
.setOngoing(true)
.setContentText("Small text with details");
Sound like Notification.FLAG_NO_CLEAR or Notification.FLAG_ONGOING_EVENT is what you are looking for.
To Create Non removable notification just use setOngoing (true);
To Create removable notification just use setOngoing (false);

Categories

Resources