您的位置:首页 > 移动开发 > Android开发

Notification通知的使用

2016-09-23 19:10 399 查看
Notification —— 通知,主要作用是警示用户。它是看不见的程序组件(Broadcast Receiver,Service和不活跃的Activity)警示用户有需要注意的事件发生的最好途径。 Notification 是由NotificationManager(系统服务)统一管理的。

Notification使用过程相对比较简单,社长这里主要介绍一些比较常用的用法,可以帮助开发者快速入门。

第一步:获取通知管理服务NotificationManager

<span style="font-size:18px;">NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);</span>

第二步:创建Notification.Builder

在SDK23之前可以直接实例化Notification使用通知,但是在23开始由于setLatestEventInfo()这个方法已经被移除了,会提示找不到这个方法,并且最新版本中官方建议用Notification.Builder创建notification实例。所以这里也只介绍这种实例方法。

Notification.Builder builder = new Notification.Builder(this);
第三步:通过builder配置通知,介绍比较常用的API吧,想深入了解的可以翻翻官方文档。

setContentInfo()    设置右下角的内容,一般可以不设置。

setContentText()    设置左下角的内容,主要的描述信息。

setContentTitle()    设置通知标题。

setWhen()   设置通知时间。

setSmallIcon()    设置通知图标。

setTicker()    设置通知时携带的通知提示内容。

setAutoCancel()    设置是否点击消失,true点击消失,false点击不消失

setDefaults()    设置常量,例如设置通知效果,builder.setDefaults(Notification.DEFAULT_SOUND);表示通知提醒带默认的通知音效,这个可以判断是否处于静音模式下,如果静音选择震动,否则选择声音提醒。主要常用常量:

             DEFAULT_ALL              使用默认字段(所有效果的感觉)

             DEFAULT_LIGHTS       默认闪光

             DEFAULT_SOUND      默认声音

             DEFAULT_VIRATE       默认震动,需要添加震动权限VIBRATE: Android.permission.VIBRATE

setOngoing()    设置是否为运行中的通知,true表示通知运行中不可移除,也不可被通知栏清理按钮清理,false则反之,例如有些mp3就需要常驻通知,不过退出的时候记得在onDestroy()中cancel掉通知。

setContentIntent()    设置PendingIntent对象,点击该通知时发送该Intent(PendingIntent,

第四步:创建Notification

<span style="font-size:18px;">Notification notification = builder.build();</span>

第五步:发送通知

nm.notify(notification_id, notification);
给个例子:

NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);;
获取通知服务

<span style="font-size:18px;">//设置并发送通知
public void setNotify(){
//实例Builder
Notification.Builder builder = new Notification.Builder(this);
//设置通知各种信息
builder.setContentInfo("点击进入生词库");
builder.setContentText("生词学习时间已到,去看看吧");
builder.setContentTitle("版权词典");
builder.setWhen(System.currentTimeMillis());
builder.setSmallIcon(R.drawable.dictionary_icon);
builder.setTicker("生词学习时间已到");
//设置点击消失
builder.setAutoCancel(true);
//设置是否为运行中的通知
builder.setOngoing(true);
//设置使用默认声音提醒
builder.setDefaults(judgeSilent());
//设置Intent
Intent intent = new Intent(this, Main3Activity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
builder.setContentIntent(pendingIntent);
//实例Notification
Notification notification = builder.build();
//发送通知
nm.notify(1, notification);
}</span>
创建Notification.Builder,配置通知信息并发送通知,judgeSilent()方法是用来判断是否当前机子是否为静音,静音的话则发送通知为震动提示,否则为声音提示。

<span style="font-size:18px;"> @Override
protected void onDestroy() {
super.onDestroy();
if(nm != null){
nm.cancelAll();
}
}</span>因为设置了该通知为运行中通知,setOngoing(true),所有应该在程序退出的时候适当的选择是否取消通知。
<span style="font-size:18px;">//判断是否为静音
private int judgeSilent(){
AudioManager audioManager = (AudioManager)getSystemService(AUDIO_SERVICE);
int statusFlag = (audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT) ? 1: 0;
//0:表示非静音,1:表示静音
if(statusFlag == 0)
return Notification.DEFAULT_SOUND;
else
return Notification.DEFAULT_VIBRATE;
}</span>

运行效果:



Notification还提供了自定义通知布局,通过setContent(RemoteViews)设置。自定义布局的通知和常规通知原理都一样,只是通知的内容设置不同,设置了自定义布局就不需要再通过builder去设置通知的显示内容。只需要设置提示内容,和通知的特性(就震动,可点之类的)。直接上代码吧。

//z自定义布局通知
private void setCustomLayoutNotify(){
Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.drawable.dictionary_icon);
builder.setTicker("生词学习时间已到");
builder.setDefaults(judgeSilent());
//设置RemoteViews加载布局
RemoteViews remoteView = new RemoteViews(this.getPackageName(),R.layout.notification);
//设置布局内容
remoteView.setImageViewResource(R.id.iv_icon,R.drawable.dictionary_icon);
r
9946
emoteView.setTextViewText(R.id.tv_title,"自定义remoteView");
//加入布局
builder.setContent(remoteView);
Intent intent = new Intent(this, Main3Activity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
builder.setContentIntent(pendingIntent);
Notification notification = builder.build();
nm.notify(2, notification);
}

首先实例一个RemoteViews并且加入自定义的布局,再设置布局中控件内容,最后通过builder设置加入到通知中,布局比较简单,就一个ImageView和TextView就不贴代码了。

运行效果:



OK!Notification快速上手相对比较容易,builder还能设置许多丰富的功能,有兴趣的可以深入了解。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息