How to add an application icon to the status bar when starting my application?

I tried using the Notification.Builder and 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 want my app icon to be added next to the battery icon, wifi icon and 3g icons. Any way to do this? I appreciate your help.

+6
source share
2 answers

You must call the build () method after you finish describing your notification. See the Android link for an example.

Basically, you should 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 allows you to show the icon in the notification panel. If you want it to be saved there, you will need to use FLAG_ONGOING_EVENT

+6
source

You can add the icon of your application for status notification. Try this one

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

Source: https://habr.com/ru/post/946363/


All Articles