通知(Notification)是Android系统比较有特色的功能。当某个应用需向用户提示某些信息,而该应用又不在前台运行,就可以借助通知(Notification)实现。发出一条通知后,手机的状态栏会显示一条通知,下拉可以看到通知的详情,当然可以实现点击通知后跳转到某个页面。
通知Notification的基本用法:
通知可以在活动、广播接收器、服务中创建。在活动中创建通知比较少,因为一般只用程序进入后台才需要使用通知提示用户。不过,在哪里创建,步骤基本相同:
① 利用Context的getSystemService(NOTIFICATION_SERVICE)获取通知这个服务,然后借助NotificationManager管理:
NotificationManager manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
② 构造通知对象:利用构造器Builder(一般用NotificationCompat中的Builder)来创建Notification对象
Notification notification=new NotificationCompat.Builder(this)
.setContentTitle("我是标题") #通知的标题
.setContentText("这是内容") #通知的内容
.setWhen(System.currentTimeMillis()) #通知的时间
.setSmallIcon(R.mipmap.ic_launcher) #通知的小图标,显示在状态栏上的
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
#通知的大图标,显示在下拉状态栏时的通知中
.setContentIntent(pendingIntent) #点击图标时跳转的页面
****等等****
.build();
(最后的builde()方法之前,都可以使用连缀任意个setXXX()的方法设置一些属性)
③ 发送通知:使用NotificationManager对象的notify(int id ,Notification notification)
int id: 设置此通知的id,往后可用标识这个通知
Notification notification:即将要发送的通知对象
eg) 示例代码:(在Activity中创建的)
//获取通知服务:getSystemService(NOTIFICATION_SERVICE),再由NotificationManager管理
NotificationManager manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
//获取通知对象:借助NotificationCompat类的Builder创建通知对象,并设置属性
Notification notification=new NotificationCompat.Builder(this)
.setContentTitle("我是标题")
.setContentText("这是内容")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
.setContentIntent(pendingIntent)
.build();
//发送通知:利用NotificationManager对象的notify(id,具体通知对象)
manager.notify(1,notification);
通知的点击:
都知道,通知点击时,一般可以跳转到某些页面的。要实现这个,需要利用PendingIntent。Intent和PendingIntent都可以指明一个“意图”,都可以用于启动一个活动、启动服务、发送广播等。不同的是,Intent更加倾向立即执行某个意图,而PendingIntent倾向于在某个合适时机执行某个意图。
Pending用法:(和Intent差不多)
● 创建意图:Intent intent=new Intent(Context,活动/广播/服务)
● 获取相应的PendingIntent实例:PendingIntent pi= getActivity()/getBroadcast()/getService()
getActivity()/getBroadcast()/getService()有4个参数:
参数1:context,参数2一般用不到传入0即可
参数3:Intent对象,可以利用它构建PendingIntent的意图
参数4:用于确定PendingIntent的行为,有FLAG_ONE_SHOT等等,一般传入0即可
● 最后在Builder构造Notification对象时,builder()前再连缀setContentIntent(intent) 即可设置通知的点击逻辑
eg: 例如点击通知跳转某个活动activity上(创建好了另一个activity)
//当点击通知时,跳转的页面:利用Intent创建PendingIntent的实例
Intent intent=new Intent(this,NoticedActivity.class); #pendingIntent的意图
PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,0); #获取pendingIntent实例
//获取通知服务:getSystemService(NOTIFICATION_SERVICE),再由NotificationManager管理
NotificationManager manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
//获取通知对象:借助NotificationCompat类的Builder创建通知对象,并设置属性
Notification notification=new NotificationCompat.Builder(this)
.setContentTitle("我是标题")
.setContentText("这是内容")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
.setContentIntent(pendingIntent)
.build();
//发送通知:利用NotificationManager对象的notify(id,具体通知对象)
manager.notify(1,notification);
取消通知:
点击通知后,显示通知图标应该消失,实现的方法有两种:
① 一种是在Builder构造Notification对象时,再连缀一个setAutoCancel(true)方法,即可
② 一种是显示地,在点击通知后的页面中,调用NotificationManager的cancel(int id)方法(id是notification标识)
//显示调用NotificationManager的cancel(id)方法撤除通知
NotificationManager manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
manager.cancel(1);
通知的进阶:
① 设置通知音效:Builder构造Notification对象前(.builder()前) 再连缀一个setSound( URI uri)即可
② 设置通知震动:Builder构造Notification对象前(.builder()前) 再连缀一个setVibrate(new Long[] {n1,n2,....})即可
setVibrate(new Long[] {n1,n2,....})参数说明:长整型数组,每一个元素代表震动/静止的毫秒数
数组元素的下标为偶数(0 2 4...),代表静止的毫秒数
数组元素的下标为偶数(1 3 5...),代表震动的毫秒数
③ 设置通知LED灯:Builder构造Notification对象前(.builder()前) 再连缀一个setLights(Color.color, 灯亮起来时长, 灯暗下来时长)
( 当然,懒得设置,直接默认即可:(.builder()前) 再连缀setDefaults(NotificationManager.DEFAULT_ALL)即可 )
通知的高级进阶
1. 通知不仅仅有短文本和图标,还可以显示长文本、图片等等富文本,(.builder()前) 再连缀一个setStyle( NotificationCompat.Style):
① 长文本信息的通知:在setStyle()中创建一个new NotificationCompat.BigTextStyle()对象,再由bigText(“长文本内容”)传入具体文本内容,就可实现在通知中显示长文本信息
Notification notification=new NotificationCompat.Builder(this)
...
.setStyle(new NotificationCompat.BigTextStyle().bigText("长文本内容") )
...
.build();
② 大图片的通知:在setStyle()中创建一个new NotificationCompat.BigPictureStyle()对象,再由bigPicture(BitmapFactory.decodeRresource(getResources), R.drawable.****)传入具体图片
Notification notification=new NotificationCompat.Builder(this)
...
.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeRresource(getResources), R.drawable.****) )
...
.build();
2. 设置通知的重要程度:setPriority(NotificationCompat.PRIORITY_***)
NotificationCompat.PRIORITY_DEFAULT: 默认,和不设置一样
NotificationCompat.PRIORITY_MIN:最低重要,如下拉状态栏才会显示此通知
NotificationCompat.PRIORITY_LOW,NotificationCompat.PRIORITY_HIGH
NotificationCompat.PRIORITY_MAX:最高重要,让用户立刻看到,如微信信息弹出提示
Notification notification=new NotificationCompat.Builder(this)
...
.setPriority(NotificationCompat.PRIORITY_***)
...
.build();