全面解析Notification

Notification在Android中使用的频率可以说是非常高的,本篇博客,我将围绕着Notification的各方面进行解析,使大家对Notification有更好的认识。

Notification的使用步骤

1.获取NotificationManager

NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  

2.创建NotificationCompat.Builder

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);  

3.对Builder设置一些Notification相关属性:

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  

4.使用Builder创建通知

Notification notification = mBuilder.build();

5.使用NotificationManager将通知推送出去

int id = 199;
LogUtils.d(TAG, "创建通知");
mNotificationManager.notify(id, notification);

Notification重要方法解析

Notification 的基本操作主要有创建、更新、取消这三种。一个 Notification 的必要属性有三项,如果不设置则在运行时会抛出异常:

  1. 小图标,通过 setSmallIcon() 方法设置
  2. 标题,通过 setContentTitle() 方法设置
  3. 内容,通过 setContentText() 方法设置

除了以上三项,其它均为可选项。虽然如此,但还是应该给 Notification 设置一个 Action ,这样就可以直接跳转到 App 的某个 Activity 、启动一个 Service 或者发送一个 Broadcast。否则,Notification 仅仅只能起到通知的效果,而不能与用户交互。
当系统接收到通知时,可以通过震动、响铃、呼吸灯等多种方式进行提醒。

1) setSmallIcon() 与 setLargeIcon()
在 NotificationCompat.Builder 中有设置通知的大小图标的两个方法。这两个方法有什么区别呢?当 setSmallIcon() 与 setLargeIcon() 同时存在时, smallIcon 显示在largeIcon的右下角;当只设置 setSmallIcon() 时, smallIcon 显示在左侧。看下图你就明白了。对于部分 ROM ,可能修改过源码,如 MIUI 上通知的大图标和小图标是没有区别的。
这里写图片描述
Google 官方是这么解释 setSmallIcon() 这个方法的:

Set the small icon resource, which will be used to represent the notification in the status bar. The platform template for the expanded view will draw this icon in the left, unless a large icon has also been specified, in which case the small icon will be moved to the right-hand side.

2) 设置提醒标志符Flags
方法解释:提醒标志符,向通知添加声音、闪灯和振动效果等设置达到通知提醒效果,可以组合多个属性
a) 创建通知栏之后通过给他添加.flags属性赋值。

1.	Notification notification = mBuilder.build();  
2.	notification.flags = Notification.FLAG_AUTO_CANCEL; 

b)	通过setContentIntent(PendingIntent intent)方法中的意图设置对应的flags
1.	public PendingIntent getDefalutIntent(int flags){  
2.	    PendingIntent pendingIntent= PendingIntent.getActivity(this, 1, new Intent(), flags);  
3.	    return pendingIntent;  
4.	}  

各标志符介绍

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    //表示正在运行的服务

3) .setDefaults(int defaults) (NotificationCompat.Builder中的方法,用于设置通知到来时,通过什么方式进行提示)

方法解释:向通知添加声音、闪灯和振动效果的最简单、使用默认(defaults)属性,可以组合多个属性(和方法1中提示效果一样的)
对应属性:
Notification.DEFAULT_VIBRATE //添加默认震动提醒 需要 VIBRATE permission

Notification.DEFAULT_SOUND // 添加默认声音提醒
Notification.DEFAULT_LIGHTS// 添加默认三色灯提醒
Notification.DEFAULT_ALL// 添加默认以上3种全部提醒

/**
* 显示带有默认铃声、震动、呼吸灯效果的通知
* 如需实现自定义效果,请参考后面三个例子
*/
private void showNotifyWithMixed() {
   NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
           .setSmallIcon(R.mipmap.ic_launcher)
           .setContentTitle("我是有铃声+震动+呼吸灯效果的通知")
           .setContentText("库里就是叼~")
           //等价于setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE);
           .setDefaults(Notification.DEFAULT_ALL);
   mManager.notify(5, builder.build());
}

4) setVibrate(long[] pattern)
方法解释:设置震动的时间

.setVibrate(new long[] {0,300,500,700});  

实现效果:延迟0ms,然后振动300ms,在延迟500ms,接着在振动700ms。
还有另外一种写法:

mBuilder.build().vibrate = new long[] {0,300,500,700};  

如果希望设置默认振动方式,设置了方法(2)中默认为DEFAULT_VIBRATE 即可。
例子:

/**
* 展示有震动效果的通知,需要在AndroidManifest.xml中申请震动权限
* <uses-permission android:name="android.permission.VIBRATE" />
* 补充:测试震动的时候,手机的模式一定要调成铃声+震动模式,否则你是感受不到震动的
*/
private void showNotifyWithVibrate() {
   //震动也有两种设置方法,与设置铃声一样,在此不再赘述
   long[] vibrate = new long[]{0, 500, 1000, 1500};
   NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
           .setSmallIcon(R.mipmap.ic_launcher)
           .setContentTitle("我是伴有震动效果的通知")
           .setContentText("颤抖吧,凡人~")
           //使用系统默认的震动参数,会与自定义的冲突
           //.setDe
  • 26
    点赞
  • 104
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 19
    评论
评论 19
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

进击的代码家

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值