2

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); 
saravana
  • 39
  • 1
  • 9
  • What do you mean by "without affecting the foreground service of the application"? – CommonsWare Apr 05 '14 at 09:14
  • at starting the startforeground method is used with notification.if my notification is removed it should not affect the application goes into foreground.. – saravana Apr 05 '14 at 11:11
  • atlast found that without notification , foreground service cannot be started :( – saravana Apr 06 '14 at 12:21
  • Possible duplicate of [How to startForeground() without showing notification?](http://stackoverflow.com/questions/10962418/how-to-startforeground-without-showing-notification) – Sam Dec 11 '16 at 20:08

2 Answers2

2

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

Udi Reshef
  • 1,083
  • 1
  • 11
  • 14
  • The trick to remove the notification using a second service has been fixed in Android 7.1. – Sam Dec 09 '16 at 17:55
  • +1 for your advice to have the user disable "show notifications" for the app in the OS settings. I've tested that method and confirmed it works from Android 4.1 through Android 7.1. – Sam Dec 16 '16 at 04:52
1

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

androiduser
  • 189
  • 2
  • 5
  • I believe not seeing the notification's icon has been fixed for a while now. – Sam Dec 09 '16 at 17:57
  • Yes it will but if you scroll down your status bar you may find a notfication related to your app.. but this is sure the easiest way to hide the notification icon.. – androiduser Dec 11 '16 at 16:37
  • Oops, sorry, I meant to type "not **setting** the notification's icon. Yes, setting the notification priority to `PRIORITY_MIN` is a good way to hide the notification icon from the status bar. – Sam Dec 16 '16 at 04:54
  • Regarding not setting the notification's icon (the bit in your answer about removing `setSmallIcon()`, this no longer works starting from Android 4.3. – Sam Dec 16 '16 at 04:55