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

旧巷老猫 提交于 2019-12-03 09:33:14

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.

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!