how to put expand icon in notification in android - 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());

Related

how to remove a persistent notification after setongoing(true)?

Here is the code that I'm using to create a notification in Android. This notification is shown as intended and isn't getting removed by swiping the notification.
But I'm unable to remove it programmatically. How should I remove it?
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(getApplicationContext(), Constants.CHANNEL_ID)
.setSmallIcon(R.drawable.small)
.setContentTitle("persistent")
.setContentText("cant remove").setOngoing(true);
mBuilder.build();
In this answer there's a nice explanation on how to do it:
https://stackoverflow.com/a/19268653/3853450
In your case should be something like this:
NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(Constants.CHANNEL_ID);
Also, as given in the original answer:
NotificationManager

BigPictureStyle Notification not showing properly in Xiaomi MIUI-8

I wanted to display a bigPictureStyle Notification in MiUi(don't want to use custom view) using the following code--
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(msg)
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(BitmapFactory.decodeResource(getResources(), R.drawable.b))
.setBigContentTitle(title));
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(10, mBuilder.build());
It works perfectly on raw android but in Xiaomi MIUI 8 (Android Lollipop based) it shows a shadow at the bottom of expanded notification view as in the picture--
How to remove this bottom shadow?
The only solution for now is using a custom view with similar looking big content view.

Android notification not showing more than 3 actions

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.

Removing the secondary icon from android notification

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));

Notification setAutoCancel(true) doesn't work

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.

Categories

Resources