1、Android O(8.0)通知的改变
NotificationChannel是android8.0新增的特性,如果App的targetSDKVersion>=26,没有设置channel通知渠道的话,就会导致通知无法展示。
Android O 引入了 通知渠道(Notification Channels),以提供统一的系统来帮助用户管理通知,如果是针对 android O 为目标平台时,必须实现一个或者多个通知渠道,以向用户显示通知。比如聊天软件,为每个聊天组设置一个通知渠道,指定特定声音、灯光等配置。
2、Android Channel通知的兼容支配
创建NotificationChannel
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// 如果该channel已经存在,则可以不用再次创建
NotificationChannel channel = new NotificationChannel("channel_id", "channel_name",
NotificationManager.IMPORTANCE_DEFAULT);
//是否绕过请勿打扰模式
channel.canBypassDnd();
//闪光灯
channel.enableLights(true);
//锁屏显示通知
channel.setLockscreenVisibility(VISIBILITY_SECRET);
//闪关灯的灯光颜色
channel.setLightColor(Color.RED);
//桌面launcher的消息角标
channel.canShowBadge();
//是否允许震动
channel.enableVibration(true);
//获取系统通知响铃声音的配置
channel.getAudioAttributes();
//获取通知取到组
channel.getGroup();
//设置可绕过 请勿打扰模式
channel.setBypassDnd(true);
//设置震动模式
channel.setVibrationPattern(new long[]{100, 100, 200});
//是否会有灯光
channel.shouldShowLights();
// 如果该channel已经存在,则可以不用再次创建
manager.createNotificationChannel(channel);
}
// 注意:此处channel_id必须是一个已存在的channel id;否则无法显示通知
Notification notification = new NotificationCompat.Builder(this, "channel_id")
.setContentTitle("Title").setContentText("This is content").setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher_round)).build();
manager.notify(0, notification);
3、设置通知重要性级别
该级别必须要在 NotificationChannel 的构造函数中指定,总共要五个级别;范围是从 NotificationManager.IMPORTANCE_NONE(0) ~ NotificationManager.IMPORTANCE_HIGH(4)
,如果要支持 Android 7.1(API 25)及以下的设备,还得调用NotificationCompat 的 setPriority 方法来设置,如下所示
builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
Android 8.0 及以上是使用NotificationManager.IMPORTANCE_,Android 7.1 及以下是使用NotificationCompat.PRIORITY_它们都是定义的常量;下面我们以表格的形式更好的展示出来。
用户通知级别 | Android8.0及以上 | Android7.0及以下 |
---|---|---|
紧急级别(发出通知声音并显示为提示通知) | IMPORTANCE_HIGH | PRIORITY_HIGH或者PRIORITY_MAX |
高级别(发出通知声音并且通知栏有通知) | IMPORTANCE_DEFAULT | PRIORITY_DEFAULT |
中等级别(没有通知声音但通知栏有通知) | IMPORTANCE_LOW | PRIORITY_LOW |
低级别(没有通知声音也不会出现在状态栏上) | IMPORTANCE_MIN | PRIORITY_MIN |
上面这些通知级别用户都是可以在 Channel 设置中更改
4、自定义Channel通知栏
private void sendCustomNotification() {
NotificationCompat.Builder builder = getNotificationBuilder();
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.layout_custom_notifition);
remoteViews.setTextViewText(R.id.notification_title, "custom_title");
remoteViews.setTextViewText(R.id.notification_content, "custom_content");
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, -1,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.turn_next, pendingIntent);
builder.setCustomContentView(remoteViews);
getNotificationManager().notify(3, builder.build());
}
5、判断通知是否开启
Api24以上,NotificationManagerCompat中提供了areNotificationsEnabled()方法。该方法中已经对API19以下,API19-24,API24以上,这三种情况做了判断。直接使用其返回值即可。返回true表示打开了消息通知,如果返回false则没有打开。
private boolean isNotificationEnabled(Context context) {
boolean isOpened = false;
try {
isOpened = NotificationManagerCompat.from(context).areNotificationsEnabled();
} catch (Exception e) {
e.printStackTrace();
}
return isOpened;
}
6、跳转通知设置界面
如果用户没有开启通知,则可以使用下面的代码跳转到通知设置界面
private void gotoSet() {
Intent intent = new Intent();
if (Build.VERSION.SDK_INT >= 26) {
// android 8.0引导
intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
intent.putExtra("android.provider.extra.APP_PACKAGE", getPackageName());
} else if (Build.VERSION.SDK_INT >= 21) {
// android 5.0-7.0
intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
intent.putExtra("app_package", getPackageName());
intent.putExtra("app_uid", getApplicationInfo().uid);
} else {
// 其他
intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
intent.setData(Uri.fromParts("package", getPackageName(), null));
}
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}