How to create a notification with NotificationCompat.Builder?

Do I need to create a simple notification that will be displayed in the notification panel along with the sound and icon, if possible? I also need it to be compatible with Android 2.2, so I found that NotificationCompat.Builder works with all the APIs above 4. If there is a better solution, feel free to mention it.

+49
android notifications android-notifications android-notification-bar
Dec 16 '12 at 14:15
source share
8 answers

NotificationCompat.Builder is the easiest way to create Notifications for all versions of Android. You can even use the features available with Android 4.1. If your application works on devices with Android> = 4.1, new functions will be used, if they are launched on Android <4.1, the notification will be a simple old notification.

To create a simple notification, just follow (see Android API Guide for notifications ):

 NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.notification_icon) .setContentTitle("My notification") .setContentText("Hello World!") .setContentIntent(pendingIntent); //Required on Gingerbread and below 

You must set at least smallIcon , contentTitle and contentText . If you miss one, a notification will not be displayed.

Beware: In Gingerbread and below you must set the content intent, otherwise there will be an IllegalArgumentException .

To create an intent that does nothing, use:

 final Intent emptyIntent = new Intent(); PendingIntent pendingIntent = PendingIntent.getActivity(ctx, NOT_USED, emptyIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

You can add sound through the builder, i.e. Sound from RingtoneManager:

 mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)) 

The notification is added to the panel through the NotificationManager:

 NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(mId, mBuilder.build()); 
+119
Dec 16
source share

Working example:

  Intent intent = new Intent(ctx, HomeActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder b = new NotificationCompat.Builder(ctx); b.setAutoCancel(true) .setDefaults(Notification.DEFAULT_ALL) .setWhen(System.currentTimeMillis()) .setSmallIcon(R.drawable.ic_launcher) .setTicker("Hearty365") .setContentTitle("Default notification") .setContentText("Lorem ipsum dolor sit amet, consectetur adipiscing elit.") .setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_SOUND) .setContentIntent(contentIntent) .setContentInfo("Info"); NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(1, b.build()); 
+23
Dec 31 '15 at 13:30
source share

I am doing this method and working perfectly. (tested in android 6.0.1)

 public void notifyThis(String title, String message) { NotificationCompat.Builder b = new NotificationCompat.Builder(this.context); b.setAutoCancel(true) .setDefaults(NotificationCompat.DEFAULT_ALL) .setWhen(System.currentTimeMillis()) .setSmallIcon(R.drawable.favicon32) .setTicker("{your tiny message}") .setContentTitle(title) .setContentText(message) .setContentInfo("INFO"); NotificationManager nm = (NotificationManager) this.context.getSystemService(Context.NOTIFICATION_SERVICE); nm.notify(1, b.build()); } 
+8
Feb 08 '17 at 20:33
source share

Notification in depth

THE CODE

 Intent intent = new Intent(this, SecondActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context) .setSmallIcon(R.drawable.your_notification_icon) .setContentTitle("Notification Title") .setContentText("Notification ") .setContentIntent(pendingIntent ); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(0, mBuilder.build()); 

Depth of knowledge

Notification can be built using Notification. Classes Builder or NotificationCompat.Builder.
But if you need backward compatibility, you should use the NotificationCompat.Builder class, as it is part of the v4 support library, as it takes care of hard work to ensure consistent appearance and functionality of Notification for API 4 and higher.

Basic notification features

The notification has 4 main properties (3 main display properties + 1 click action property)

  • Small icon
  • title
  • Text
  • Button Click Event (Click Click Notification Event)

A button click event becomes optional on Android 3.0 and above. This means that you can create your notification using only the properties of the screen if your minSdk is oriented to Android 3.0 or higher. But if you want your notification to run on older devices than Android 3.0, you must provide a Click event, otherwise you will see an IllegalArgumentException.

Notification display

Notifications are displayed by calling the notify () method of the NotificationManger class

notify () options

There are two options for the notification method.

 notify(String tag, int id, Notification notification) 

or

 notify(int id, Notification notification) 

The notify method accepts an integer identifier to uniquely identify your notification. However, you can also provide an optional String tag to further identify your notification in the event of a conflict.

