android 8.0通知新增了NotificationChannel(通知渠道),用来帮助管理用户通知。Android Studio新版会默认使用新版本的SDK编译项目,若是App的targetSDKVersion >=26或者Android Studio默认使用高于26的sdk做为targetSDK时,没有使用NotificationChannel就会出如今Android 8.0及以上系统通知没法展现的状况。java
Android O 引入了 通知渠道(Notification Channels),以提供统一的系统来帮助用户管理通知,若是是针对 android O 为目标平台时,必须实现一个或者多个通知渠道,以向用户显示通知。好比聊天软件,为每一个聊天组设置一个通知渠道,指定特定声音、灯光等配置。android
建立NotificationChannel对象时,除了构造方法里面的参数,其他参数是可选的,若是不设置会使用系统默认的,且部分参数由于手机厂商不一样,可能没有效果好比设置桌面Launcher消息角标web
public static String CALENDAR_ID = "channel_01"; private static Notification createNotification(Context context, NotificationManager notificationManager) { Notification notification;NotificationCompat.Builder builder; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel notificationChannel = new NotificationChannel(CALENDAR_ID, "123", NotificationManager.IMPORTANCE_DEFAULT); // 设置渠道描述 notificationChannel.setDescription("测试通知组"); // 是否绕过请勿打扰模式 notificationChannel.canBypassDnd(); // 设置绕过请勿打扰模式 notificationChannel.setBypassDnd(true); // 桌面Launcher的消息角标 notificationChannel.canShowBadge(); // 设置显示桌面Launcher的消息角标 notificationChannel.setShowBadge(true); // 设置通知出现时声音,默认通知是有声音的 notificationChannel.setSound(null, null); // 设置通知出现时的闪灯(若是 android 设备支持的话) notificationChannel.enableLights(true); notificationChannel.setLightColor(Color.RED); // 设置通知出现时的震动(若是 android 设备支持的话) notificationChannel.enableVibration(true); notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400}); notificationManager.createNotificationChannel(notificationChannel); } builder = new NotificationCompat.Builder(context, CALENDAR_ID); PendingIntent pendingIntent = PendingIntent.getActivity(context, 11, new Intent(context, SecondActivity.class), 0); notification = builder.setSmallIcon(R.mipmap.ic_launcher_round) .setContentIntent(pendingIntent) // .setChannelId(CALENDAR_ID) .build(); return notification; }
PS:推荐使用v4包下面的NotificationCompat.Builder来建立Notification,兼容性和新特性都能使用到,建立Notification.Builder的时候推荐使用双参数的构造方法 Builder(@NonNull Context context, @NonNull String channelId)
,单参数的构造方法已经被弃用,可能会由于没有配置ChannelId致使通知发送失败,即网上有的开发者说的Android 8.0通知不显示的问题svg
通知的重要级别在Android 8.0 上下的设置方式、命名和值都不同,好比一样是DEFAULT,Android 8.0里面是IMPORTANCE_DEFAULT(IMPORTANCE_),值是3,Android 7.0里面是PRIORITY_DEFAULT(PRIORITY_),值是0;
Android 7.0设置方法测试
builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
Android 8.0是经过NotificationChannel构造方法传入ui
NotificationChannel notificationChannel = new NotificationChannel(CALENDAR_ID, "123", NotificationManager.IMPORTANCE_DEFAULT); notificationManager.createNotificationChannel(notificationChannel);
下面经过表格说明Android 8.0及以上与Android 8.0如下的通知用户级别的关系spa
用户通知级别 | Android 8.0及以上 | Android 7.0及如下 |
---|---|---|
紧急级别(发出通知声音并显示为提示通知) | IMPORTANCE_HIGH | PRIORITY_HIGH或者PRIORITY_MAX |
高级别(发出通知声音而且通知栏有通知 | IMPORTANCE_DEFAULT | PRIORITY_DEFAULT |
中等级别(没有通知声音但通知栏有通知) | IMPORTANCE_LOW | PRIORITY_LOW |
低级别(没有通知声音也不会出如今状态栏上) | IMPORTANCE_MIN | PRIORITY_MIN |
根据表格显示,级别是高级及以上级别时,系统是会发出声音的,无论你有没有设置提示语音。code
参考连接:Android O(8.0) Notification解决方案xml