Notification 的使用

Notification 为用于在状态栏显示通知信息的控件。
实现notification的步骤:
第一步:获取通知管理器 NotificationManager 
NotificationManager是状态栏通知的管理类,负责发通知;清楚通知的;它是一个系统的Service,必须通过getSystemService()方法来获取。

 NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
第二步:创建一个builder用来设置通知栏的属性。
setLargeIcon()   设置大图标
setSmallIcon()     设置小图标
setTicker()        设置消息来的时候的提示语
setContentTitle()  设置通知栏的标题
setContentText()   设置通知栏显示的内容
下面对Notification类中的一些常量,字段,方法简单介绍一下:
例如: 
builder.setDefaults(Notification.DEFAULT_SOUND)
     常量:
        Notification.DEFAULT_ALL            使用所有默认值,比如声音,震动,闪屏等等
        Notification.DEFAULT_LIGHTS         使用默认闪光提示
        Notification.DEFAULT_SOUNDS         使用默认提示声音
        Notification.DEFAULT_VIBRATE        使用默认手机震动
      【说明】:加入手机震动,一定要在manifest.xml中加入权限:
           <uses-permission Android:name="android.permission.VIBRATE" />



1:
 Notification.Builder builder = new Notification.Builder(MainActivity.this);
                //设置builder的属性
                //设置大图标
                Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher);
                builder.setLargeIcon(bitmap);
                //设置消息来时得提示
                builder.setTicker("通知来了");
                //设置通知标题
                builder.setContentTitle("蓝鸥紧急通知");
                //设置通知内容
                builder.setContentText("今晚别走");
                //设置小图标
                builder.setSmallIcon(R.mipmap.ic_launcher);
                 //通知发来时的提示音
                 builder.setDefaults(Notification.DEFAULT_SOUND);

  2:               
  点击通知栏跳转到界面,看通知的内容 (跳转):首先要创建一个activity

//创建一个跳转 intent
   Intent intent = new Intent(MainActivity.this,suActivity.class);
                //参数1:传一个上下文对象
                // 参数2:传一个返回的状态码
                //参数3:一个intent  通常是跳转的 intent
                //参数4:是通知与页面的更新类型

                 PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,100,intent,PendingIntent.FLAG_CANCEL_CURRENT);
                builder.setContentIntent(pendingIntent);

第三步:根据builder 创建notification,通过NotificationManager的 notify()方法来启动Notification。
  第一个参数是标识该Notification,第二个参数就是Notification对象。

//根据builder创建notification
                Notification notification =builder.build();
   //利用manager将通知显示出来   参数1:给他设了个id 作为标识   参数2:notification 对象
                manager.notify(100,notification);

设置自定义通知:

第一步:获取通知管理器 NotificationManager
NotificationManager是状态栏通知的管理类,负责发通知;清楚通知的;它是一个系统的Service,必须通过getSystemService()方法来获取。

NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

第二步:创建一个builder用来设置通知栏的属性。

Notification.Builder builder = new Notification.Builder(MainActivity.this);

                builder.setSmallIcon(R.mipmap.ic_launcher);

第三步:创建 remoteView 对象
RemoteViews views = new RemoteViews(getPackageName(),R.layout.notification_layout);
//2 设置remoteView属性
views.setTextViewText(R.id.text,”姑娘 你真不嫌脏”);
views.setTextViewText(R.id.text2,”不嫌脏”);
//设置的图标:
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.w);
builder.setLargeIcon(bitmap);
builder.setContent(views);

自定义跳转:


  //1创建一个PendingIntent
 Intent intent1 = new Intent(MainActivity.this,suActivity.class);
  PendingIntent pendingIntent1 = PendingIntent.getActivity(MainActivity.this,100,intent1,PendingIntent.FLAG_UPDATE_CURRENT);
  //设置并关联响应事件
 views.setOnClickPendingIntent(R.id.butInfo,pendingIntent1);

第四步:根据builder 创建notification,通过NotificationManager的 notify()方法来启动Notification。
第一个参数是 标识 该Notification,第二个参数就是Notification对象。

//根据builder创建notification
                Notification notification =builder.build();
   //利用manager将通知显示出来   参数1:给他设了个id 作为标识   参数2:notification 对象
                manager.notify(100,notification);

清除通知 :
通过NotificationManager 的cancel(int)方法,来清除某个通知。其中参数就是Notification 标识 ID。
当然也可以通过 cancelAll() 来清除状态栏所有的通知。

 button2= (Button) findViewById(R.id.cancle_notification);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //1.拿到通知manager
                NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                //2取消通知
                //取消所有通知
             //   manager.cancelAll();
                //取消指定的通知,根据 Id 标识  来确定对象
                manager.cancel(100);
            }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值