Notification 是系统级别的;那么操作Notification就需要通过系统的NotificationManager;创建Notification通过构建者模式来创建;Notification.Builder方法只支持Android4.1及以上版本;为了兼容通常使用NotifivationCompat.Builder来创建;
在O版本的系统上面谷歌为了便于管理通知行为和设置推出了一个新的概念:渠道和组,它允许你为要显示的每种通知类型创建自定义的类别;需要对O版本进行单独的适配
1,创建一个消息渠道(通过channelId可以创建不同的渠道)
NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH); // 开启指示灯,如果设备有的话 channel.enableLights(true); // 设置指示灯颜色 channel.setLightColor(ContextCompat.getColor(context, R.color.colorPrimary)); // 是否在久按桌面图标时显示此渠道的通知 channel.setShowBadge(true); // 设置是否应在锁定屏幕上显示此频道的通知 channel.setLockscreenVisibility(NotificationCompat.VISIBILITY_PRIVATE); // 设置绕过免打扰模式 channel.setBypassDnd(true); manager.createNotificationChannel(channel);
2,创建一个组(也就是可以将不同渠道的通知放到一起进行管理)
ArrayList<NotificationChannelGroup> groups = new ArrayList<>(); NotificationChannelGroup group = new NotificationChannelGroup(groupId, groupName); groups.add(group); NotificationChannelGroup group_download = new NotificationChannelGroup(groupDownloadId, groupDownloadName); groups.add(group_download); manager.createNotificationChannelGroups(groups);
3,消息级别的控制
IMPORTANCE_MIN 开启通知,不会弹出,但没有提示音,状态栏中无显示 IMPORTANCE_LOW 开启通知,不会弹出,不发出提示音,状态栏中显示 IMPORTANCE_DEFAULT 开启通知,不会弹出,发出提示音,状态栏中显示 IMPORTANCE_HIGH 开启通知,会弹出,发出提示音,状态栏中显示
4,通知角标(角标和ios的还是有差别的)
channel.setShowBadge(true); // 展示 Notification.Builder builder=new Notification.Builder(this, channelId); builder.setNumber(10); // 设置数字
5,通知超时(设置超时之后;该消息就会取消)
Notification.Builder builder=new Notification.Builder(this, channelId); builder.setTimeoutAfter(timeout*1000)
6,通知清除(可以是用户手动清除;也可以是系统清除;需要手动开启权限)
1)设置>应用和通知>高级>特殊应用权限->通知使用权
2) 继承NotificationListenerService @RequiresApi (api = Build.VERSION_CODES.O) public class RemoveNotificationService extends NotificationListenerService { @Override public void onNotificationPosted(StatusBarNotification sbn, RankingMap rankingMap) { super.onNotificationPosted(sbn, rankingMap); Log.d("onNotificationPosted", sbn.toString()); } @Override public void onNotificationRemoved(StatusBarNotification sbn, RankingMap rankingMap, int reason) { super.onNotificationRemoved(sbn, rankingMap, reason); Log.d("onNotificationRemoved", sbn.toString()); } } 3)清单文件注册 <service android:name = ".service.RemoveNotificationService" android: permission = "android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" > <intent - filter > <action android:name = "android.service.notification.NotificationListenerService" / > </intent - filter > </service >
7,修改通知的背景颜色(必须是前台服务才可以;8.0版本对后台服务进行了比较严格的控制)
builder.setColorized(true); builder.setColor(Color.BLACK);
8,一个简单的适配8.0的例子
RxView.clicks(mActNotifBtn2).throttleFirst(AppConfig.BUTTON_DELAY_TIME, TimeUnit.SECONDS) .subscribe(new Consumer<Object>() { @Override public void accept(Object o) throws Exception { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { String channelId = "subscribe"; String channelName = "订阅消息"; int importance = NotificationManager.IMPORTANCE_DEFAULT; createNotificationChannel(channelId, channelName, importance); } NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Notification notification = new NotificationCompat .Builder(M_NotificationActivity.this, "subscribe") .setContentTitle("收到一条订阅消息") .setContentText("地铁沿线30万商铺抢购中!") .setNumber(2) .setWhen(System.currentTimeMillis()) .setSmallIcon(R.mipmap.co_powerstatistics_comparison) .setLargeIcon(BitmapFactory.decodeResource(getResources() , R.mipmap.co_powerstatistics_comparison)) .setAutoCancel(true) .build(); manager.notify(0, notification); } }); }
/** * @param channelId 渠道id * @param channelName 渠道Name * @param importance 重要等级 * @author changquan * @retrun */ @TargetApi(Build.VERSION_CODES.O) private void createNotificationChannel(String channelId, String channelName, int importance) { NotificationChannel channel = new NotificationChannel(channelId, channelName, importance); NotificationManager notificationManager = (NotificationManager) getSystemService( NOTIFICATION_SERVICE); notificationManager.createNotificationChannel(channel); }