NotificationCompat.Builder deprecated in Android O NotificationCompat.Builder deprecated in Android O android android

NotificationCompat.Builder deprecated in Android O


It is mentioned in the documentation that the builder method NotificationCompat.Builder(Context context) has been deprecated. And we have to use the constructor which has the channelId parameter:

NotificationCompat.Builder(Context context, String channelId)

NotificationCompat.Builder Documentation:

This constructor was deprecated in API level 26.0.0-beta1. useNotificationCompat.Builder(Context, String) instead. All postedNotifications must specify a NotificationChannel Id.

Notification.Builder Documentation:

This constructor was deprecated in API level 26. useNotification.Builder(Context, String) instead. All postedNotifications must specify a NotificationChannel Id.

If you want to reuse the builder setters, you can create the builder with the channelId, and pass that builder to a helper method and set your preferred settings in that method.


enter image description here

Here is working code for all android versions as of API LEVEL 26+ with backward compatibility.

 NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getContext(), "M_CH_ID");        notificationBuilder.setAutoCancel(true)                .setDefaults(Notification.DEFAULT_ALL)                .setWhen(System.currentTimeMillis())                .setSmallIcon(R.drawable.ic_launcher)                .setTicker("Hearty365")                .setPriority(Notification.PRIORITY_MAX) // this is deprecated in API 26 but you can still use for below 26. check below update for 26 API                .setContentTitle("Default notification")                .setContentText("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")                .setContentInfo("Info");NotificationManager notificationManager = (NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE);notificationManager.notify(1, notificationBuilder.build());

UPDATE for API 26 to set Max priority

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);    String NOTIFICATION_CHANNEL_ID = "my_channel_id_01";    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {        NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_MAX);        // Configure the notification channel.        notificationChannel.setDescription("Channel description");        notificationChannel.enableLights(true);        notificationChannel.setLightColor(Color.RED);        notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});        notificationChannel.enableVibration(true);        notificationManager.createNotificationChannel(notificationChannel);    }    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);    notificationBuilder.setAutoCancel(true)            .setDefaults(Notification.DEFAULT_ALL)            .setWhen(System.currentTimeMillis())            .setSmallIcon(R.drawable.ic_launcher)            .setTicker("Hearty365")       //     .setPriority(Notification.PRIORITY_MAX)            .setContentTitle("Default notification")            .setContentText("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")            .setContentInfo("Info");    notificationManager.notify(/*notification id*/1, notificationBuilder.build());


Call the 2-arg constructor: For compatibility with Android O, call support-v4 NotificationCompat.Builder(Context context, String channelId). When running on Android N or earlier, the channelId will be ignored. When running on Android O, also create a NotificationChannel with the same channelId.

Out of date sample code: The sample code on several JavaDoc pages such as Notification.Builder calling new Notification.Builder(mContext) is out of date.

Deprecated constructors: Notification.Builder(Context context) and v4 NotificationCompat.Builder(Context context) are deprecated in favor of Notification[Compat].Builder(Context context, String channelId). (See Notification.Builder(android.content.Context) and v4 NotificationCompat.Builder(Context context).)

Deprecated class: The entire class v7 NotificationCompat.Builder is deprecated. (See v7 NotificationCompat.Builder.) Previously, v7 NotificationCompat.Builder was needed to support NotificationCompat.MediaStyle. In Android O, there's a v4 NotificationCompat.MediaStyle in the media-compat library's android.support.v4.media package. Use that one if you need MediaStyle.

API 14+: In Support Library from 26.0.0 and higher, the support-v4 and support-v7 packages both support a minimum API level of 14. The v# names are historical.

See Recent Support Library Revisions.