说起Android手机的 Notification,是真的痛苦,看到那些经常推送的的东西,如果不是我感兴趣或者重要的,我基本上会划掉不让他们通知。还好从Android8.0开始Google引入了通知渠道这个概念,其实就是开发者定义相应的渠道,用户自由的去控制渠道 ,下面图就是google map 在Android8.0手机上的Notification。我们可以看出这里面划分很详细,用户可以自己去选择想要接收的Notification。(可怜我最近没有8.0手机,只能用用模拟器了)适配
- 检查targetSdkVersion已经指定到了26或者更高,如图所示:
- 创建通知渠道:
//判断版本 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { String channelId = "attention";//唯一的标志id String channelName = "关注消息";//给用户看的, int importance = NotificationManager.IMPORTANCE_HIGH;//通知的等级,用户也可以修改 createChannel(channelId, channelName, importance); channelId = "comment"; channelName = "评论消息"; importance = NotificationManager.IMPORTANCE_DEFAULT; createChannel(channelId, channelName, importance); channelId = "news"; channelName = "新闻消息"; importance = NotificationManager.IMPORTANCE_LOW; createChannel(channelId, channelName, importance); } } @TargetApi(Build.VERSION_CODES.O) private void createChannel(String channelId, String channelName, int importance) { //开始创建 NotificationChannel channel = new NotificationChannel(channelId, channelName, importance); NotificationManager notificationManager = (NotificationManager) getSystemService( NOTIFICATION_SERVICE); notificationManager.createNotificationChannel(channel); }
创建完成,运行后,可以在设置通知的地方看到我们设置的渠道,可以手动去更改:
- 显示通知
private void sendAttention() { NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); //注意第二个参数为channelId,必须加上和以前的版本有些区别 Notification notification = new NotificationCompat.Builder(this, "attention") .setContentTitle("关注通知") .setContentText("mst关注了你") .setWhen(System.currentTimeMillis()) .setSmallIcon(R.mipmap.ic_launcher) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)) .setAutoCancel(true) .build(); notificationManager.notify(1, notification); } private void sendComment() { NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); //注意第二个参数为channelId,必须加上和以前的版本有些区别 Notification notification = new NotificationCompat.Builder(this, "comment") .setContentTitle("评论通知") .setContentText("你最近还好吗?") .setWhen(System.currentTimeMillis()) .setSmallIcon(R.mipmap.ic_launcher) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)) .setAutoCancel(true) .build(); notificationManager.notify(2, notification); } private void sendNews() { NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); //注意第二个参数为channelId,必须加上和以前的版本有些区别 Notification notification = new NotificationCompat.Builder(this, "news") .setContentTitle("今日要点") .setContentText("明天世界杯揭幕战") .setWhen(System.currentTimeMillis()) .setSmallIcon(R.mipmap.ic_launcher) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)) .setAutoCancel(true) .build(); notificationManager.notify(3, notification); }
从下图可以看出只会在设置优先级高的时候notication才会弹出来,其他的则出现在通知栏,快速滑动通知栏可以关闭一条通知,但是在Android8.0中缓慢地向左或者向右滑动,就会看到这样两个按钮(好像和我当时用的华为手机不太一样),一个是暂缓通知我一会再看,一个是设置用来控制notification
重温Android8.0系统的通知栏适配
猜你喜欢
转载自blog.csdn.net/qq_30797001/article/details/80675819
今日推荐
周排行