13
 String ns = Context.NOTIFICATION_SERVICE;
 NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

 int icon = R.drawable.ic_notification_icon;
 android.app.Notification.Builder nbuilder = new Notification.Builder(this);

 nbuilder.setContentTitle(getString(R.string.notifcation_title,mProfile.mName));
 nbuilder.setContentText(msg);
 nbuilder.setOnlyAlertOnce(true);
 nbuilder.setOngoing(true);
 nbuilder.setSmallIcon(icon,level.level);

How can I hide or completely delete the smallIcon? I tried to not use nbuilder.setSmallIcon, but the result is that the notification is not shown at all!

user1071138
  • 656
  • 3
  • 12
  • 30
  • 4
    Only way I know of is to use a transparent image for the icon, and then set the order of the notifications so that yours is last (furthest right) that will make it appear to the user as though there is no icon. – FoamyGuy Apr 23 '13 at 13:30
  • 3
    Why is this in the user's interest? – CommonsWare Apr 23 '13 at 13:34
  • @CommonsWare : One use case: A communication app running in stealth does want the user to know when a message arrives, but doesn't want to show app icons or any icons so that others can't see. – Harish Vishwakarma Aug 07 '17 at 11:58

6 Answers6

17

On Jelly Bean and later you can use nbuilder.setPriority(Notification.PRIORITY_MIN); the exact interpretation of this priority level is left up to the system UI, but in AOSP this causes the notification's icon to be hidden.

This is intended for "ambient" or "background" information that doesn't need to get the user's attention, but if the user happens to be poking around the notification panel, she might be interested. See the Notifications section of the Android Design site for more about how to best use notification priority.

dsandler
  • 2,471
  • 17
  • 11
9

Update: don't use it on Android 7, it's crashing


Using reflection, here's something that's working in Lollipop (emulators and Moto G device) and Marshmallow (emulator)

Notification notification = NotificationCompat.Builder.blabla().build();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  int smallIconViewId = context.getResources().getIdentifier("right_icon", "id", android.R.class.getPackage().getName());

  if (smallIconViewId != 0) {
    if (notification.contentView != null)
      notification.contentView.setViewVisibility(smallIconViewId, View.INVISIBLE);

    if (notification.headsUpContentView != null)
      notification.headsUpContentView.setViewVisibility(smallIconViewId, View.INVISIBLE);

    if (notification.bigContentView != null)
      notification.bigContentView.setViewVisibility(smallIconViewId, View.INVISIBLE);
  }
}

I couldn't find a better way.

This can stop working the moment they change the id of the view or alter any of the RemoteViews fields inside Notification, so use at your own risk.

DiLDoST
  • 335
  • 3
  • 12
Maragues
  • 37,861
  • 14
  • 95
  • 96
3

you can't do that... without setSmallIcon(icon,level.level); Notification is not showing..

Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177
0

You can set your own layout without icon:

RemoteViews notificationView = new RemoteViews(
                context.getPackageName(),
                R.layout.notify
        );
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
      .setContent(notificationView)
Boken
  • 4,825
  • 10
  • 32
  • 42
Denis Dda
  • 21
  • 2
0

Try in your Notification builder adding this line
.setPriority(Notification.PRIORITY_MIN)
So your Notification will be like this :

Notification notification = new Notification.Builder(this)
            .setSmallIcon(R.drawable.stat_notify_chat)
            .setContentTitle("my app")
            .setContentText("my app ")
            .setPriority(Notification.PRIORITY_MIN)
            .setContentIntent(pendingIntent).build();

So in this Case you can see your icon just in Notification bar and it be hide in status bar . hope this help .

Boken
  • 4,825
  • 10
  • 32
  • 42
Mohamed Hocine
  • 575
  • 6
  • 10
0

I faced this problem before and solved it like that:

private int getNotificationIcon() {
    boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT>=android.os.Build.VERSION_CODES.LOLLIPOP);
    // Lollipop or Marshmellow >>>>>>>>>>>>>>>>>>>>> KitKat or less
    return useWhiteIcon ? R.drawable.logo_new : R.drawable.logo;
}

and just call this function in setSmallIcon()

nbuilder.setSmallIcon(getNotificationIcon());
juzraai
  • 5,693
  • 8
  • 33
  • 47
Malik Abu Qaoud
  • 208
  • 1
  • 9