Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Notification.Builder(context) deprecated Android O [duplicate]

Notification.Builder(context) has been deprecated recently with the venue of Notification Channels in Android O.

PROBLEM:

After using Notification.Builder(context, StringID) instead of Notification.Builder(context)I did receive a notification to my Android O device.
However, after trying that on an Android 23 (M), I did not receive a notification. I debugged my code and it just stopped executing once the debugger hit the line post Notification.Builder(context, StringID) on Android 23 (M).

FIX:

To Fix this issue, I used if/else condition to segregate between Android O devices and the rest of other devices.

I have the following code snippet:

Notification.Builder notificationBuilder;  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {     notificationBuilder = new Notification.Builder(mContext,             mContext.getResources().getString(R.string.notification_id_channel)); } else {     notificationBuilder = new Notification.Builder(mContext); } 

Lint in Android Studio is displaying the following deprecation line:

enter image description here

QUESTION:

Is there a way to get rid off that deprecation warning line?

like image 899
Red M Avatar asked Jun 23 '17 19:06

Red M


People also ask

What can I use instead of NotificationCompat builder?

This method is deprecated. use Builder(Context, String) instead. All posted notifications must specify a NotificationChannel ID.

How do I make my notification icons smaller?

Set the small icon resource, which will be used to represent the notification in the status bar. Set the small icon, which will be used to represent the notification in the status bar and content view (unless overridden there by a large icon ).

What is Channel ID in Android notification?

ChannelId is a unique String to identify each NotificationChannel and is used in Notification. Builder (line 7) when constructing the Notification object. NotificationChannel settings, except channel name and description text, are immutable after it is submitted to NotificationManager at line 5.


1 Answers

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.

like image 97
Wess Avatar answered Sep 17 '22 08:09

Wess