# Notification **Repository Path**: haojie_xuexi/Notification ## Basic Information - **Project Name**: Notification - **Description**: 简单的通知 带有意图的通知 大图通知 长文本通知 自定义View通知 带有进度条的通知 - **Primary Language**: Android - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2018-09-15 - **Last Updated**: 2021-11-09 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Notification #### 项目介绍 简单的通知 带有意图的通知 大图通知 长文本通知 自定义View通知 带有进度条的通知 # Notification 的使用 ## 显示最基本的通知,以及详细的配置信息 ``` public static int NOTIFY_ID = 321; private static final String NOTIFY_CHANNEL_ID = "OK_CHANNEL_ID"; private static final String NOTIFY_CHANNEL_NAME = "OK_CHANNEL_NAME"; public void test1(View view) { //Step1:获取通知管理器 NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); //在Android(O) 8.0 及以上,需要做以下配置 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (manager != null) { NotificationChannel notificationChannel = new NotificationChannel(NOTIFY_CHANNEL_ID, NOTIFY_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH); manager.createNotificationChannel(notificationChannel); } } //NotificationManagerCompat manager = NotificationManagerCompat.from(this); if (manager != null) { //Step3: 构造 Notification 对象 Notification notification = new NotificationCompat.Builder(this, NOTIFY_CHANNEL_ID) //设置通知的 小图标(必须设置,否则通知不显示) .setSmallIcon(R.mipmap.qq2) //通知标题 .setContentTitle("QQ消息通知") //设置 通知 显示的时间 .setWhen(System.currentTimeMillis() - 3600000L) //通知的内容 .setContentText("小丽,今晚哪里约!") //在5.0之前的版本中,当通知刚到达时,在状态栏显示 .setTicker("QQ消息:小丽叫你今晚约!") //设置 通知 来时的触发(铃声\震动\闪光) .setDefaults(Notification.DEFAULT_ALL) //构造Notification对象 .build(); // notification.flags |= Notification.FLAG_NO_CLEAR; //让通知不能滑动取消 // notification.flags |= Notification.FLAG_INSISTENT; //重复发出 通知的声音/震动 直到用户点击该通知 // notification.flags |= Notification.FLAG_ONGOING_EVENT; //让通知 显示 正在运行 notification.flags |= Notification.FLAG_AUTO_CANCEL; //让通知被点击之后自动取消掉 // //Step2:唤醒通知, 每个 通知的ID 是唯一的.如果ID一样,则通知栏不会创建新的通知,会重用原来的通知 manager.notify(NOTIFY_ID++, notification); } } ``` ## 带有意图的通知 ``` //Step1:获取通知管理器 NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); //在Android(O) 8.0 及以上,需要做以下配置 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (manager != null) { NotificationChannel notificationChannel = new NotificationChannel(NOTIFY_CHANNEL_ID, NOTIFY_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH); manager.createNotificationChannel(notificationChannel); } } //NotificationManagerCompat manager = NotificationManagerCompat.from(this); if (manager != null) { //创建 PendingIntent 跳转到 Activity Intent intent = new Intent(this, DetailActivity.class); intent.putExtra("key", "阿里斯顿富欧式风dal;ksjf;阿里山叠加开发;阿里上看到肌肤"); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); //Step3: 构造 Notification 对象 Notification notification = new NotificationCompat.Builder(this, NOTIFY_CHANNEL_ID) //设置通知的 小图标(必须设置,否则通知不显示) .setSmallIcon(R.mipmap.qq2) //通知标题 .setContentTitle("QQ消息通知") //设置 通知 显示的时间 .setWhen(System.currentTimeMillis() - 3600000L) //通知的内容 .setContentText("小丽,今晚哪里约!") //在5.0之前的版本中,当通知刚到达时,在状态栏显示 .setTicker("QQ消息:小丽叫你今晚约!") //设置 通知 来时的触发(铃声\震动\闪光) .setDefaults(Notification.DEFAULT_ALL) //添加意图对象 .setContentIntent(pendingIntent) //构造Notification对象 .build(); // notification.flags |= Notification.FLAG_NO_CLEAR; //让通知不能滑动取消 // notification.flags |= Notification.FLAG_INSISTENT; //重复发出 通知的声音/震动 直到用户点击该通知 // notification.flags |= Notification.FLAG_ONGOING_EVENT; //让通知 显示 正在运行 // notification.flags |= Notification.FLAG_AUTO_CANCEL; //让通知被点击之后自动取消掉 // //Step2:唤醒通知, 每个 通知的ID 是唯一的.如果ID一样,则通知栏不会创建新的通知,会重用原来的通知 manager.notify(NOTIFY_ID++, notification); } ``` ## 大图通知 ``` //Step1:获取通知管理器 NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); //在Android(O) 8.0 及以上,需要做以下配置 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (manager != null) { NotificationChannel notificationChannel = new NotificationChannel(NOTIFY_CHANNEL_ID, NOTIFY_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH); manager.createNotificationChannel(notificationChannel); } } //NotificationManagerCompat manager = NotificationManagerCompat.from(this); if (manager != null) { String title = "QQ消息通知"; String content = "小丽,今晚哪里约!"; //创建 PendingIntent 跳转到 Activity Intent intent = new Intent(this, DetailActivity.class); intent.putExtra("key", "阿里斯顿富欧式风dal;ksjf;阿里山叠加开发;阿里上看到肌肤"); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); Bitmap bigPic = BitmapFactory.decodeResource(getResources(), R.mipmap.dog); Bitmap bigLargeIcon = BitmapFactory.decodeResource(getResources(), R.mipmap.qq2); NotificationCompat.BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle() //大图通知样式 的 大图标 .bigLargeIcon(bigLargeIcon) //大图通知样式的 图片 .bigPicture(bigPic) //通知的内容 .setSummaryText(content) //通知的标题 .setBigContentTitle(title); //Step3: 构造 Notification 对象 Notification notification = new NotificationCompat.Builder(this, NOTIFY_CHANNEL_ID) //设置通知的 小图标(必须设置,否则通知不显示) .setSmallIcon(R.mipmap.qq2) //通知标题 .setContentTitle(title) //设置 通知 显示的时间 .setWhen(System.currentTimeMillis() - 3600000L) //通知的内容 .setContentText(content) //在5.0之前的版本中,当通知刚到达时,在状态栏显示 .setTicker(title + ":" + content) //设置 通知 来时的触发(铃声\震动\闪光) .setDefaults(Notification.DEFAULT_ALL) //添加意图对象 .setContentIntent(pendingIntent) //设置 通知的样式(大图/长文本) .setStyle(bigPictureStyle) //构造Notification对象 .build(); // notification.flags |= Notification.FLAG_NO_CLEAR; //让通知不能滑动取消 // notification.flags |= Notification.FLAG_INSISTENT; //重复发出 通知的声音/震动 直到用户点击该通知 // notification.flags |= Notification.FLAG_ONGOING_EVENT; //让通知 显示 正在运行 // notification.flags |= Notification.FLAG_AUTO_CANCEL; //让通知被点击之后自动取消掉 // //Step2:唤醒通知, 每个 通知的ID 是唯一的.如果ID一样,则通知栏不会创建新的通知,会重用原来的通知 manager.notify(NOTIFY_ID++, notification); } ``` ## 长文本通知 ``` //Step1:获取通知管理器 NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); //在Android(O) 8.0 及以上,需要做以下配置 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (manager != null) { NotificationChannel notificationChannel = new NotificationChannel(NOTIFY_CHANNEL_ID, NOTIFY_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH); manager.createNotificationChannel(notificationChannel); } } //NotificationManagerCompat manager = NotificationManagerCompat.from(this); if (manager != null) { String title = "QQ消息通知"; String content = "小丽,今晚哪里约!"; //创建 PendingIntent 跳转到 Activity Intent intent = new Intent(this, DetailActivity.class); intent.putExtra("key", "阿里斯顿富欧式风dal;ksjf;阿里山叠加开发;阿里上看到肌肤"); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle().setBigContentTitle(title).setSummaryText(content).bigText("asdjfhask阿里山的咖啡机阿斯利康打飞机阿里上看到肌肤啊拉克丝登记费爱上咖啡胸前语文哦发生了地方和阿里开始交电话费拉斯交电话费拉克丝交电话费"); //Step3: 构造 Notification 对象 Notification notification = new NotificationCompat.Builder(this, NOTIFY_CHANNEL_ID) //设置通知的 小图标(必须设置,否则通知不显示) .setSmallIcon(R.mipmap.qq2) //通知标题 .setContentTitle(title) //设置 通知 显示的时间 .setWhen(System.currentTimeMillis() - 3600000L) //通知的内容 .setContentText(content) //在5.0之前的版本中,当通知刚到达时,在状态栏显示 .setTicker(title + ":" + content) //设置 通知 来时的触发(铃声\震动\闪光) .setDefaults(Notification.DEFAULT_ALL) //添加意图对象 .setContentIntent(pendingIntent) //设置 通知的样式(大图/长文本) .setStyle(bigTextStyle) //构造Notification对象 .build(); // notification.flags |= Notification.FLAG_NO_CLEAR; //让通知不能滑动取消 // notification.flags |= Notification.FLAG_INSISTENT; //重复发出 通知的声音/震动 直到用户点击该通知 // notification.flags |= Notification.FLAG_ONGOING_EVENT; //让通知 显示 正在运行 notification.flags |= Notification.FLAG_AUTO_CANCEL; //让通知被点击之后自动取消掉 // //Step2:唤醒通知, 每个 通知的ID 是唯一的.如果ID一样,则通知栏不会创建新的通知,会重用原来的通知 manager.notify(NOTIFY_ID, notification); } ``` ## 通知的取消 ``` NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); if (manager != null) { //取消该应用程序发送的所有通知 // manager.cancelAll(); //取消指定ID 的通知 manager.cancel(NOTIFY_ID); } ``` ## 带有进度条的通知 ``` /** * * @param fileName 通知标题 * @param info 通知内容 * @param progress 当前进度 * @param indeterminate 是否是不明确的 */ private void showProgressNotification(String fileName, String info, int progress, boolean indeterminate) { //Step1:获取通知管理器 NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); //在Android(O) 8.0 及以上,需要做以下配置 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (manager != null) { NotificationChannel notificationChannel = new NotificationChannel(NOTIFY_CHANNEL_ID, NOTIFY_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH); manager.createNotificationChannel(notificationChannel); } } //NotificationManagerCompat manager = NotificationManagerCompat.from(this); if (manager != null) { //Step3: 构造 Notification 对象 Notification notification = new NotificationCompat.Builder(this, NOTIFY_CHANNEL_ID) //设置通知的 小图标(必须设置,否则通知不显示) .setSmallIcon(R.mipmap.qq2) //通知标题 .setContentTitle(fileName) //参数1是:总进度,参数2是:当前进度,参数3是:是否不明确 .setProgress(100, progress, indeterminate) //设置 通知 显示的时间 .setWhen(System.currentTimeMillis() - 3600000L) //通知的内容 .setContentText(info) //设置 通知 来时的触发(铃声\震动\闪光) .setDefaults(Notification.DEFAULT_ALL) //构造Notification对象 .build(); // notification.flags |= Notification.FLAG_NO_CLEAR; //让通知不能滑动取消 // notification.flags |= Notification.FLAG_INSISTENT; //重复发出 通知的声音/震动 直到用户点击该通知 // notification.flags |= Notification.FLAG_ONGOING_EVENT; //让通知 显示 正在运行 notification.flags |= Notification.FLAG_AUTO_CANCEL; //让通知被点击之后自动取消掉 // //Step2:唤醒通知, 每个 通知的ID 是唯一的.如果ID一样,则通知栏不会创建新的通知,会重用原来的通知 manager.notify(NOTIFY_ID, notification); } } ``` ## 自定义View 通知 ``` NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); //在Android(O) 8.0 及以上,需要做以下配置 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (manager != null) { NotificationChannel notificationChannel = new NotificationChannel(NOTIFY_CHANNEL_ID, NOTIFY_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH); manager.createNotificationChannel(notificationChannel); } } if (manager != null) { //布局最大高度为 96dp RemoteViews remoteView = new RemoteViews(getPackageName(), R.layout.view_notification); remoteView.setTextViewText(R.id.tv_show, "拉克丝登记费老大撒框架"); remoteView.setImageViewResource(R.id.iv_show, R.mipmap.dog); // 布局最大高度为 256DP RemoteViews remoteViewBig = new RemoteViews(getPackageName(), R.layout.view_notification_big); remoteViewBig.setTextViewText(R.id.tv_show, "拉克丝登记费老大撒框架"); remoteViewBig.setTextViewText(R.id.tv_show2, "这是 大的自定义View特有的TextView"); remoteViewBig.setImageViewResource(R.id.iv_show, R.mipmap.dog); //点击事件,一般配合 BroadcastReceiver 使用 // remoteView.setOnClickPendingIntent(R.id.iv_show,null); //Step3: 构造 Notification 对象 Notification notification = new NotificationCompat.Builder(this, NOTIFY_CHANNEL_ID) //设置通知的 小图标(必须设置,否则通知不显示) .setSmallIcon(R.mipmap.qq2) //通知标题 .setContentTitle("QQ消息通知") //设置 通知 显示的时间 .setWhen(System.currentTimeMillis() - 3600000L) //通知的内容 .setContentText("小丽,今晚哪里约!") //在5.0之前的版本中,当通知刚到达时,在状态栏显示 .setTicker("QQ消息:小丽叫你今晚约!") //设置普通布局,当通知栏空间不足的时候显示的布局 .setCustomContentView(remoteView) //设置大布局,当通知栏空间足够的时候显示该布局 .setCustomBigContentView(remoteViewBig) //设置 通知 来时的触发(铃声\震动\闪光) .setDefaults(Notification.DEFAULT_ALL) //构造Notification对象 .build(); // notification.flags |= Notification.FLAG_NO_CLEAR; //让通知不能滑动取消 // notification.flags |= Notification.FLAG_INSISTENT; //重复发出 通知的声音/震动 直到用户点击该通知 // notification.flags |= Notification.FLAG_ONGOING_EVENT; //让通知 显示 正在运行 notification.flags |= Notification.FLAG_AUTO_CANCEL; //让通知被点击之后自动取消掉 // //Step2:唤醒通知, 每个 通知的ID 是唯一的.如果ID一样,则通知栏不会创建新的通知,会重用原来的通知 manager.notify(NOTIFY_ID++, notification); } ```