3.1 Notification建立必要的属性,必须设置php
3.1.1 必须添加的属性android
小图标,经过 setSmallIcon() 方法设置git
标题,经过 setContentTitle() 方法设置github
内容,经过 setContentText() 方法设置segmentfault
3.2 Notification建立步骤api
3.2.1 Notification 的建立主要涉及到 Notification.Builder 、 Notification 、 NotificationManager Notification.Builer : 使用建造者模式构建 Notification 对象。因为 Notification.Builder 仅支持 Android 4.1及以后的版本,为了解决兼容性问题, Google 在 Android Support v4 中加入了 - NotificationCompat.Builder 类。对于某些在 Android 4.1 以后才特性,即便 NotificationCompat.Builder 支持该方法,在以前的版本中也不能运行。微信
Notification : 通知对应类,保存通知相关的数据。- NotificationManager 向系统发送通知时会用到。app
NotificationManager : NotificationManager 是通知管理类,它是一个系统服务。调用 NotificationManager 的 notify() 方法能够向系统发送通知。ide
3.2.2 Notification建立步骤与代码函数
// 建立一个NotificationManager的引用 NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); // 定义Notification的各类属性 Notification.Builder mBuilder = new Notification.Builder(this.getApplicationContext()) .setSound(android.provider.Settings.System.DEFAULT_NOTIFICATION_URI) // .setSmallIcon(R.mipmap.ic_launcher) //设置通知的图标 .setTicker("有新消息呢") //设置状态栏的标题 .setContentTitle("这个是标题") //设置标题 .setContentText("这个是内容") //消息内容 .setDefaults(Notification.DEFAULT_ALL) //设置默认的提示音 .setPriority(Notification.PRIORITY_DEFAULT) //设置该通知的优先级 .setOngoing(false) //让通知左右滑的时候不能取消通知 .setPriority(Notification.PRIORITY_DEFAULT) //设置该通知的优先级 .setWhen(System.currentTimeMillis()) //设置通知时间,默认为系统发出通知的时间,一般不用设置 .setAutoCancel(true); //打开程序后图标消失 //处理点击Notification的逻辑 Intent resultIntent = new Intent(this, TestActivity.class); resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //添加为栈顶Activity resultIntent.putExtra("what",5); PendingIntent resultPendingIntent = PendingIntent.getActivity(this,5,resultIntent,PendingIntent.FLAG_UPDATE_CURRENT); mBuilder.setContentIntent(resultPendingIntent); //发送 mNotificationManager.notify(1, mBuilder.build()); //结束广播 //mNotificationManager.cancel(1);
3.3 关于setSmallIcon()与setLargeIcon()区别
在 NotificationCompat.Builder 中有设置通知的大小图标的两个方法。这两个方法有什么区别呢?
当 setSmallIcon() 与 setLargeIcon() 同时存在时, smallIcon 显示在通知的右下角, largeIcon 显示在左侧
当只设置 setSmallIcon() 时, smallIcon 显示在左侧。看下图你就明白了。
对于部分 ROM ,可能修改过源码,如 MIUI 上通知的大图标和小图标是没有区别的。
效果如图所示:
3.4 Notification的Action属性
设置一个 Action ,这样就能够直接跳转到 App 的某个 Activity 、启动一个 Service 或者发送一个 Broadcast。不然,Notification 仅仅只能起到通知的效果,而不能与用户交互。
具体代码以下所示:
//建立intent Intent resultIntent = new Intent(this, TestActivity.class); resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //添加为栈顶Activity resultIntent.putExtra("what",5); PendingIntent resultPendingIntent = PendingIntent.getActivity(this,5,resultIntent,PendingIntent.FLAG_UPDATE_CURRENT); //发送pendingIntent mBuilder.setContentIntent(resultPendingIntent);
3.5 更新Notification
更新通知很简单,只须要再次发送相同 ID 的通知便可,若是以前的通知还未被取消,则会直接更新该通知相关的属性;若是以前的通知已经被取消,则会从新建立一个新通知。更新通知跟发送通知使用相同的方式。
3.6 取消Notification
取消通知有以下 5 种方式:
点击通知栏的清除按钮,会清除全部可清除的通知
设置了 setAutoCancel() 或 FLAG_AUTO_CANCEL 的通知,点击该通知时会清除它
经过 NotificationManager 调用 cancel(int id) 方法清除指定 ID 的通知
经过 NotificationManager 调用 cancel(String tag, int id) 方法清除指定 TAG 和 ID 的通知
经过 NotificationManager 调用 cancelAll() 方法清除全部该应用以前发送的通知
注意事项
若是你是经过 NotificationManager.notify(String tag, int id, Notification notify) 方法建立的通知,那么只能经过 NotificationManager.cancel(String tag, int id) 方法才能清除对应的通知,调用NotificationManager.cancel(int id) 无效。
3.7 设置flag属性
设置FLAG_NO_CLEAR表示
设置通知不能被状态栏的清除按钮给清除掉,也不能被手动清除,但能经过 cancel() 方法清除
代码:
private void sendNotification9() { Notification.Builder mBuilder = new Notification.Builder(this.getApplicationContext()) .setSound(android.provider.Settings.System.DEFAULT_NOTIFICATION_URI) .setSmallIcon(R.mipmap.ic_launcher) //设置通知的图标 .setTicker("有新消息呢9") //设置状态栏的标题 .setContentTitle("这个是标题9") //设置标题 .setContentText("这个是内容9") //消息内容 .setDefaults(Notification.DEFAULT_ALL) //设置默认的提示音 .setOngoing(false) //让通知左右滑的时候不能取消通知 .setAutoCancel(true); //打开程序后图标消失 Notification notification = mBuilder.build(); //设置 Notification 的 flags = FLAG_NO_CLEAR //FLAG_NO_CLEAR 表示该通知不能被状态栏的清除按钮给清除掉,也不能被手动清除,但能经过 cancel() 方法清除 //flags 能够经过 |= 运算叠加效果 notification.flags |= Notification.FLAG_NO_CLEAR; //获取NotificationManager 对象 mNotificationManager.notify(9, notification); } //取消通知: if(mNotificationManager!=null){ mNotificationManager.cancelAll(); }
3.8 设置Notification的通知效果
Notification 有震动、响铃、呼吸灯三种响铃效果,能够经过 setDefaults(int defualts) 方法来设置。 Default 属性有如下四种,一旦设置了 Default 效果,自定义的效果就会失效。楼主在这里踩了坑,愣是调了半天没找到为何自定义效果会消失,忘你们慎之。
//设置系统默认提醒效果,一旦设置默认提醒效果,则自定义的提醒效果会所有失效。具体可看源码//添加默认震动效果,须要申请震动权限//<uses-permission android:name="android.permission.VIBRATE" /> Notification.DEFAULT_VIBRATE //添加系统默认声音效果,设置此值后,调用setSound()设置自定义声音无效 Notification.DEFAULT_SOUND //添加默认呼吸灯效果,使用时须与 Notification.FLAG_SHOW_LIGHTS 结合使用,不然无效 Notification.DEFAULT_LIGHTS //添加上述三种默认提醒效果 Notification.DEFAULT_ALL
除了以上几种设置 Notification 默认通知效果,还能够经过如下几种 FLAG 设置通知效果。
//提醒效果经常使用 Flag//三色灯提醒,在使用三色灯提醒时候必须加该标志符 Notification.FLAG_SHOW_LIGHTS //发起正在运行事件(活动中) Notification.FLAG_ONGOING_EVENT //让声音、振动无限循环,直到用户响应 (取消或者打开) Notification.FLAG_INSISTENT //发起Notification后,铃声和震动均只执行一次 Notification.FLAG_ONLY_ALERT_ONCE //用户单击通知后自动消失 Notification.FLAG_AUTO_CANCEL //只有调用NotificationManager.cancel()时才会清除 Notification.FLAG_NO_CLEAR //表示正在运行的服务 Notification.FLAG_FOREGROUND_SERVICE
设置默认提醒
// 添加默认声音提醒 builder.setDefaults(Notification.DEFAULT_SOUND); // 添加默认呼吸灯提醒,自动添加FLAG_SHOW_LIGHTS builder.setDefaults(Notification.DEFAULT_LIGHTS);
设置铃声属性,用的不多
private void sendNotification11() { Notification.Builder builder = new Notification.Builder(this) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle("我是伴有铃声效果的通知11") .setContentText("美妙么?安静听~11") //调用系统默认响铃,设置此属性后setSound()会无效 //.setDefaults(Notification.DEFAULT_SOUND) //调用系统多媒体裤内的铃声 //.setSound(Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI,"2")); //调用本身提供的铃声,位于 /res/values/raw 目录下 .setSound(Uri.parse("android.resource://com.yc.cn.ycnotification/" + R.raw.hah)); //另外一种设置铃声的方法 //Notification notify = builder.build(); //调用系统默认铃声 //notify.defaults = Notification.DEFAULT_SOUND; //调用本身提供的铃声 //notify.sound = Uri.parse("android.resource://com.yc.cn.ycnotification/"+R.raw.sound); //调用系统自带的铃声 //notify.sound = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI,"2"); //mManager.notify(11,notify); mNotificationManager.notify(11, builder.build()); }
设置震动属性
private void sendNotification12() { //震动也有两种设置方法,与设置铃声同样,在此再也不赘述 long[] vibrate = new long[]{0, 500, 1000, 1500}; Notification.Builder builder = new Notification.Builder(this) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle("我是伴有震动效果的通知") .setContentText("颤抖吧,逗比哈哈哈哈哈~") //使用系统默认的震动参数,会与自定义的冲突 //.setDefaults(Notification.DEFAULT_VIBRATE) //自定义震动效果 .setVibrate(vibrate); //另外一种设置震动的方法 //Notification notify = builder.build(); //调用系统默认震动 //notify.defaults = Notification.DEFAULT_VIBRATE; //调用本身设置的震动 //notify.vibrate = vibrate; //mManager.notify(3,notify); mNotificationManager.notify(12, builder.build()); }
3.9 设置自定义Notification通知栏布局
代码以下,注意,这里只取部分代码,完整代码能够下载github的完整项目:https://github.com/yangchong211/YCNotification
.setContent(getRemoteViews()) // 设置通知栏的布局 //建立自定义布局 private RemoteViews getRemoteViews() { RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification_mobile_play); // 设置 点击通知栏的上一首按钮时要执行的意图 remoteViews.setOnClickPendingIntent(R.id.btn_pre, getActivityPendingIntent(11)); // 设置 点击通知栏的下一首按钮时要执行的意图 remoteViews.setOnClickPendingIntent(R.id.btn_next, getActivityPendingIntent(12)); // 设置 点击通知栏的播放暂停按钮时要执行的意图 remoteViews.setOnClickPendingIntent(R.id.btn_start, getActivityPendingIntent(13)); // 设置 点击通知栏的根容器时要执行的意图 remoteViews.setOnClickPendingIntent(R.id.ll_root, getActivityPendingIntent(14)); remoteViews.setTextViewText(R.id.tv_title, "标题"); // 设置通知栏上标题 remoteViews.setTextViewText(R.id.tv_artist, "艺术家"); // 设置通知栏上艺术家 return remoteViews; }
4.1 PendingIntent说明
4.1.1 PendingIntent基本说明
PendingIntent 是一种特殊的 Intent ,字面意思能够解释为延迟的 Intent ,用于在某个事件结束后执行特定的 Action 。从上面带 Action 的通知也能验证这一点,当用户点击通知时,才会执行。
PendingIntent 是 Android 系统管理并持有的用于描述和获取原始数据的对象的标志(引用)。也就是说,即使建立该PendingIntent对象的进程被杀死了,这个PendingItent对象在其余进程中仍是可用的。 平常使用中的短信、闹钟等都用到了 PendingIntent。
4.1.2 PendingIntent三种获取方式
//获取一个用于启动 Activity 的 PendingIntent 对象public static PendingIntent getActivity(Context context, int requestCode, Intent intent, int flags); //获取一个用于启动 Service 的 PendingIntent 对象public static PendingIntent getService(Context context, int requestCode, Intent intent, int flags); //获取一个用于向 BroadcastReceiver 广播的 PendingIntent 对象public static PendingIntent getBroadcast(Context context, int requestCode, Intent intent, int flags)
4.1.3 PendingIntent具备几种flag
FLAG_CANCEL_CURRENT:若是当前系统中已经存在一个相同的 PendingIntent 对象,那么就将先将已有的 PendingIntent 取消,而后从新生成一个 PendingIntent 对象。 FLAG_NO_CREATE:若是当前系统中不存在相同的 PendingIntent 对象,系统将不会建立该 PendingIntent 对象而是直接返回 null 。 FLAG_ONE_SHOT:该 PendingIntent 只做用一次。 FLAG_UPDATE_CURRENT:若是系统中已存在该 PendingIntent 对象,那么系统将保留该 PendingIntent 对象,可是会使用新的 Intent 来更新以前 PendingIntent 中的 Intent 对象数据,例如更新 Intent 中的 Extras 。
4.2 建立返回栈PendingIntent
4.2.1 添加返回栈代码 默认状况下,从通知启动一个Activity,按返回键会回到主屏幕。 但某些时候有按返回键仍然留在当前应用的需求,这就要用到TaskStackBuilder了。
Notification.Builder mBuilder = new Notification.Builder(context) .setSound(android.provider.Settings.System.DEFAULT_NOTIFICATION_URI) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle("广播接受者标题,小杨") .setContentText("广播接受者内容,扯犊子") .setAutoCancel(true); Log.i(TAG, "onReceive: intent" + intent.getClass().getName()); Intent resultIntent = new Intent(context, MainActivity.class); TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); //将该Activity添加为栈顶 stackBuilder.addParentStack(MainActivity.class); stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); mBuilder.setContentIntent(resultPendingIntent); NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager.notify(1, mBuilder.build());
4.3 注意要点
若是用户的手机使静音模式,那么设置铃声或者震动效果将会失效
5.1 RemoteView是什么?
5.1.1 什么是RemoteView?
为啥Notification不会设计成和普通View同样的使用方式?理由很简单!状态栏不是单单由你的应用程序管理.状态栏是由Android系统管理的.你须要显示Notification就必须和系统打交道.必须经过Notification服务才能显示你的Notification.因此设计成用一个Notification实例表明一个Notification,而后经过notificationManager.notify函数提交给Notification服务.
5.1.2 Notification服务是什么?是一个独立的线程!
又扯出一个问题.跨线程显示View.该怎么显示?不是在本应用程序显示View.这里就要借用RemoteView.
RemoteView理解成对一个View的封装,而后把RemoteView提交给其余线程.其余线程接收到RemoteView而且解析里面View的信息把它显示出来.
5.1.3 在使用系统自带的Notification系统会建立一个默认的RemoteView!
系统默认使用R.layout.notification_template_material_base生产一个RemoteView. 至于这里的布局是怎么查到的,请看下面源码分析
5.2 查看源码,了解Notification如何建立布局
5.2.1 首先看Notification中build代码
5.2.2 而后看上图中的createContentView()方法
5.2.3 而后看上图中的createContentView()方法
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { //android 8.0以上须要特殊处理,也就是targetSDKVersion为26以上 createNotificationChannel(); } @TargetApi(Build.VERSION_CODES.O) private void createNotificationChannel() { NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT); channel.canBypassDnd();//是否绕过请勿打扰模式 channel.enableLights(true);//闪光灯 channel.setLockscreenVisibility(VISIBILITY_SECRET);//锁屏显示通知 channel.setLightColor(Color.RED);//闪关灯的灯光颜色 channel.canShowBadge();//桌面launcher的消息角标 channel.enableVibration(true);//是否容许震动 channel.getAudioAttributes();//获取系统通知响铃声音的配置 channel.getGroup();//获取通知取到组 channel.setBypassDnd(true);//设置可绕过 请勿打扰模式 channel.setVibrationPattern(new long[]{100, 100, 200});//设置震动模式 channel.shouldShowLights();//是否会有灯光 getManager().createNotificationChannel(channel); }
public class NotificationUtils extends ContextWrapper { public static final String CHANNEL_ID = "default"; private static final String CHANNEL_NAME = "Default_Channel"; private NotificationManager mManager; private int[] flags; public NotificationUtils(Context base) { super(base); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { //android 8.0以上须要特殊处理,也就是targetSDKVersion为26以上 createNotificationChannel(); } } @TargetApi(Build.VERSION_CODES.O) private void createNotificationChannel() { //第一个参数:channel_id //第二个参数:channel_name //第三个参数:设置通知重要性级别 //注意:该级别必需要在 NotificationChannel 的构造函数中指定,总共要五个级别; //范围是从 NotificationManager.IMPORTANCE_NONE(0) ~ NotificationManager.IMPORTANCE_HIGH(4) NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT); channel.canBypassDnd();//是否绕过请勿打扰模式 channel.enableLights(true);//闪光灯 channel.setLockscreenVisibility(VISIBILITY_SECRET);//锁屏显示通知 channel.setLightColor(Color.RED);//闪关灯的灯光颜色 channel.canShowBadge();//桌面launcher的消息角标 channel.enableVibration(true);//是否容许震动 channel.getAudioAttributes();//获取系统通知响铃声音的配置 channel.getGroup();//获取通知取到组 channel.setBypassDnd(true);//设置可绕过 请勿打扰模式 channel.setVibrationPattern(new long[]{100, 100, 200});//设置震动模式 channel.shouldShowLights();//是否会有灯光 getManager().createNotificationChannel(channel); } /** * 获取建立一个NotificationManager的对象 * @return NotificationManager对象 */ public NotificationManager getManager() { if (mManager == null) { mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); } return mManager; } /** * 清空全部的通知 */ public void clearNotification(){ getManager().cancelAll(); } /** * 建议使用这个发送通知 * 调用该方法能够发送通知 * @param notifyId notifyId * @param title title * @param content content */ public void sendNotification(int notifyId, String title, String content , int icon) { Notification build; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { //android 8.0以上须要特殊处理,也就是targetSDKVersion为26以上 //通知用到NotificationCompat()这个V4库中的方法。可是在实际使用时发现书上的代码已通过时而且Android8.0已经不支持这种写法 Notification.Builder builder = getChannelNotification(title, content, icon); build = builder.build(); } else { NotificationCompat.Builder builder = getNotificationCompat(title, content, icon); build = builder.build(); } if (flags!=null && flags.length>0){ for (int a=0 ; a<flags.length ; a++){ build.flags |= flags[a]; } } getManager().notify(notifyId, build); } /** * 调用该方法能够发送通知 * @param notifyId notifyId * @param title title * @param content content */ public void sendNotificationCompat(int notifyId, String title, String content , int icon) { NotificationCompat.Builder builder = getNotificationCompat(title, content, icon); Notification build = builder.build(); if (flags!=null && flags.length>0){ for (int a=0 ; a<flags.length ; a++){ build.flags |= flags[a]; } } getManager().notify(notifyId, build); } private NotificationCompat.Builder getNotificationCompat(String title, String content, int icon) { NotificationCompat.Builder builder; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { builder = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID); } else { //注意用下面这个方法,在8.0以上没法出现通知栏。8.0以前是正常的。这里须要加强判断逻辑 builder = new NotificationCompat.Builder(getApplicationContext()); builder.setPriority(PRIORITY_DEFAULT); } builder.setContentTitle(title); builder.setContentText(content); builder.setSmallIcon(icon); builder.setPriority(priority); builder.setOnlyAlertOnce(onlyAlertOnce); builder.setOngoing(ongoing); if (remoteViews!=null){ builder.setContent(remoteViews); } if (intent!=null){ builder.setContentIntent(intent); } if (ticker!=null && ticker.length()>0){ builder.setTicker(ticker); } if (when!=0){ builder.setWhen(when); } if (sound!=null){ builder.setSound(sound); } if (defaults!=0){ builder.setDefaults(defaults); } //点击自动删除通知 builder.setAutoCancel(true); return builder; } @RequiresApi(api = Build.VERSION_CODES.O) private Notification.Builder getChannelNotification(String title, String content, int icon){ Notification.Builder builder = new Notification.Builder(getApplicationContext(), CHANNEL_ID); Notification.Builder notificationBuilder = builder //设置标题 .setContentTitle(title) //消息内容 .setContentText(content) //设置通知的图标 .setSmallIcon(icon) //让通知左右滑的时候是否能够取消通知 .setOngoing(ongoing) //设置优先级 .setPriority(priority) //是否提示一次.true - 若是Notification已经存在状态栏即便在调用notify函数也不会更新 .setOnlyAlertOnce(onlyAlertOnce) .setAutoCancel(true); if (remoteViews!=null){ //设置自定义view通知栏 notificationBuilder.setContent(remoteViews); } if (intent!=null){ notificationBuilder.setContentIntent(intent); } if (ticker!=null && ticker.length()>0){ //设置状态栏的标题 notificationBuilder.setTicker(ticker); } if (when!=0){ //设置通知时间,默认为系统发出通知的时间,一般不用设置 notificationBuilder.setWhen(when); } if (sound!=null){ //设置sound notificationBuilder.setSound(sound); } if (defaults!=0){ //设置默认的提示音 notificationBuilder.setDefaults(defaults); } if (pattern!=null){ //自定义震动效果 notificationBuilder.setVibrate(pattern); } return notificationBuilder; } private boolean ongoing = false; private RemoteViews remoteViews = null; private PendingIntent intent = null; private String ticker = ""; private int priority = Notification.PRIORITY_DEFAULT; private boolean onlyAlertOnce = false; private long when = 0; private Uri sound = null; private int defaults = 0; private long[] pattern = null; /** * 让通知左右滑的时候是否能够取消通知 * @param ongoing 是否能够取消通知 * @return */ public NotificationUtils setOngoing(boolean ongoing){ this.ongoing = ongoing; return this; } /** * 设置自定义view通知栏布局 * @param remoteViews view * @return */ public NotificationUtils setContent(RemoteViews remoteViews){ this.remoteViews = remoteViews; return this; } /** * 设置内容点击 * @param intent intent * @return */ public NotificationUtils setContentIntent(PendingIntent intent){ this.intent = intent; return this; } /** * 设置状态栏的标题 * @param ticker 状态栏的标题 * @return */ public NotificationUtils setTicker(String ticker){ this.ticker = ticker; return this; } /** * 设置优先级 * 注意: * Android 8.0以及上,在 NotificationChannel 的构造函数中指定,总共要五个级别; * Android 7.1(API 25)及如下的设备,还得调用NotificationCompat 的 setPriority方法来设置 * * @param priority 优先级,默认是Notification.PRIORITY_DEFAULT * @return */ public NotificationUtils setPriority(int priority){ this.priority = priority; return this; } /** * 是否提示一次.true - 若是Notification已经存在状态栏即便在调用notify函数也不会更新 * @param onlyAlertOnce 是否只提示一次,默认是false * @return */ public NotificationUtils setOnlyAlertOnce(boolean onlyAlertOnce){ this.onlyAlertOnce = onlyAlertOnce; return this; } /** * 设置通知时间,默认为系统发出通知的时间,一般不用设置 * @param when when * @return */ public NotificationUtils setWhen(long when){ this.when = when; return this; } /** * 设置sound * @param sound sound * @return */ public NotificationUtils setSound(Uri sound){ this.sound = sound; return this; } /** * 设置默认的提示音 * @param defaults defaults * @return */ public NotificationUtils setDefaults(int defaults){ this.defaults = defaults; return this; } /** * 自定义震动效果 * @param pattern pattern * @return */ public NotificationUtils setVibrate(long[] pattern){ this.pattern = pattern; return this; } /** * 设置flag标签 * @param flags flags * @return */ public NotificationUtils setFlags(int... flags){ this.flags = flags; return this; } }