Android SDK 26及以上的通知是否支持26以下的SDK?

Does notification in Android SDK 26 and above supports SDK below 26?

所以我在 Android SDK 26 及更高版本中有以下通知代码: 首先,我有一个包装器 class 来包装整个应用程序,我在此 class:

中声明通知通道
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ) {
        NotificationChannel Channel = new NotificationChannel(
                "channel",
                "myChannel",
                NotificationManager.IMPORTANCE_HIGH
        );


        Channel.setDescription("My Event Channel");

        NotificationManager manager = getSystemService(NotificationManager.class);
        manager.createNotificationChannel(Channel);

    }

并且在点击按钮时调用通知服务:

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
Notification notification = new NotificationCompat.Builder(context,"channel")
                .setSmallIcon(R.drawable.ic_notification)
                .setContentTitle("Notification")
                .setContentText("my notification")
                .setCategory(NotificationCompat.CATEGORY_ALARM)
                .build();

        notificationManager.notify(0,notification);

所以我的问题是,对于低于 26 的 SDK,频道没有被创建。那么上面的代码是否也适用于 26 以下的 SDK?

提前致谢:)

简答:

是的,但有一些小改动。

长答案:

所以我找到了在SDK低于26的设备上测试的详细方法:

  1. 您可以在设置模拟器时配置API级别,如下图:

在上图中,我有 selected Pixel 模拟器,可以 select 安装所需的 API 级别(我的 API等级为 22)

  1. 然后继续安装API和配置模拟器
  2. 启动模拟器后 运行,请注意以下代码:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    NotificationChannel Channel = new NotificationChannel(
                "channel",
                "myChannel",
                NotificationManager.IMPORTANCE_HIGH
        );
   AudioAttributes audioAttributes = new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                .build();
   Uri sound = Uri.parse(
                    ContentResolver.SCHEME_ANDROID_RESOURCE +
                    "://" +
                    getApplicationContext().getPackageName() +
                    "/" +
                    R.raw.void_event
            );


   Channel.setDescription("My Event Channel");
   Channel.setSound(sound, audioAttributes)

        NotificationManager manager =    getSystemService(NotificationManager.class);
        manager.createNotificationChannel(Channel);

    }

上面的代码设置通道优先级(NotificationManager.IMPORTANCE_HIGH)和声音(setSound(sound, audioAttributes))只工作如果您的 API 高于 26.

  1. 您可以做的是,您还可以在要触发通知时添加这些代码行,例如点击按钮或接收推送通知(您正在使用 NotificationCompat.Builder):
// Define sound 
Uri sound = Uri.parse(
                    ContentResolver.SCHEME_ANDROID_RESOURCE +
                            "://" +
                            context.getPackageName() +
                            "/" +
                            R.raw.void_event
            );

`enter code here`NotificationManagerCompat notificationManager =          NotificationManagerCompat.from(context);
    Notification notification = new NotificationCompat.Builder(context,"channel")
// The next 2 lines of code is for adding sound & priority for API < 26
// For devices with API > 26, it just ignores it
                .setSound(sound, AudioManager.STREAM_ALARM)
                .setPriority(NotificationManager.IMPORTANCE_HIGH)
                .setSmallIcon(R.drawable.ic_notification)
                .setContentTitle("Notification")
                .setContentText("my notification")
                .setCategory(NotificationCompat.CATEGORY_ALARM)
                .build();

        notificationManager.notify(0,notification);