Android(安卓)通知栏显示与设置
16lz
2021-01-24
Android通知栏
Android 基础设置和发送通知在通知栏显示。
Android 版本通知适配
Android 8.0 及以上使用通知都必须设置 NotificationChannel 类才能正常在通知栏弹出通知,只需 Application 中设置一次就可以了。 当 NotificationChannel 设置成功后具体可以在手机系统(设置 / 通知和状态栏 / 自身应用 / 通知类别)里看到详细的设置介绍。
Android 8.0 以下可直接创建通知然后发送,并不需要设置 NotificationChannel 类。
设置 NotificationChannel 代码
在继承了 Application 的类中的 onCreate() 中进行设置。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {// 通知渠道的id(后台推送则与后台设置的Id保持一致和创建通知时传入的ChannelId保持一致) String channelId = "1"; // 用户可以看到的通知渠道的名字. String name = "新通知"; // 用户可以看到的通知渠道的描述 String description = "新通知描述"; // 该通知的重要级别 int importance = NotificationManager.IMPORTANCE_HIGH; // 创建渠道 NotificationChannel mChannel = new NotificationChannel(channelId, name, importance); // 配置通知渠道的属性 mChannel.setDescription(description); // 设置通知出现时的闪灯(如果 android 设备支持的话) mChannel.enableLights(true); // 设置桌面图标右上角通知提示的颜色 mChannel.setLightColor(Color.RED); // 设置通知出现时的震动(如果 android 设备支持的话) mChannel.enableVibration(true); // 设置发布到此频道的通知的振动模式 mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400}); // 是否在久按桌面图标时显示此渠道的通知 mChannel.setShowBadge(true); NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);if (mNotificationManager != null) {// 创建单个渠道mNotificationManager.createNotificationChannel(mChannel);// 创建多个渠道// List list = new ArrayList<>(); // list.add(mChannel);// mNotificationManager.createNotificationChannels(list); }}
设置通知栏代码
当接收到服务器的推送或自身应用服务发出命令后,则创建一个通知并在通知栏弹出提示。
Notification notification;if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {Notification.Builder builder = new Notification.Builder(context, channelId); builder.setSmallIcon(R.mipmap.ic_launcher_round)//设置通知小图标在状态栏显示(必须设置) //设置通知大图标,在通知栏显示 .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher)) //通知产生的时间,会在通知信息里显示,一般是系统获取到的时间 .setWhen(System.currentTimeMillis()) //设置该通知优先级 .setPriority(Notification.PRIORITY_HIGH) //通知首次出现在通知栏,带上升动画效果的 .setTicker(message.getTitle()) //设置通知栏标题 .setContentTitle(message.getTitle()) //设置通知栏内容 .setContentText(message.getContent()) //设置通知出现时的震动具体值 .setVibrate(new long[0]) //ture,设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接) .setOngoing(false) //设置这个标志当用户单击面板就可以让通知将自动取消 .setAutoCancel(true); //创建PendingIntent,处理点击通知之后的逻辑 Intent intent = new Intent(context, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); builder.setContentIntent(pendingIntent); notification = builder.build();} else {NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context); mBuilder.setContentTitle(message.getTitle())//设置通知栏标题 //通知首次出现在通知栏,带上升动画效果的 .setTicker("通知来啦") //通知产生的时间,会在通知信息里显示,一般是系统获取到的时间 .setWhen(System.currentTimeMillis()) //设置该通知优先级 .setPriority(NotificationCompat.PRIORITY_HIGH) //设置通知栏内容 .setContentText(message.getContent()) //设置通知大图标,在通知栏显示 .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher)) //设置这个标志当用户单击面板就可以让通知将自动取消 .setAutoCancel(true) //ture,设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接) .setOngoing(false) //设置通知小图标在状态栏显示(必须设置) .setSmallIcon(R.mipmap.ic_launcher_round);//设置通知小ICON Intent intent = new Intent(context, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); mBuilder.setContentIntent(pendingIntent); notification = mBuilder.build();}NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);if (notificationManager != null) {notificationManager.notify("1", notification);}
更多相关文章
- Android(安卓)后台服务简要概述
- Android(安卓)Activity生命周期(Android艺术开发探索读书笔记)
- Android(安卓)通知使用权(NotificationListenerService)的使用
- Android(安卓)Studio的gradle配置
- (转载)Android下Affinities和Task
- Android中称为四大组件
- Android第十三期 - 百度云推送(百度后台版)
- Android中AVD(Android(安卓)Virtual Device)不能启动的处理方法
- android拔掉耳机后音乐自动暂停