After upgrading my project to Android O
buildToolsVersion "26.0.1"
Lint in Android Studio is showing a deprecated warning for the follow notification builder method:
new NotificationCompat.Builder(context)
The problem is: Android Developers update their Documentation describing NotificationChannel to support notifications in Android O, and provide us with a snippet, yet with the same deprecated warning:
Notification notification = new Notification.Builder(MainActivity.this)
.setContentTitle("New Message")
.setContentText("You've received new messages.")
.setSmallIcon(R.drawable.ic_notify_status)
.setChannelId(CHANNEL_ID)
.build();
My question: Is there is any other solution for building notification, and still support Android O?
A solution I found is to pass the channel ID as a parameter in Notification.Builder constructor. But this solution is not exactly reusable.
new Notification.Builder(MainActivity.this, "channel_id")
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.