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

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

Related

NotificationCompat at a time

I want to show notification at defined time. I use "setWhen()". But any argument of setWhen() is ignoring and notification always showing instantly. What I did wrong? My code:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setWhen(???)
.setSmallIcon(R.drawable.app_icon)
.setContentTitle("Test")
.setContentText("Test");
Intent resultIntent = new Intent(this, MainActivity.class);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
That's not the purpose of setWhen. It's only a UI thing, if setShowWhen is enabled this timestamp is shown at the notification.
For your purpose, I would suggest the android AlarmManager. An example can be found here
setWhen() is used to show the time on the notification itself, not when the notification is shown.
The time on the top right of the notification is what setWhen() controls:
For showing the notification at a particular time, you can use the AlarmManager class. This has code to do the same.

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

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.

Android: Adding notification to status bar

I have created a notification with NotificationCompat.Builder but i don't know how to show it in status bar. I tried using
NotificationManager notificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(mId, mBuilder.build());
but it doesn't work on 2.2 so i need another way...
this is my notification
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification)
.setContentTitle("Message")
.setContentText("Notification");
but it doesn't work on 2.2
Yes it does.
This sample application shows an IntentService downloading a file and displaying a Notification using NotificationCompat.Builder. It uses code pretty much like yours for actually raising the Notification:
NotificationManager mgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mgr.notify(NOTIFY_ID, b.build());
This code works fine on Android 2.2, as I just tried it (again) on an emulator, and the Notification displays as expected.

How to show notification in status bar?

So i have created this notification in my activity
Notification n = new Notification.Builder(getApplicationContext())
.setContentTitle("New mail from " + sender)
.setContentText(subject)
.setSmallIcon(R.drawable.notification)
.build();
How can i now show it in status/notification bar along with the sound?
There is some good documentation at android developer site
and have a look at the NotificationManager doc's
Here you go, some code...:
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, n);
Note that it is also a goo idea to add your intend to the notification...
mBuilder.setContentIntent(resultPendingIntent);
To add sound you can use the Notification.Builder.setSound() method.
How can i now show it in status/notification bar along with the sound?
Use the Notification.Builder.setSound() method.
You can get the default ringtone like this:
Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
and then set it as notification sound:
Notification.Builder(getApplicationContext()).setSound(uri);
and then after you've built your notification you launch it with:
myNotificationManager.notify(myNotificationId, n);

Categories

Resources