android - Notification.Builder(context) 已弃用 Android O

标签 android android-notifications android-8.0-oreo

Notification.Builder(context) 最近在 Android O 中的 Notification Channels 位置已被弃用。

问题:

在使用 Notification.Builder(context, StringID) 而不是 Notification.Builder(context)后,我确实收到了我的 Android O 设备的通知。
但是,在 Android 23 (M) 上尝试之后,我没有收到通知。我调试了我的代码,一旦调试器在 Android 23 (M) 上发布 Notification.Builder(context, StringID) 行,它就会停止执行。

修复:

为了解决这个问题,我使用 if/else 条件在 Android O 设备和其他设备之间进行隔离。

我有以下代码 fragment :

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);
}

Android Studio 中的 Lint 显示以下弃用行:

enter image description here

问题:

有没有办法摆脱弃用警告线?

最佳答案

您的解决方案是使用 NotificationCompat.Builder(Context context, String channelId)。如果您使用它,则无需检查 API 级别,生成器会忽略 pre-Oreo 设备上的 channel ID。

我已经在 API 15、22、23 和 26 上对其进行了测试,并且运行良好。

关于android - Notification.Builder(context) 已弃用 Android O,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44728542/

相关文章:

android - 带有 Fragment 选项卡和 AdMob 的操作栏

android - 分组通知计数 (+1),如 Gmail

c# - Unity制作的Android游戏中的声音问题

Android 10 以下没有互联网的情况下,Android 会自动与 WiFi 断开连接

android - API>25 中的前台服务在应用程序运行(可见)时是否强制通知

android - 将工具栏添加到 FragmentActivity

Android C++ 异常抛出导致 SIGSEGV

java - LG G3 手机的 Android 6.0 HTTPClient 问题

android - NotificationCompat.InboxStyle : How many lines can I add at most?

java - 禁用奥利奥通知点