一为什么会与Notification?
因为开发中经常会使用到通知机制中的通知栏框架(Notificaiton),它适用于交互事件的通知。它是位于顶层可以展开的通知列表。它会时不时的提醒你什么软件该更新了,什么人发你微信消息了,有多少条qq消息未读,显示正在进行的事物比如酷狗音乐后台播放。个人感觉:要学好Notificaiton通知并拓展使用就一定要学习好android的4大组件,因为它经常跟四大组件配合使用。
二状态通知栏涉及到的四个类
Notification , NotificationManager ,NotificationCompat.Builder,PendingIntent
Notification:为通知信息类,它里面对应了通知栏的各个属性
NotificationManager : 是状态栏通知的管理类,负责发通知、清除通知等操作。
以上四个类的调用顺序:
1,首先获取通知栏管理类:
NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
2,实例化通知栏构造器NotificationCompat.Builder并进行配置:

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);  
    mBuilder.setContentTitle("测试标题")//设置通知栏标题  
        .setContentText("测试内容") //设置通知栏显示内容 
        .setContentIntent(getDefalutIntent(Notification.FLAG_AUTO_CANCEL)) //设置通知栏点击意图  
    //  .setNumber(number) //设置通知集合的数量  
        .setTicker("测试通知来啦") //通知首次出现在通知栏,带上升动画效果的  
        .setWhen(System.currentTimeMillis())//通知产生的时间,会在通知信息里显示,一般是系统获取到的时间  
        .setPriority(Notification.PRIORITY_DEFAULT) //设置该通知优先级  
    //  .setAutoCancel(true)//设置这个标志当用户单击面板就可以让通知将自动取消    
        .setOngoing(false)//ture,设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)  
        .setDefaults(Notification.DEFAULT_VIBRATE)//向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置,使用defaults属性,可以组合  
        //Notification.DEFAULT_ALL  Notification.DEFAULT_SOUND 添加声音 // requires VIBRATE permission  
        .setSmallIcon(R.drawable.ic_launcher);//设置通知小ICON

3,实例化Notification并设置flag以及flag详解以及方法解释

Notification notification = mBuilder.build();  
notification.flags = Notification.FLAG_AUTO_CANCEL;
提醒标志符成员:
Notification.FLAG_SHOW_LIGHTS              //三色灯提醒,在使用三色灯提醒时候必须加该标志符
Notification.FLAG_ONGOING_EVENT          //发起正在运行事件(活动中)
Notification.FLAG_INSISTENT   //让声音、振动无限循环,直到用户响应 (取消或者打开)
Notification.FLAG_ONLY_ALERT_ONCE  //发起Notification后,铃声和震动均只执行一次
Notification.FLAG_AUTO_CANCEL      //用户单击通知后自动消失
Notification.FLAG_NO_CLEAR          //只有全部清除时,Notification才会清除 ,不清楚该通知(QQ的通知无法清除,就是用的这个)
Notification.FLAG_FOREGROUND_SERVICE    //表示正在运行的服务

4,实例化PendingIntent并解释
1)获取实例
public PendingIntent getDefalutIntent(int flags){
PendingIntent pendingIntent= PendingIntent.getActivity(this, 1, new Intent(), flags);
return pendingIntent;
}
PendingIntent和Intent略有不同,它可以设置执行次数,主要用于远程服务通信、闹铃、通知、启动器、短信中,在一般情况下用的比较少。
2)PendingIntent什么用
Notification支持多种Intent来响应单击事件、消除事件、处理紧急状态的全屏事件等。
这里就用到了setContentIntent(PendingIntent intent)来处理以上这么多的事件。
3)相关属性和方法
属性:
PendingIntent的位标识符:
FLAG_ONE_SHOT 表示返回的PendingIntent仅能执行一次,执行完后自动取消
FLAG_NO_CREATE 表示如果描述的PendingIntent不存在,并不创建相应的PendingIntent,而是返回NULL
FLAG_CANCEL_CURRENT 表示相应的PendingIntent已经存在,则取消前者,然后创建新的PendingIntent,这个有利于数据保持为最新的,可以用于即时通信的通信场景
FLAG_UPDATE_CURRENT 表示更新的PendingIntent
4)调用

Intent intent = new Intent(context,XXX.class);  
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);  
mBuilder.setContentIntent(pendingIntent)

最后一步启动通知:

mNotificationManager.notify(notifyId, mBuilder.build());