I have a foreground service which is used to upload file and show the progress of it via notification.
The code below I'm posting works pretty good, except that on every update event, it vibrates.
My code to start a foreground service and init the notification is as below.
//this method is called on onStartService().
void startForegroundService() {
mCompatBuilder.setSmallIcon(R.drawable.img_checkbox);
mCompatBuilder.setContentIntent(pendingIntent);
mCompatBuilder.setTicker("Ticker");
mCompatBuilder.setWhen(System.currentTimeMillis());
mCompatBuilder.setContentTitle("Uploading");
mCompatBuilder.setProgress(100, 0, false);
mCompatBuilder.setContentText("0%");
mCompatBuilder.setContentIntent(pendingIntent);
mCompatBuilder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
mCompatBuilder.setPriority(NotificationCompat.PRIORITY_LOW);
mCompatBuilder.setOngoing(true);
mCompatBuilder.setAutoCancel(false);
mCompatBuilder.setVibrate(new long[]{0L});
startForeground(1, mCompatBuilder.build());
}
The progress update part is as below.
public void updateProgress(int progress, String contentText) {
mCompatBuilder.setContentText(contentText);
mCompatBuilder.setProgress(100, progress, false);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mCompatBuilder.build());
}
I've tried to lower the priority of the notification, and also tried to set the setVibration to new long{0L}. But neither of it seems to be working.
What else can I try? Please help!
Can you try removing this line? mCompatBuilder.setVibrate(new long[]{0L});
How can I get the title or the notification ID that I want to Clear.
My App produces several notification .
Api is 19 .
using  NotificationListenerService useful?
Yes, you can use getActiveNotifications from NotificationListenerService to get all active notifications.
StatusBarNotification[] activeNotifications = service.getActiveNotifications();
Then loop through available notifications and use getId()
if(activeNotifications!=null){
for(StatusBarNotification notification : activeNotifications ){
if (notification.getId() == ID_TO_CHECK) {
// do your operation
}
}
}
I want to delete a notification which is located on my status bar. I used Notification Manager.cancel() but it didn't work. I used cancelAll() too with the same result.
My code is:
public class NotificationListenerService extends android.service.notification.NotificationListenerService {
...
#Override
public void onNotificationPosted(StatusBarNotification sbn) {
NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nManager.cancel(sbn.getId());
nManager.cancel(sbn.getTag(), sbn.getID());
nManager.cancelAll();
}
I found more answers here on SO but none solved my issue. The sbn object is right, I print in my log all its data.
It's possible to delete an exiting notification located on the status bar with Android API < 20? (so without using cancelNotification())
With API >= 21, you can try use the method cancelNotification (String key)
With API < 21, you can try use the method cancelNotification (String pkg, String tag, int id)
Ive looked around and found some examples of using the new notificationlistenerservice class, but im having issues with it not even hearing the notification post event.
the examples that im finding are using a gui and broadcast receivers which arent really needed by my goal.
im trying to fire off toast notifications at least with this, but i get nothing. i have verified that the application is allowed to listen to notifications and has notifications enabled.
Anything im missing?
//listen for new notifications?
#Override
public void onNotificationPosted(StatusBarNotification sbn) {
Toast.makeText(getApplicationContext(), sbn.getPackageName().toString(), 1).show();
Toast.makeText(getApplicationContext(), "anything? anything? please?", 1).show();
}
//listen for the removal of the notities
#Override
public void onNotificationRemoved(StatusBarNotification sbn) {
//uhh not done yet
}
//prepare
public void onCreate() {
super.onCreate();
//ongoing notification test, is this needed for notification listeners?
NotificationManager notificationManager;
Notification myNotification;
Context context = getApplicationContext();
myNotification = new Notification.Builder(context)
.setContentTitle("NotifyWake")
.setSmallIcon(R.drawable.ic_notification_open)
.setOngoing(true)
.build();
notificationManager = (NotificationManager)getSystemService(context.NOTIFICATION_SERVICE);
notificationManager.notify(1, myNotification);
//end ongoing notification
}
Figured it out, you must create some threaded or broadcasted method to offload anything from the post/removed events. things like toast do not work since the method fires off a threading error and silently fails(noted in logcats)
so, a simple solution is to extend broadcastreceiver and pass the heavy lifting and gui stuff to that method.
Android: I am trying to cancel a notification from the notification bar after a package being installed.
What I am doing is the following:
public class MyBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "MyBroadcastReceiver";
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
Uri data = intent.getData();
//some code goes here
//get the id of the notification to cancel in some way
notificationhelper._completeNotificationManager.cancel(id);
}
}
}
where
public class notificationhelper {
public static NotificationManager _completeNotificationManager = null;
public void complete() {
if (_completeNotificationManager == null)
_completeNotificationManager = (NotificationManager) _context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(
R.drawable.notification,
_context.getString(R.string.notification),
System.currentTimeMillis());
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.flags |= Notification.FLAG_NO_CLEAR;
_completeNotificationManager.notify(TEXT, id, notification);
}
}
But the notificationhelper._completeNotificationManager.cancel(id) does not work. I tried to use notificationhelper._completeNotificationManager.cancelAll(); and it works. What I am doing wrong?
In my experience, you can't cancel all notifications with a particular ID, regardless of tag.
That is, if you create two notifications like so:
notificationManager.notify(TAG_ONE, SAME_ID, notification_one);
notificationManager.notify(TAG_TWO, SAME_ID, notification_two);
Then, notificationManager.cancel(SAME_ID) won't cancel either of them! I suspect that this is because the "tag" field, if unspecified in notify() and cancel(), defaults to null, which you have to cancel explicitly.
So, to cancel these two notifications, you have to call:
notificationManager.cancel(TAG_ONE, SAME_ID);
notificationManager.cancel(TAG_TWO, SAME_ID);
In your case, you're supplying "TEXT" as the tag but cancelling just using the id, which defaults to using tag=null.
So, either don't provide TEXT as your tag:
_completeNotificationManager.notify(id, notification);
Or, if you need separate notifications and don't want them to clobber each other, keep track of the active tags:
_completeNotificationManager.notify(TEXT, id, notification);
collectionOfActiveTags.add(TEXT);
...
for (String activeTag : collectionOfActiveTags)
notificationhelper._completeNotificationManager.cancel(activeTag, id);
I wish that what you're trying to do was supported, as it seems that it should be.
Well this is probably irrelevant at this point, but it should be posted here so that people like me dealing with the same problem might find the solution.
If NotificationManager.cancel() isn't working, try changing the ID for the notification.
notificationManager.notify(NOTIFICATION_ID, notification);
When I changed NOTIFICATION_ID from 1 to [RANDOM_NUMBER], it magically started working. I assume that 1 is somehow reserved, although there is no note in any documentation...
An of course make sure you use the same NOTIFICATION_ID to cancel:
notificationManager.cancel(NOTIFICATION_ID);
My notifications were not getting removed because my service was Foreground Service and NOT a regular service started by StartService.
If your service is foreground, call stopForeground(true) instead of stopself(). So now my code looks like this:
NotificationManagerCompat.from(this).cancel(NotificationHelper.PLAYER_NOTIFICATION_ID);
stopForeground(true);
and it worked, notification was removed.
I was facing the same issue recently. I have managed to solve it.
So from what i understood.
use the id which is basically a random number to notify and send this same id to the piece of code (receiver/activity...) where you want to cancel it.
When using tags, it seems to not work for me as I was giving one tag to all notifications but with unique id. It worked only on the first tag so I completely avoided using tags. If you want to use tags, issue unique tags along with unique id and use them both while cancelling.
So final answer... what I used and what works for me:
STEP 1:
int notif_id = (int)(System.currentTimeMillis()%10000);
STEP2: add this id inside the action intent (I am launching an activity where the notification gets cancelled on the action click):
Intent notificationSettingsIntent = new Intent(context.getApplicationContext(), NotificationSettingsActivity.class);
notificationSettingsIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
notificationSettingsIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
notificationSettingsIntent.putExtra("fromNotification",true);
notificationSettingsIntent.putExtra("notif_id",notif_id);
PendingIntent notificationSettingsActivityPendingIntent = PendingIntent.getActivity(context,notif_id,notificationSettingsIntent,PendingIntent.FLAG_ONE_SHOT);
STEP 3: notify using the id in the step 1 but with no tags
NotificationManagerCompat notificationCompat = NotificationManagerCompat.from(context.getApplicationContext());
notificationCompat.notify(notif_id,notificationBuilder.build());
Now in the Activity which gets opened by my action click, I cancel the notification as:
NotificationManagerCompat notificationCompat = NotificationManagerCompat.from(context.getApplicationContext());
notificationCompat.cancel(getIntent().getIntExtra("notif_id"));
Works every time now.
Sorry for late joining!
But following worked fine for me.
NotificationManagerCompat mNotificationManager = NotificationManagerCompat.from(context.getApplicationContext());
mNotificationManager.cancel("<TAG>",<Notificatoin-id>);
Following worked for me:
final NotificationManagerCompat mNotificationManager = NotificationManagerCompat.from(context.getApplicationContext());
mNotificationManager.cancel(<Notificatoin-id>);
Since there is no accepted answer, I am posting another one with same scenario I faced
private fun stopForegroundService() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
stopForeground(STOP_FOREGROUND_DETACH)
}else if(Build.VERSION.SDK_INT < Build.VERSION_CODES.N){
stopForeground(true)
}
notificationManager.cancel(NOTIFICATION_ID)
}
Point to note is first you need to set stopForeground(false) then call notificationManager.cancel(NOTIFICATION_ID)
If you change the order, it won't work