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.
Related
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());
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.
Here is how I do my basic Notification:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Title")
.setContentText(msg);
mBuilder.setAutoCancel(true);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NID, mBuilder.build());
I am not using the BigViewStyle, just your basic notification. If the incoming msg is too long, it doesn't show the entire sentence. Is there anyway to do this without using BigViewStyle?
Is there anyway to do this without using BigViewStyle?
No, simply because the size of a regular Notification is limited.
How can I remove the secondary smaller icon on the bottom right of a notification? Since it's required that we call the setSmallIcon method for the notification, we can’t just remove the call for this method.
Is there any way to remove the secondary icon while retaining it on the status bar? Do we have to call setStyle and set a custom style for the notification?
I got rid of it like that (works for Android 5 and below):
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Bitmap notificationLargeIconBitmap = BitmapFactory.decodeResource(
context.getResources(),
R.drawable.notification_icon_big);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_notification_systray)
//.setLargeIcon(notificationLargeIconBitmap)
.setContentTitle(context.getString(R.string.app_name))
.setContentText(msg)
.setColor(context.getResources().getColor(R.color.blue))
.extend(new NotificationCompat.WearableExtender().setBackground(notificationLargeIconBitmap));
I'm trying to show a notification that is removed when the user taps on it.
I'm using the NotificationCompat class to build my notification and I call setAutoCancel(true) on my builder. This is the piece of code:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("title")
.setAutoCancel(true)
.setContentText("content");
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, mBuilder.build());
The notification is correctly added but when I tap on it nothing happens! What am I doing wrong?
Using setContentIntent should solve your problem:
.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0));
In your example:
NotificationCompat.Builder mBuilder= new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("title")
.setAutoCancel(true)
.setContentText("content")
.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0));
NotificationManager notificationManager= (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, mBuilder.build());
Often you might want to direct the user to the relevant content and so might replace 'new Intent()' with something else.
I uploaded a demo to github.
I know an answer has already been accepted, but I had the same problem with a different solution so I will share it here.
For me, I was using the same NotificationCompat.Builder object to create a notification which called setOngoing(true). This was for an upload progress notification which should not be removed while working.
Anyways, after the task was complete, I called setAutoCancel(true) but the notification was still not swiping away. What I had to do also was call setOngoing(false).
It seems pretty obvious now, but it might save someone else some time in the future.