Notificationcompat.Builder Deprecated in Android O

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. use
NotificationCompat.Builder(Context, String) instead. All posted
Notifications must specify a NotificationChannel Id.

Notification.Builder Documentation:

This constructor was deprecated in API level 26. use
Notification.Builder(Context, String) instead. All posted
Notifications 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.

Notification.Builder(context) deprecated Android O

Your solution is to use NotificationCompat.Builder(Context context, String channelId). If you use this you don't have to check the API level, the Builder ignores the channel ID on pre-Oreo devices.

I have tested it on API 15, 22, 23, and 26 and it works perfectly.

NotificationCompat.Builder only has deprecated constructor

Solved it. One of you were correct.

compile 'com.android.support:appcompat-v7:26.+' was not really grabbing version 26. I switched to compile 'com.android.support:appcompat-v7:26.1.0'
and it now works.

setDefaults is deprecated in Notification.Builder

setDefaults should be replaced with use of the following

  • NotificationChannel.enableVibration(boolean)
  • NotificationChannel.enableLights(boolean)
  • NotificationChannel.setSound(Uri, AudioAttributes)

source

setSound should be replaced with use of

  • NotificationChannel.setSound(Uri, AudioAttributes)

source

NotificationCompat.Builder is deprecated

Creating a notification channel is not that hard.
Here is some code: https://developer.android.com/training/notify-user/channels#CreateChannel

You can execute that code in your MainActivity or your Application class on app startup.

Once you created the channel id, you can use it with
NotificationCompat.Builder(Context context, String channelId)

EDIT

The CHANNEL_ID is a simple String.
You can for example add this to a constants file:

public static final String CHANNEL_ID = "your.package.name.notificationChannelId"

You can now use this constants hwen creating your notification channel and when calling NotificationCompat.Builder(Context context, String channelId)

Replacement for the obsolete Notification.Builder.SetPriority()?


This method was deprecated in API level 26.
use setImportance(int) instead.

SetImportance is contained in API26 within a NotificationChannel and can be set as a property on the channel, or within the NotificationChannel constructor:

channel.Importance = NotificationImportance.High

or

channel = new NotificationChannel(myUrgentChannel, channelName, NotificationImportance.High);

Conditional API Notification Example:

var title = "Note from Sushi";
var message = "StackOverflow is a really great source of information";
using (var notificationManager = NotificationManager.FromContext(BaseContext))
{
Notification notification;
if (Android.OS.Build.VERSION.SdkInt < Android.OS.BuildVersionCodes.O)
{
notification = new Notification.Builder(BaseContext)
.SetContentTitle(title)
.SetContentText(message)
.SetAutoCancel(true)
.SetPriority(1)
.SetSmallIcon(Resource.Drawable.icon)
.SetDefaults(NotificationDefaults.All)
.Build();
}
else
{
var myUrgentChannel = PackageName;
const string channelName = "SushiHangover Urgent";

NotificationChannel channel;
channel = notificationManager.GetNotificationChannel(myUrgentChannel);
if (channel == null)
{
channel = new NotificationChannel(myUrgentChannel, channelName, NotificationImportance.High);
channel.EnableVibration(true);
channel.EnableLights(true);
channel.LockscreenVisibility = NotificationVisibility.Public;
notificationManager.CreateNotificationChannel(channel);
}
channel?.Dispose();

notification = new Notification.Builder(BaseContext)
.SetChannelId(myUrgentChannel)
.SetContentTitle(title)
.SetContentText(message)
.SetAutoCancel(true)
.SetSmallIcon(Resource.Drawable.icon)
.Build();
}
notificationManager.Notify(1331, notification);
notification.Dispose();
}

Constructor of `NotificationCompat.MessagingStyle(String userDisplayName)` is deprecated

For details check this: https://medium.com/google-developer-experts/exploring-android-p-enhanced-notifications-a9adb8d78387

val sender = Person.Builder()
.setName(R.string.user_name)
.build()

Then use this as

NotificationCompat.MessagingStyle(sender)
.addMessage("Check this out!", Date().time, sender)
.setBuilder(notificationBuilder)

NotificationCompat v7 and Android O

NotificationCompat v7 is now deprecated, you should use NotificationCompat v4 (according to the comments on NotificationCompat v7 class ).

/**
* @deprecated Use the static classes in {@link android.support.v4.app.NotificationCompat}.
*/

Then you can build your notification (Kotlin):

val notificationBuilder = NotificationCompat.Builder(context, "your_notification_channel_name")                     
.setContentTitle("title")
[...]

Note: the latest support version is "com.android.support:appcompat-v7:26.0.0"



Related Topics



Leave a reply



Submit