1.NotificationManager 通知管理者
NotificationManager notificationManager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
2.NotificationChannel 通知渠道
NotificationChannel notificationChannel=new NotificationChannel("ChannelID","ChannelName", NotificationManager.IMPORTANCE_HIGH);
//三个参数分别为 渠道ID 渠道名 重要性
notificationChannel.setSound(null,null);//设置推送通知时的铃声;null为静音推送,不设置为默认提示音
notificationChannel.enableLights(true);//设置在桌面图标右上角显示小红点(小红点也可为其他颜色的点)
notificationChannel.setLightColor(Color.RED);//设置小红点的颜色
notificationChannel.setShowBadge(true);//在长按桌面图标时显示该渠道通知
//创建渠道
notificationManager.createNotificationChannel(notificationChannel);
//通知渠道是在API26以上使用,需添加如下代码进行判断
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
3.Notification 通知
Notification notification = new Notification.Builder(MainActivity.this)
.setChannelId("ChannelID")//渠道ID 要与渠道对应
.setAutoCancel(true)//设置点击后是否清除通知
.setContentTitle("标题")
.setContentText("你好")
.setSmallIcon(R.drawable.icon)//状态栏中小图标
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.icon))
//下拉显示的大图标-从res中获取资源并生成位图
.setWhen(System.currentTimeMillis())//设置发送通知到现在时间
.setShowWhen(true)
.setContentIntent(pendingIntent)//设置等待Intent(点击通知后执行,可不写)
.setDeleteIntent(pendingIntent)//设置等待Intent(滑除通知后执行,可不写)
.setTicker("状态栏显示信息")//设置状态栏显示的信息
.build();
4.Notification.Builder方法
setWhen(long time) 设置显示的推送时间,格式为“小时:分钟”;显示在通知栏右侧。setShowoWhen(boolean b)设置是否显示推送时间。
setUsesChronometer(bodean b)设置是否显示计时器,动态显示从通知被推送到当前时间的间隔,格式为“小时:分钟”。设置显示计时器时则不会显示推送时间。
setSmallIcon(int imageResource) 设置状态栏中的小图标。
setLargeIcon(Bitmap bitmap) 设置下拉显示的大图标,可用BitmapFactory.decodeResource()。
setTicker(String str) 设置状态栏里面的提示文本。
setContentTitle(String title) 设置通知栏内的标题。
setContentText(String str) 设置通知样内的内容文本。
setSubText(String text) 设置通知栏里面的附加说明文本,位于内容文本下方。若调用该方法,则setProgressBar的设置失效。
setProgress(int max,int now,boolean b) 设置进度条与当前进度。进度条位于标题文本与内容文本之间。参数分别为最大值,当前值,是否为不确定进度条。
setNumber(int num) 设置通知栏右下方的数字,可与SetProgress联合使用,表示
当前的进度数值。
setContentInfo(String str) 设置通知栏右下方的文本。若调用该方法,则setNumber
的设置失效。
setContentIntent(PendingIntent pendingIntent) 设置点击后触发的PendingIintent,通常
调用getActivity,表示点击后跳转。
setDeleteIntent(PendingIntent perdingIntent) 设置滑除后触发的PendingIntent。setAutoCancel(boolean b) 设置点击后是否自动清除通知。
setContent(RemoteViews rv) 设置一个定制的通知栏视图Remoteliews,用于取代默认视图模板。
build() 构建通知(完成全部设黑后使用)。
注意:
(1) setSmallIcon方法必须调用,否则不显示通知。
(2) setWhen与setUsesChronometer同时只能调用一个,都在右侧。
(3) setSubText与setProgress同时只能调用一个,都在标题文本下方。
(4) setNumber与setContentInfo同时只能调用一个,都在右下方。
5.PendingIntent
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
PendingIntent pendingIntent=PendingIntent.getActivity(MainActivity.this , 1 , intent , PendingIntent.FLAG_IMMUTABLE );
//四个参数分别为 环境 请求码 执行的Intent 标志
//标志建议使用PendingIntent.FLAG_IMMUTABLE创建不可变PendingIntent,只有在某些功能依赖于 PendingIntent 的可变性,才应使用PendingIntent.FLAG_MUTABLE
(1) int FLAG_CANCEL_CURRENT
如果新请求的PendingIntent发现已存在时,取消已存在的,用新的PendingIntent替换。
(2) int FLAG_NO_CREATE
如果新请求的PendingIntent发现已存在时,忽略新请求的,继续使用已存在的,日常开发中很少使用。
(3) int FLAG_ONE_SHOT
表示PendingIntent只能使用一次,如果已使用过,那么 getXXX(...) 将返回NULL。
(4) int FLAG_UPDATE_CURRENT 【常用】
如果新请求的PendingIntent发现已存在时,如果Intent有字段改变了,Extra,这更新已存在的PendingIntent。
(5) int FLAG_IMMUTABLE【常用】 和 FLAG_MUTABLE
在Android12后,为了安全,必须在FLAG_IMMUTABLE和FLAG_MUTABLE中设置一个。FLAG_IMMUTABLE【常用】表示PendingIntent不可变,他的内容不可修改,推荐使用;FLAG_MUTABLE表示PendingIntent可变,允许修改它的内容。
6.发送通知
notificationManager.notify(1,notification);
//参数为 通知的ID 通知
7.删除通知
notificationManager.cancel(1); //删除指定ID的通知
notificationManager.cancelAll(); //删除全部通知
8.例
//创建通知管理者
NotificationManager notificationManager= (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
//判断android版本&&创建并添加渠道
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel=new NotificationChannel("id","name",NotificationManager.IMPORTANCE_DEFAULT);
notificationChannel.setSound(null,null);//设置推送通知时的铃声;null为静音推送,不设置为默认提示音
notificationChannel.enableLights(true);//设置在桌面图标右上角显示小红点(小红点也可为其他颜色的点)
notificationChannel.setLightColor(Color.RED);//设置小红点的颜色
notificationChannel.setShowBadge(true);//在长按桌面图标时显示该渠道通知
notificationManager.createNotificationChannel(notificationChannel);
}
//创建通知Builder
Notification.Builder builder=new Notification.Builder(context);
//根据android版本判断是否为通知设置渠道
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.setChannelId("id");
}
//创建通知会执行的等待意图PendingIntent
Intent intent_content=new Intent(context,MainActivity3.class);
PendingIntent pendingIntent_content=PendingIntent.getActivity(context,123,intent_content,PendingIntent.FLAG_IMMUTABLE|PendingIntent.FLAG_UPDATE_CURRENT);
Intent intent_delete=new Intent(context,MainActivity4.class);
PendingIntent pendingIntent_delete=PendingIntent.getActivity(context,456,intent_delete,PendingIntent.FLAG_IMMUTABLE|PendingIntent.FLAG_UPDATE_CURRENT);
//设置通知
builder.setContentText("接收到广播");
builder.setSmallIcon(R.drawable.icon2);
builder.setShowWhen(true);
builder.setWhen(System.currentTimeMillis());
builder.setAutoCancel(true);
builder.setContentIntent(pendingIntent_content);
builder.setDeleteIntent(pendingIntent_delete);
builder.setTicker("状态栏显示信息");
//生成通知
Notification notification=builder.build();
//发送通知 参数为通知ID 通知对象
notificationManager.notify(2333,notification);
//删除指定ID的通知
notificationManager.cancel(2333);
//删除全部通知
notificationManager.cancelAll();