7

I tried to use the Notification.Builder and the Notification classes.

I tried using this code :

Notification notification = new Notification.Builder(this).build();
notification.icon = R.drawable.ic_launcher;
notification.notify();

but it seems useless.

I only want my app's icon to be added next to the battery icon,wifi icon and 3g icons.. Any way to do that? I appreciate your help.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
ARMAGEDDON
  • 939
  • 3
  • 11
  • 23
  • Possible duplicate of [How to show an icon in the status bar when application is running, including in the background?](http://stackoverflow.com/questions/3973208/how-to-show-an-icon-in-the-status-bar-when-application-is-running-including-in) – Ciro Santilli OurBigBook.com Feb 12 '16 at 19:22

2 Answers2

6

You have to call the method build() after you have finished describing your notification. Check out the Android reference for an example.

Basically, you have to change your code to the following:

Context context = getApplicationContext();
NotificationCompat.Builder builder = new NotificationCompat.Builder(context )
.setSmallIcon(R.drawable.ic_launcher);      

Intent intent = new Intent( context, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, mID , intent, 0);
builder.setContentIntent(pIntent);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

Notification notif = builder.build();
mNotificationManager.notify(mID, notif);

Note: this code will only allow to show your icon in the notification bar. If you want it to persist there, you will have to use FLAG_ONGOING_EVENT

Community
  • 1
  • 1
verybadalloc
  • 5,768
  • 2
  • 33
  • 49
  • The previous code was to simply show the proper ordering to show the icon. I edited the answer to reflect your comment. – verybadalloc Jun 03 '13 at 01:33
  • That doesnt add the icon to the right where the battery and signal icons are. – James Heald Mar 20 '14 at 18:15
  • 1
    @JamerTheProgrammer In that case, this question might be useful: http://stackoverflow.com/questions/18350016/how-to-create-a-notification-status-bar-icon-on-the-right-side – verybadalloc Mar 20 '14 at 22:38
0

You can add your app icon for status bar notification. Try this one

Notification notification = new Notification.Builder(this).setSmallIcon(R.mipmap.ic_launcher).build();