This type of conflict is rare, but let's say you created a library, and other developers use your library. Now they create their own notification, and somehow your notification and the notification identifier of another developer are the same, then you will run into a conflict.

Notification after API 11 (more control)

API 11 provides additional control over the behavior of notifications

  • Dismissal notice
    By default, if a user clicks on a notification, he executes the designated click event, but does not delete the notification. If you want your notification to be cleared then you must add this

    mBuilder.setAutoClear (true);

  • Prevent user from rejecting notifications
    The user can also reject the notification by posting it. You can disable this default behavior by adding it when creating a notification

    mBuilder.setOngoing (true);

  • Post Notification
    You can set the relative priority of your notification.

    mBuilder.setOngoing (int pri);

If your application runs with a lower API than 11, then your notification will work without the above additional features. This is the advantage of choosing NotificationCompat.Builder over Notification.Builder.

Notification after API 16 (more informative)

With the introduction of API 16, notifications have received many new features.
Notification can be much more informative.
You can add a large picture to your logo. Say you received a message from a person now with mBuilder.setLargeIcon (Bitmap), which you can display a photograph of that person. Thus, in the status bar you will see an icon, when scrolling you will see a photo of a person instead of an icon. There are other features.

  • Add counter in notification
  • Ticker message when you see a notification for the first time
  • Expandable notification
  • Multiline notification, etc.
+2
Mar 09 '18 at 4:51
source share

You can try this code, this works fine for me:

  NotificationCompat.Builder mBuilder= new NotificationCompat.Builder(this); Intent i = new Intent(noti.this, Xyz_activtiy.class); PendingIntent pendingIntent= PendingIntent.getActivity(this,0,i,0); mBuilder.setAutoCancel(true); mBuilder.setDefaults(NotificationCompat.DEFAULT_ALL); mBuilder.setWhen(20000); mBuilder.setTicker("Ticker"); mBuilder.setContentInfo("Info"); mBuilder.setContentIntent(pendingIntent); mBuilder.setSmallIcon(R.drawable.home); mBuilder.setContentTitle("New notification title"); mBuilder.setContentText("Notification text"); mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)); NotificationManager notificationManager= (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(2,mBuilder.build()); 
+1
May 14 '17 at 6:32 a.m.
source share

easy way to make notifications

  NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(this) .setSmallIcon(R.mipmap.ic_launcher) //icon .setContentTitle("Test") //tittle .setAutoCancel(true)//swipe for delete .setContentText("Hello Hello"); //content NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(1, builder.build() ); 
+1
May 31 '18 at 23:06
source share

Use this code

  Intent intent = new Intent(getApplicationContext(), SomeActvity.class); PendingIntent pIntent = PendingIntent.getActivity(getApplicationContext(), (int) System.currentTimeMillis(), intent, 0); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext()) .setSmallIcon(R.drawable.your_notification_icon) .setContentTitle("Notification title") .setContentText("Notification message!") .setContentIntent(pIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(0, mBuilder.build()); 
0
Jul 17 '17 at 12:00
source share

Show Notificaton in Android 8.0

 @TargetApi(Build.VERSION_CODES.O) @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN) public void show_Notification(){ Intent intent=new Intent(getApplicationContext(),MainActivity.class); String CHANNEL_ID="MYCHANNEL"; NotificationChannel notificationChannel=new NotificationChannel(CHANNEL_ID,"name",NotificationManager.IMPORTANCE_LOW); PendingIntent pendingIntent=PendingIntent.getActivity(getApplicationContext(),1,intent,0); Notification notification=new Notification.Builder(getApplicationContext(),CHANNEL_ID) .setContentText("Heading") .setContentTitle("subheading") .setContentIntent(pendingIntent) .addAction(android.R.drawable.sym_action_chat,"Title",pendingIntent) .setChannelId(CHANNEL_ID) .setSmallIcon(android.R.drawable.sym_action_chat) .build(); NotificationManager notificationManager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.createNotificationChannel(notificationChannel); notificationManager.notify(1,notification); } 
0
Jan 30 '19 at 18:13
source share



All Articles