[Android Software/Hacking General [Developers Only]] nicobo:
This is a guide to hiding your notifications' icons in the notification bar if you don't want one.

I've used the code in this post for my own app (see the URL at the end if you're curious) and I've felt other developers would benefit from this experimental work.

The text of this post comes from the original article, which contains a few more details and has a better formatting imho (no spam, just a technical article).

You can browse and download the sample code of this post at : http://bitbucket.org/nicobo/tweaking-android-notifications.

Why ?

Using Android's notification bar is a great way to give users a quick access to your services from (almost) any application at (almost) any time.

Android's Notification API is really made for instant notifications, not persistent ones, and that implies several inconveniences :

when creating a notification, the developer has to put an icon in the reduced notification bar, taking some precious space

by default an 'event timestamp' is shown next to the expanded content of the notification, which means nothing for a permanent service

there is no programmatic way to know if a notification is currently displayed or not

In this post I'm describing a way to create a notification that :

doesn't show up in the reduced notification bar

doesn't have a timestamp in the expanded notification bar

I haven't found a reusable way to know if a notification is currently displayed...

Creating the Notification

I'll quickly pass over the standard code which follows.

// The Intent that will be called upon a click on the notification
Intent notificationIntent = new Intent(context, NotifyService.class);

// Builds the notification view as it should be displayed in the end
// Note that the icon R.drawable.icon_notif we provided to the android.app.Notification constructor is really the final one that will be displayed when the user will expand the notification bar : no trick for now.
Notification notification = new Notification(R.drawable.icon_notif, "My notification has just been displayed", 0);

// Sets the flags that will determine the behaviour of the notification
notification.flags |= Notification.FLAG_ONGOING_EVENT; // this tells that this notification is going to last
notification.flags |= Notification.FLAG_NO_CLEAR; // this goes together with 'ongoing', as the notification will not be removed if the user chooses to clear notifications

// Defines the notification's expanded message
CharSequence contentTitle = resources.getString(R.string.app_name);
CharSequence contentText = resources.getString(R.string.notification_text);

// Wraps our Intent with a PendingIntent instance
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

// Injects all the elements into the notification
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

On the last line, calling Notification.setLatestEventInfo(...) sets the layout of our Notification.

Now we can start tweaking it !

Note that if we were to build a more standard notification but still with no timestamp displayed next to it, we could set notification.when=0 at this point to disable the display of the timestamp.

Tweaking the notification

This is the interesting part !

/**
* The time of the notification rules if it is aligned to the right or left
* of the notification bar, and therefore we use it to place the empty icon
* where it will not be noticed by the user.
*/
private static final long TIME_HIDDEN = SDK_VERSION >= 9 ? -Long.MAX_VALUE : Long.MAX_VALUE;

// ...

// 1. After the notification's layout has been filled, we reset the time that will be used
// to place it on the edge of the notification bar in order to hide its icon
notification.when = TIME_HIDDEN;

// 2. The following line only changes the icon displayed in the notification bar when reduced.
// The icon displayed in the expanded view is still the one that was initially set.
// Together with a placement to the right (or left depending on the SDK version), it hides the icon.
notification.icon = R.drawable.icon_hidden;

// 3. Triggers the notification.
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(MYNOTIFS_1, notification);
// After that, we cannot tell about the state of the notification...

It is very important to place this code after the call to Notification.setLatestEventInfo(...), to make sure that the notification's view has already been built.

The magic happens in two steps :

we move the icon to the edge of the bar so that it doesn't take some place between 2 other icons

we replace the original icon with a transparent 1x1px icon (R.drawable.icon_hidden)

The first step simply implies defining a fake timestamp that will be the greatest (Long.MAX_VALUE) or the smallest possible one (-Long.MAX_VALUE) so that the icon will be placed before of after any other icon (that has a reasonable timestamp). To know which value to use, we must test the SDK version.

The other step is to replace the icon with a transparent one, making it invisible to the user. We simply used a transparent PNG image.

The best part is that the icon we used to build the Notification is still there when the notification bar is expanded, it is not replaced with the transparent one...

The last lines are standard Android code that trigger the notification.

Et voilà !

End note

I have to emphasize that this is experimental work, and the method described in this article may not be available on all devices, and could be deprecated as well in future releases of Android.

However, the app I've created based on this article has already been installed on several kind of devices without issue so you may be confident for now...

Happy notifying !

References

Original article : http://nicobo.net/articles/tweaking-android-notifications

Official documentation about notifications : http://developer.android.com/guide/topics/ui/notifiers/notifications.html

Notification API : http://developer.android.com/reference/android/app/Notification.html

The magic recipe to set Notification.when : http://stackoverflow.com/questions/2855110/android-no-icon-for-notification

Browse the sources of the sample application : http://bitbucket.org/nicobo/tweaking-android-notifications

SwitchDataSwitch, the app for which I've been doing all this : http://bitbucket.org/nicobo/switchdataswitch

SwitchDataSwitch at xda : http://xdaforums.com/showthread.php?p=18077506#post18077506