定义及用法
定义: 是在系统的通知栏中呈现多样式持久性消息的类
用处:
显示客户端的推送消息(如有新版本发布、广告、推荐新闻等)
显示正在进行的事物(如音乐播放器、版本更新时候的下载进度等)
显示接收到短消息,即时消息等信息(如QQ、微信、新浪、短信)
常用方法
setContentTitle(); // 设置标题
setContentText(); // 设置内容
setWhen(); // 设置通知时间
setSmallIcon; // 设置通知的小图标 (必须)
setLargeIcon(); // 设置通知大图标
setAutoCancel(); // 自动删除通知
setSmallIcon() 与 setLargeIcon()
当 setSmallIcon() 与 setLargeIcon() 同时存在时, smallIcon 显示在通知的右下角, largeIcon 显示在左侧;当只设置 setSmallIcon() 时, smallIcon 显示在左侧。
创建一个最简单的通知
Notification.Builder builder = new Notification.Builder(this);
builder.setContentTitle("贪玩蓝月");
builder.setContentText("装备好爆又好卖");
builder.setSmallIcon(R.mipmap.ic_launcher);
Notification notification = builder.build();
自定义通知
其实就是加载一个布局. 加载布局用到了 RemoteViews 代码中有详细注释.
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
代码中加载
Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher); // 设置小图标(用不到,但是规定必须设置)
// 创建自定义布局
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.cus_view);
// 设置布局中的控件内容
remoteViews.setImageViewResource(R.id.img, R.mipmap.ic_launcher);
remoteViews.setTextViewText(R.id.tv, "贪玩蓝月");
builder.setCustomContentView(remoteViews);
进度条通知
Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher);
Timer timer = new Timer();
// 使用定时器模拟下载情况
timer.schedule(new TimerTask() {
private int progress = 0;
@Override
public void run() {
// 更新进度
progress += 10;
builder.setContentText("下载中....进度" + progress);
// 参数 进度最大值, 当前进度, 是否使用模糊进度
builder.setProgress(100, progress, false);
// 更新进度条
notificationManager.notify(6, builder.build());
// 下载完成模拟安装
if (progress >= 100) {
timer.cancel(); // 取消定时器
builder.setContentText("安装中...");
builder.setProgress(0, 0, true); // 设置模糊进度条
notificationManager.notify(6, builder.build()); // 更新进度条
// 休眠5秒
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 取消通知
notificationManager.cancel(6);
}
}
}, 0, 1000);
通知分组
用途: 将同一个APP的消息合并
Notification.Builder builder0 = new Notification.Builder(this);
Notification.Builder builder1 = new Notification.Builder(this);
Notification.Builder builder2 = new Notification.Builder(this);
Notification.Builder builder3 = new Notification.Builder(this);
builder0.setGroup("1"); // 设置分组
builder0.setSmallIcon(R.mipmap.ic_launcher);
// 被设置setGroupSummary为true的消息会隐藏内容,其内容会显示另外分组成员信息
builder0.setGroupSummary(true);
builder1.setGroup("1");
builder1.setSmallIcon(R.mipmap.ic_launcher);
builder1.setContentTitle("1111");
builder1.setContentText("1111");
builder2.setGroup("1");
builder2.setSmallIcon(R.mipmap.ic_launcher);
builder2.setContentTitle("2222");
builder2.setContentText("2222");
builder3.setGroup("1");
builder3.setSmallIcon(R.mipmap.ic_launcher);
builder3.setContentTitle("3333");
builder3.setContentText("3333");
notificationManager.notify(0, builder0.build());
notificationManager.notify(1, builder1.build());
notificationManager.notify(2, builder2.build());
notificationManager.notify(3, builder3.build());
将消息打开
锁屏通知
Android 5.0(API level 21)开始,通知可以显示在锁屏上,通过设置选择是否允许敏感的通知内容显示在安全的锁屏上
//builde的时候
//通过 setVisibility() 方法设置即可
.setVisibility(VISIBILITY_PUBLIC)
.build();
setVisibility() 方法共有三个选值:
1.VISIBILITY_PRIVATE : 显示基本信息,如通知的图标,但隐藏通知的全部内容;
2.VISIBILITY_PUBLIC : 显示通知的全部内容;
3.VISIBILITY_SECRET : 不显示任何内容,包括图标。
安卓7.0直接回复通知
private void responseNotification2() {
// 其中RESULT_KEY是获取回复的内容,setLabel设置的值就是EditText的hint值
RemoteInput remoteInput = new RemoteInput.Builder("KEY").setLabel("回复通知").build();
Intent intent = new Intent(this, Main2Activity.class);
PendingIntent pendingIntent = PendingIntent.getService(this,1,intent,PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.mipmap.ic_launcher,"回复",pendingIntent).addRemoteInput(remoteInput).build();
// 其中这个Builder需要传递四个参数,第一个就是logo图片,第二个类似于标签我们要点击的。第三个就是要做的动作intent.最后把我们创建的remoteInput加入进来。
NotificationCompat.Builder builder = new NotificationCompat.Builder(this,"1")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("请问需要银行贷款吗?")
.setContentText("您好,我是XX银行的XX经理, 请问你需要办理银行贷款吗?")
.setColor(Color.CYAN)
.setPriority(Notification.PRIORITY_MAX) // 设置优先级为Max,则为悬浮通知
.addAction(action) // 设置回复action
.setAutoCancel(true)
.setWhen(System.currentTimeMillis())
.setDefaults(Notification.DEFAULT_ALL) // 想要悬浮出来, 这里必须要设置
.setCategory(Notification.CATEGORY_MESSAGE);
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// NotificationManager nm = getSystemService(NotificationManager.class);
Notification notification = builder.build();
nm.notify(1,notification);
Bundle resultsFromIntent = RemoteInput.getResultsFromIntent(intent);
Log.i(TAG, "responseNotification2: "+resultsFromIntent);
//根据key拿回复的内容
Log.i(TAG, "responseNotification2: ddd");
if (null!=resultsFromIntent){
String resultString = resultsFromIntent.getString("KEY");
//处理回复内容
Log.i(TAG, "responseNotification2: ");
reply(resultString);
}
}
private void reply(final String resultString) {
new Thread(new Runnable() {
@Override
public void run() {
SystemClock.sleep(1000);
Log.i(TAG, "run: "+Main2Activity.class.getSimpleName()+resultString);
onReply();
}
}).start();
}
private void onReply() {
final NotificationManager nm = getSystemService(NotificationManager.class);
final Handler handler = new Handler(getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
// 更新通知为“回复成功”
Notification notification = new NotificationCompat.Builder(NotificationActivity.this,"1")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentText("回复成功")
.build();
nm.notify(1, notification);
}
});
// 最后将通知取消
handler.postDelayed(new Runnable() {
@Override
public void run() {
nm.cancel(1);
}
}, 2000);
}
通知的样式
大图通知
列表通知
大图通知
Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher);
Notification.BigPictureStyle bigPictureStyle = new Notification.BigPictureStyle();
bigPictureStyle.bigPicture(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
builder.setStyle(bigPictureStyle);
列表通知
Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher);
Notification.InboxStyle inboxStyle = new Notification.InboxStyle();
inboxStyle.addLine("贪玩蓝月");
inboxStyle.addLine("古天乐绿了");
builder.setStyle(inboxStyle);
如何发布通知
通过系统服务获取
NotificationManager notificationManager = (NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE);
/** 发布通知
* 参数 通知的id, 通知
* 如果id一致则会覆盖通知
* /
notificationManager.notify(12, builder.build());
PendingIntent
说明
PendingIntent是一种特殊的Intent,字面意思可以解释为延迟的Intent,用于某个事件结束后执行的Action,从上面带Action的通知也能验证这点,当用户点击通知时才会执行。
PendingIntent 是 Android 系统管理并持有的用于描述和获取原始数据的对象的标志(引用)。也就是说,即便创建该PendingIntent对象的进程被杀死了,这个PendingItent对象在其他进程中还是可用的。
日常使用中的短信、闹钟等都用到了 PendingIntent
构造方法说明
//获取一个用于启动 Activity 的 PendingIntent 对象
PendingIntent.getActivity(Context context, int requestCode, Intent intent, int flags);
//获取一个用于启动 Service 的 PendingIntent 对象
PendingIntent.getService(Context context, int requestCode, Intent intent, int flags);
//获取一个用于向 BroadcastReceiver 广播的 PendingIntent 对象
PendingIntent.getBroadcast(Context context, int requestCode, Intent intent, int flags)
PendingIntent 具有的几种 flag:
FLAG_CANCEL_CURRENT:
如果当前系统中已经存在一个相同的 PendingIntent 对象,那么就将先将已有的 PendingIntent 取消,然后重新生成一个 PendingIntent 对象。
FLAG_NO_CREATE:
如果当前系统中不存在相同的 PendingIntent 对象,系统将不会创建该 PendingIntent 对象而是直接返回 null 。
FLAG_ONE_SHOT:
该 PendingIntent 只作用一次。
FLAG_UPDATE_CURRENT:
如果系统中已存在该 PendingIntent 对象,那么系统将保留该 PendingIntent 对象,但是会使用新的 Intent 来更新之前 PendingIntent 中的 Intent 对象数据,例如更新 Intent 中的 Extras 。
发布一个带交互的通知
Notification.Builder builder = new Notification.Builder(this);
builder.setAutoCancel(true); // 点击消息通知取消
builder.setSmallIcon(R.mipmap.ic_launcher);
// 自定义布局
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.cus_view);
remoteViews.setTextViewText(R.id.tv, "贪玩蓝月");
remoteViews.setImageViewResource(R.id.img, R.mipmap.ic_launcher);
builder.setCustomContentView(remoteViews);
// 创建intent意图
Intent intent = new Intent(this, Main2Activity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// 设置意图对象
builder.setContentIntent(pendingIntent);
notificationManager.notify(1, builder.build());
设置 Notification 的通知效果 (震动需要真机)
Notification 有震动、响铃、呼吸灯三种响铃效果,可以通过 setDefaults(int defaults) 方法来设置。 Default 属性有以下四种,一旦设置了 Default 效果,自定义的效果就会失效。
// 在builder的时候加上如下属性即可.
builder.setDefaults(Notification.DEFAULT_ALL);
//设置系统默认提醒效果,一旦设置默认提醒效果,则自定义的提醒效果会全部失效。具体可看源码
//添加默认震动效果,需要申请震动权限
//<uses-permission android:name="android.permission.VIBRATE" />
Notification.DEFAULT_VIBRATE
//添加系统默认声音效果,设置此值后,调用setSound()设置自定义声音无效
Notification.DEFAULT_SOUND
//添加默认呼吸灯效果,使用时须与 Notification.FLAG_SHOW_LIGHTS 结合使用,否则无效
Notification.DEFAULT_LIGHTS
//添加上述三种默认提醒效果
Notification.DEFAULT_ALL
更新 Notification
更新通知很简单,只需要再次发送相同 ID 的通知即可,如果之前的通知还未被取消,则会直接更新该通知相关的属性;如果之前的通知已经被取消,则会重新创建一个新通知。
取消Notification
取消通知有如下 5 种方式:
点击通知栏的清除按钮,会清除所有可清除的通知
设置了 setAutoCancel() 或 FLAG_AUTO_CANCEL 的通知,点击该通知时会清除它
通过 NotificationManager 调用 cancel(int id) 方法清除指定 ID 的通知
通过 NotificationManager 调用 cancel(String tag, int id) 方法清除指定 TAG 和 ID 的通知
通过 NotificationManager 调用 cancelAll() 方法清除所有该应用之前发送的通知
如果你是通过 NotificationManager.notify(String tag, int id, Notification notify) 方法创建的通知,那么只能通过 NotificationManager.cancel(String tag, int id) 方法才能清除对应的通知,调用NotificationManager.cancel(int id) 无效。
安卓8.0以上通知的写法
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel no1 = new NotificationChannel("100", "通知1", NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(no1);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this,"100");
RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.notify_layout);
Intent intent = new Intent(this, MainActivity.class);
PendingIntent activity = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// 不能在加这样的属性了,否则没有效果
// remoteViews.setImageViewResource(R.id.start_id,R.mipmap.ic_launcher);
remoteViews.setOnClickPendingIntent(R.id.start_id,activity);
builder.setContentTitle("标题")
.setContentText("内容")
.setAutoCancel(true)
.setCustomContentView(remoteViews)
.setSmallIcon(R.mipmap.ic_launcher);
Notification build = builder.build();
notificationManager.notify(1,build);
}
属性演示
/**
* 最普通的通知效果
*/
private void showNotifyOnlyText() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(mLargeIcon)
.setContentTitle("我是只有文字效果的通知")
.setContentText("我没有铃声、震动、呼吸灯,但我就是一个通知");
mManager.notify(1, builder.build());
}
/**
* 展示有自定义铃声效果的通知
* 补充:使用系统自带的铃声效果:Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");
*/
private void showNotifyWithRing() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("我是伴有铃声效果的通知")
.setContentText("美妙么?安静听~")
//调用系统默认响铃,设置此属性后setSound()会无效
//.setDefaults(Notification.DEFAULT_SOUND)
//调用系统多媒体裤内的铃声
//.setSound(Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI,"2"));
//调用自己提供的铃声,位于 /res/values/raw 目录下
.setSound(Uri.parse("android.resource://com.littlejie.notification/" + R.raw.sound));
//另一种设置铃声的方法
//Notification notify = builder.build();
//调用系统默认铃声
//notify.defaults = Notification.DEFAULT_SOUND;
//调用自己提供的铃声
//notify.sound = Uri.parse("android.resource://com.littlejie.notification/"+R.raw.sound);
//调用系统自带的铃声
//notify.sound = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI,"2");
//mManager.notify(2,notify);
mManager.notify(2, builder.build());
}
/**
* 展示有震动效果的通知,需要在AndroidManifest.xml中申请震动权限
* <uses-permission android:name="android.permission.VIBRATE" />
* 补充:测试震动的时候,手机的模式一定要调成铃声+震动模式,否则你是感受不到震动的
*/
private void showNotifyWithVibrate() {
//震动也有两种设置方法,与设置铃声一样,在此不再赘述
long[] vibrate = new long[]{0, 500, 1000, 1500};
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("我是伴有震动效果的通知")
.setContentText("颤抖吧,凡人~")
//使用系统默认的震动参数,会与自定义的冲突
//.setDefaults(Notification.DEFAULT_VIBRATE)
//自定义震动效果
.setVibrate(vibrate);
//另一种设置震动的方法
//Notification notify = builder.build();
//调用系统默认震动
//notify.defaults = Notification.DEFAULT_VIBRATE;
//调用自己设置的震动
//notify.vibrate = vibrate;
//mManager.notify(3,notify);
mManager.notify(3, builder.build());
}
/**
* 显示带有呼吸灯效果的通知(####)
*/
private void showNotifyWithLights() {
final NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("我是带有呼吸灯效果的通知")
.setContentText("一闪一闪亮晶晶~")
//ledARGB 表示灯光颜色、 ledOnMS 亮持续时间、ledOffMS 暗的时间
.setLights(0xFF0000, 3000, 3000);
Notification notify = builder.build();
//只有在设置了标志符Flags为Notification.FLAG_SHOW_LIGHTS的时候,才支持呼吸灯提醒。
notify.flags = Notification.FLAG_SHOW_LIGHTS;
//设置lights参数的另一种方式
//notify.ledARGB = 0xFF0000;
//notify.ledOnMS = 500;
//notify.ledOffMS = 5000;
//使用handler延迟发送通知,因为连接usb时,呼吸灯一直会亮着
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mManager.notify(4, builder.build());
}
}, 10000);
}
/**
* 显示带有默认铃声、震动、呼吸灯效果的通知
* 如需实现自定义效果,请参考前面三个例子
*/
private void showNotifyWithMixed() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("我是有铃声+震动+呼吸灯效果的通知")
.setContentText("我是最棒的~")
//等价于setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE);
.setDefaults(Notification.DEFAULT_ALL);
mManager.notify(5, builder.build());
}
/**
* 通知无限循环,直到用户取消或者打开通知栏(其实触摸就可以了),效果与FLAG_ONLY_ALERT_ONCE相反
* 注:这里没有给Notification设置PendingIntent,也就是说该通知无法响应,所以只能手动取消
*/
private void showInsistentNotify() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("我是一个死循环,除非你取消或者响应")
.setContentText("啦啦啦~")
.setDefaults(Notification.DEFAULT_ALL);
Notification notify = builder.build();
notify.flags |= Notification.FLAG_INSISTENT;
mManager.notify(6, notify);
}
/**
* 通知只执行一次,与默认的效果一样
*/
private void showAlertOnceNotify() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("仔细看,我就执行一遍")
.setContentText("好了,已经一遍了~")
.setDefaults(Notification.DEFAULT_ALL);
Notification notify = builder.build();
notify.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
mManager.notify(7, notify);
}
/**
* 清除所有通知
*/
private void clearNotify() {
mManager.cancelAll();
}