Android O 中不推荐使用 NotificationCompat.Builder

新手上路,请多包涵

将我的项目升级到 Android O

buildToolsVersion "26.0.1"

Android Studio 中的 Lint 显示以下通知生成器方法已弃用的警告:

 new NotificationCompat.Builder(context)

问题是: Android 开发人员更新了他们描述 NotificationChannel 的文档,以支持 Android O 中的通知,并为我们提供了一个片段,但同样有弃用的警告:

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

通知概述

我的问题: 是否有任何其他构建通知的解决方案,并且仍然支持 Android O?

我找到的一个解决方案是在 Notification.Builder 构造函数中将通道 ID 作为参数传递。但是这个解决方案并不是完全可重用的。

 new Notification.Builder(MainActivity.this, "channel_id")

原文由 GuilhermeFGL 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 660
2 个回答

文档中提到,构建器方法 NotificationCompat.Builder(Context context) 已被弃用。我们必须使用具有 channelId 参数的构造函数:

 NotificationCompat.Builder(Context context, String channelId)

NotificationCompat.Builder 文档:

此构造函数在 API 级别 26.0.0-beta1 中已弃用。改用 NotificationCompat.Builder(Context, String)。所有发布的通知都必须指定一个 NotificationChannel Id。

Notification.Builder 文档:

此构造函数在 API 级别 26 中已弃用。请改用 Notification.Builder(Context, String)。所有发布的通知都必须指定一个 NotificationChannel Id。

如果您想重用构建器设置器,您可以使用 channelId 创建构建器,并将该构建器传递给辅助方法并在该方法中设置您的首选设置。

原文由 Bob 发布,翻译遵循 CC BY-SA 4.0 许可协议

我构建了这段代码,它允许您向 android api level < 26 或 api level >= 26 显示通知

  private void showNotifcation(String title, String body) {
        //Este método muestra notificaciones compatibles con Android Api Level < 26 o >=26
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //Mostrar notificacion en Android Api level >=26
            final String CHANNEL_ID = "HEADS_UP_NOTIFICATIONS";
            NotificationChannel channel = new NotificationChannel(
                    CHANNEL_ID,
                    "MyNotification",
                    NotificationManager.IMPORTANCE_HIGH);

            getSystemService(NotificationManager.class).createNotificationChannel(channel);
            Notification.Builder notification = new Notification.Builder(this, CHANNEL_ID)
                    .setContentTitle(title)
                    .setContentText(body)
                    .setSmallIcon(R.drawable.ic_launcher_background)
                    .setAutoCancel(true);
            NotificationManagerCompat.from(this).notify(1, notification.build());

        }else{
            //Mostrar notificación para Android Api Level Menor a 26
            String NOTIFICATION_CHANNEL_ID = "my_channel_id_01";
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
                    .setContentTitle(title)
                    .setContentText(body)
                    .setSmallIcon(R.drawable.ic_launcher_background)
                    .setAutoCancel(true);

            NotificationManager notificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(/*notification id*/1, notificationBuilder.build());

        }

    }

干杯!

原文由 FreddicMatters 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题