Question

I need to remove the notification.without affecting the foreground service of the application.thanks in advance

 Notification note = new Notification(R.drawable.ic_blank,"",System.currentTimeMillis());
 Intent i=new Intent(this, MyLocationListener.class);
 i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
 PendingIntent pi=PendingIntent.getActivity(this, 0,i, 0);
 note.setLatestEventInfo(this, "","", pi);
 note.flags |= Notification.FLAG_NO_CLEAR;
 startForeground(42, note); 
Was it helpful?

Solution

Android OS don’t like you to do this because users are entitled to know you are running a foreground serivce on their devices. But if you must remove notification of foreground service : In order to remove the notification icon in the notification area (the status bar) while foreground service still running : Just set the priority to minimum (-2) in the notification builder:

for example:

/* (Notification.PRIORITY_MIN) will remove the notification in statusbar */ 
builder.setPriority(Notification.PRIORITY_MIN)     
 .setSmallIcon(R.drawable.ic_launcher)
 .setContentTitle("Service Started")
 .setTicker("Music Playing")
 .setWhen(System.currentTimeMillis());

This will only remove the small notification icon in the notification area.

if you also need to get rid of the detail notification rectangle in the notification drawer :

then what you need to do is more complex: you need to start your service as foreground, then start another foreground service with the same notification ID as you have in your original service. Then close ( stopself() ) the new foreground service, and Android system will remove the notification (while your original service that started previously will stay in foreground without the notification).

This works fine in 5.1.1, I don’t know if android team already close this breach in marshmallow .

BTW:

  1. In order to do this there is also a non-programmatically way : Go to settings -> applications -> application manager find your application and press on it. You will get inside your application info. Disable the “show notifications” option in your application info. This will get rid of all notifications for your app but it also disable toast messages.. I don’t think there is a way to disable this option in settings programmatically from inside the application - I think android prevent it for security reasons. If anyone knows how to change this programmatically please tell..

  2. if while trying to avoid the notification detail rectangle in the drawer you will remove these lines in your notification builder:

     .setSmallIcon(R.drawable.ic_launcher)
     .setContentTitle("Service Started")
     .setTicker("Music Playing")
     .setWhen(System.currentTimeMillis());
    

    Then Android system will keep on showing a notification rectangle about your service (with the title “This service is running, touch for more information or stop the service ” ) and pressing on this rectangle will lead the user to Your application info on settings -> applications -> application manager with option to “force stop” this service. Regarding that you can read more here https://plus.google.com/105051985738280261832/posts/MTinJWdNL8t

hope it helps :-)

OTHER TIPS

Adding this line builder.setPriority(Notification.PRIORITY_MIN) will remove the notification icon from the status bar and lock screen.

Also removing or commenting builder.setSmallIcon(R.drawable.ic_launcher) removes the notification icon even when you scroll down notifications when device is unlockedbut i m not sure how it will work in android N

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top