29

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?

Red M
  • 2,609
  • 3
  • 30
  • 50
  • 2
    There should be a quick-fix (Alt-Enter with the cursor in the deprecation) to add an annotation to the method that suppresses the deprecation warning. Or, switch to `NotificationCompat.Builder`, particularly when the v26 edition of the Support Library out of beta. – CommonsWare Jun 23 '17 at 19:22
  • 2
    Just switch to `NotificationCompat` – tyczj Jun 23 '17 at 19:40
  • 2
    No, NotificationCompat.Builder(Context context) has been deprecated too with API level O. https://developer.android.com/reference/android/app/Notification.Builder.html – Red M Jun 23 '17 at 19:50
  • 2
    Yeah it was replaced with `NotificationCompat(Context context, String channelId)`. Did you even read your link you posted? – tyczj Jun 23 '17 at 19:56
  • Sorry I posted the wrong link. Here you go https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#NotificationCompat.Builder(android.content.Context, java.lang.String) – Red M Jun 23 '17 at 20:28
  • And my previous statement still stands, use the new constructor in NotificationCompat – tyczj Jun 23 '17 at 20:31
  • One thing that I already mentioned in the open question thread is that after I applied Notification.Builder(context, StringID) and built my APK on an Android O, I did receive a notification. However, after running it on an Android M, I did not receive any notification, hence segregation using if/else condition for OSVersion and wanting to hide the warning from Lint. NotificationCompat is not any different from Notification class in term of functionality. I did not try to replace and run it and see if it work properly on an Android M, but I'm pretty sure it won't work. – Red M Jun 23 '17 at 20:52
  • 2
    Im pretty sure it does since I use it and it works fine so why dont you actually give it a try first instead of saying it wont work? NotificationCompat is built specifically to handle different OS versions – tyczj Jun 23 '17 at 20:58
  • It actually did work after using NotificationCompat instead of Notification. If you want to post it as an answer to the question, I can go ahead and accept it. Thanks again. – Red M Jun 23 '17 at 22:19
  • Interesting what happens when you listen to stuff people say isn't it – tyczj Jun 23 '17 at 23:15
  • I did not doubt you but what I doubted was the fact that the Android documentation did not say anything different between the two classes in terms of that constructor change. – Red M Jun 24 '17 at 00:10
  • Had the same issue. Was solved for me here: https://stackoverflow.com/questions/44443690/notificationcompat-with-api-26 – Mike T Jun 27 '17 at 14:15
  • @tyczj If you can go ahead and post an answer to this question, I can go ahead and accept it since you were the first one who answered it right. – Red M Sep 08 '17 at 14:55
  • Related post - [NotificationCompat.Builder doesn't accept 2nd argument](https://stackoverflow.com/q/50765964/465053) – RBT Sep 03 '18 at 04:45

3 Answers3

45

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.

Wess
  • 779
  • 8
  • 12
  • this should be the right answer – Gatschet Sep 26 '17 at 10:34
  • 6
    what should be the value of `channel ID`? – Narendra Singh Sep 28 '17 at 13:23
  • 3
    Channel IDs are unique static Strings defined by you for each notification channel you created in Oreo. I just pass the appropriate channel ID to the builder as if I'm building the notification for Oreo. If the notification is not being posted on Oreo (and the corresponding channel does not exist) the builder doesn't care which channel ID you gave it, it ignores it so this works for all API levels. – Wess Sep 29 '17 at 06:46
  • 1
    You also need to take care for support version of NotificationCompat Replace support.v7.app.NotificationCompat to support.v4.app.NotificationCompat and after passing channeId param, you will no longer see deprecated inspection. – codepeaker Mar 20 '18 at 11:16
  • Though you still need to check API level, for setting either priority or importance based on API level. – LoveForDroid Aug 02 '18 at 19:17
3

You have to define a unique channelId (for example "MyChannelId_01") and call NotificationCompat.Builder (ctx, "MyChannelId_01"). The constructed Notification will be posted on this NotificationChannel "MyChannelId_01".

This alow you to define importance of the notification (this controls how interruptive notifications posted to this channel are. Value is IMPORTANCE_UNSPECIFIED, IMPORTANCE_NONE, IMPORTANCE_MIN, IMPORTANCE_LOW, IMPORTANCE_DEFAULT or IMPORTANCE_HIGH).

You can find an example here : Creating a notification channel

Christian
  • 748
  • 7
  • 23
2

I had the same issue and since I am targeting android 22 and 24 I just did this: NotificationCompat.Builder notification = new NotificationCompat.Builder(MainActivity.this, "")

I am sure someone will say this is a hack but it gets rid of the warning and I have no issues.

Seems passing an empty string works for < android 26.

Maybe someone else can state if this causes issues for 26.

Thanks