Notification

2016-09-08  本文已影响0人  Tuttonero

Notification的作用

1.显示接收到短消息、即使消息等信息 (如QQ、微信、新浪、短信)
2.显示客户端的推送消息(如有新版本发布,广告,推荐新闻等)
3.显示正在进行的事物(例如:后台运行的程序)(如音乐播放器、版本更新时候的下载进度等)

Notification的Flag属性

NotificationCompat.Builder:setDefaults()
Notification.DEFAULT_VIBRATE           //添加默认震动提醒  需要 VIBRATE permission
Notification.DEFAULT_SOUND             //添加默认声音提醒
Notification.DEFAULT_LIGHTS            // 添加默认三色灯提醒
Notification.DEFAULT_ALL               // 添加默认以上3种全部提醒
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才会清除 ,不清除该通知 
Notification.FLAG_FOREGROUND_SERVICE   //表示正在运行的服务
setContentIntent:在通知窗口区域,Notification被单击时的响应事件由该intent触发;
setDeleteIntent:当用户点击全部清除按钮时,响应该清除事件的Intent;
setFullScreenIntent:响应紧急状态的全屏事件(例如来电事件),也就是说通知来的时候,跳过在通知区域点击通知这一步,直接执行fullScreenIntent代表的事件。

普通通知

NotificationCompat.Builder builder = new Builder(this);
builder.setSmallIcon(R.drawable.ic_launcher)                    //必须
           .setContentTitle("通知的标题")                        //必须
           .setContentText("通知的内容")                         //必须
           .setDefaults(                                       //设置通知方式
Notification.DEFAULT_SOUND);                                   //声音
manager.notify(1,builder.build()) ;                            //发送通知

大视图通知

NotificationCompat.Builder builder = new Builder(this);
builder.setSmallIcon(R.drawable.ic_launcher)                   //必须
           .setContentTitle("通知的标题")                       //必须
           .setContentText("通知的内容")                        //必须
           .setDefaults(                                      //设置通知方式
Notification.DEFAULT_SOUND);                                  //声音
//大图片样式
NotificationCompat.BigPictureStyle style = new BigPictureStyle() ;
style.setBigContentTitle("大视图标题")
        .setSummaryText("大视图文本");
style.bigPicture( BitmapFactory.decodeResource( getResources(), R.drawable.qq));
builder.setStyle(style) ;
manager.notify(2,builder.build());

进度通知

final NotificationCompat.Builder builder = new Builder(this);
builder.setSmallIcon(R.drawable.ic_launcher)
           .setContentText("下载")
           .setContentText("下载中...");

//开始模拟进度条
new Thread(new Runnable() {
    @Override
    public void run() {
        for (int i = 0 ; i <=100 ; i++) {
               //第一个参数:进度指示器设置为最大值 
               //第二个参数:当前完成百分比 
               //第三个参数:活动指示器启用 如果true 那么会是一个进度展示
               builder.setProgress(100,i,true);
               Thread.sleep(100);
               manager.notify(3,builder.build());
         }
         manager.cancel(3);
      }
}).start();

自定义通知

NotificationCompat.Builder builder = new Builder(this);
builder.setSmallIcon(R.drawable.ic_launcher)
           .setContentTitle("title")
           .setContentText("text")
          .setDefaults( Notification.DEFAULT_ALL ) ;
RemoteViews views = new RemoteViews(getPackageName(), R.layout.activity_notification ;//layoutId :自己布局文件
builder.setContent(views);
manager.notify(4,builder.build());

通过通知跳转页面

Intent intent = new Intent( this, NotificationActivity.class );
//封装延迟意图(点击通知要进入的下一个页面)
PendingIntent pendingIntent = PendingIntent.getActivity(this, 100, intent, PendingIntent.FLAG_ONE_SHOT );
builder.setContentIntent( pendingIntent );       //通过构造者发送延时意图
Notification notification = builder.build( );    //通过构造者建立一个通知对象
manager.notify( 1, notification );              //发送通知
//在跳转的页面
private NotificationManager manager;            //创建一个通知管理者对象
//在onCreate方法里创建出来
manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(1);                             //id:就是当初发送通知的定义的id

PendingIntent的用法

PendingIntent和Intent略有不同,它可以设置执行次数,主要用于远程服务通信、闹铃、通知、启动器、短信中,在一般情况下用的比较少。
Notification支持多种Intent来响应单击事件、消除事件、处理紧急状态的全屏事件等。
这里就用到了setContentIntent(PendingIntent intent)来处理以上这么多的事件。

PendingIntent属性:
PendingIntent的位标识符:
FLAG_ONE_SHOT                        表示返回的PendingIntent仅能执行一次,执行完后自动取消
FLAG_NO_CREATE                       表示如果描述的PendingIntent不存在,并不创建相应的PendingIntent,而是返回NULL
FLAG_CANCEL_CURRENT                  表示相应的PendingIntent已经存在,则取消前者,然后创建新的PendingIntent,这个有利于数据保持为最新的,可以用于即时通信的通信场景
FLAG_UPDATE_CURRENT                  表示更新的PendingIntent
上一篇 下一篇

猜你喜欢

热点阅读