Android之Notification详解

Notification概述:
Notification,属于Google原创。是一种具有全局效果的通知,可以在系统的状态栏以及通知面板中显示。

Notification的作用:
1、显示接收到及时信息,(如微信、QQ)。
2、显示客户端的推送消息,如广告、优惠、版本更新、推荐新闻等。
3、显示正在运行的事物,例如后台运行的程序,如音乐播放进度、下载进度等。

Notification的基本操作:
Notification的基本操作主要有创建、更新、取消这三种。
通常我们会去设置通知的小图标、标题、内容。当系统收到通知时,可以通过震动、响铃、呼吸灯等方式提醒用户。
创建通知:创建通知,我们需要用到Notification、NotificationManager以及Notification.Builder(我们这里只需要兼容Android 4.1以上,所以使用Notification.Builder),我们创建Notification使用到了建造者模式。简单实例代码如下:

   NotificationManager notifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
   Notification.Builder builder = new Notification.Builder(this)
           .setSmallIcon(R.mipmap.icon)
           .setContentTitle("最简单的Notification")
           .setContentText("只有小图标、标题、内容")
   notifyManager.notify(1, builder.build());

给Notification设置Action
要想Notification与用户产生交互,则必须给Notification设置Action。那么具体怎么做呢?步骤如下:
1、创建Intent;
2、创建PendingIntent;
2、setContentIntent;
实例代码如下:

   Intent intent = new Intent(this, MainActivity.class);
   PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, mainIntent, PendingIntent.FLAG_UPDATE_CURRENT);
   NotificationManager notifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
   Notification.Builder builder = new Notification.Builder(this)
           .setSmallIcon(R.mipmap.icon)
           .setContentTitle("最简单的Notification")
           .setContentText("只有小图标、标题、内容")
           .setAutoCancel(true)
           .setContentIntent(pendingIntent);
   notifyManager.notify(0, builder.build());

关于PendingIntent
PendingIntent 是一种特殊的Intent ,字面意思可以解释为延迟的Intent,用于在某个事件结束后执行特定的Action。
PendingIntent 是Android系统管理并持有的用于描述和获取原始数据的对象的标志(引用)。也就是说,即便创建该PendingIntent对象的进程被杀死了,这个PendingItent对象在其他进程中还是可用的。
日常使用中的短信、闹钟等都用到了PendingIntent。
PendingIntent主要可以通过以下三种方式获取:
1、

//获取一个用于启动Activity的PendingIntent对象
public static PendingIntent getActivity(Context context, int requestCode, Intent intent, int flags);

2、

//获取一个用于启动Service的PendingIntent对象
public static PendingIntent getService(Context context, int requestCode, Intent intent, int flags);

3、

//获取一个用于向BroadcastReceiver广播的PendingIntent对象
public static PendingIntent getBroadcast(Context context, int requestCode, Intent intent, int flags)

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 。

更新通知
更新通知很简单,只需要再次发送相同 ID 的通知即可,如果之前的通知还未被取消,则会直接更新该通知相关的属性;如果之前的通知已经被取消,则会重新创建一个新通知。

取消通知
取消通知有以下5种方式:
1、点击通知栏的清除按钮,会清除所有可清除的通知;
2、设置了setAutoCancel()或FLAG_AUTO_CANCEL的通知,点击该通知时会清除它;
3、通过NotificationManager调用cancel(int id)方法清除指定 ID 的通知;
4、通过NotificationManager调用cancel(String tag, int id)方法清除指定TAG和ID的通知;
5、通过NotificationManager调用cancelAll()方法清除所有该应用之前发送的通知。
注意:如果是通过NotificationManager.notify(String tag, int id, Notification notify)方法创建的通知,那么只能通过 NotificationManager.cancel(String tag, int id)方法才能清除对应的通知,调用NotificationManager.cancel(int id)无效。

给Notification设置Flag
我们可以给Notification设置Flag。示例如下:
我们想我们的通知不可以通过点击清除按钮清除,也不可以被滑动清除,只能通过程序中调用cancel方法清除,那么我们可以这样子:
notification.flags |= Notification.FLAG_NO_CLEAR;
注意:flags可以通过|=运算叠加效果。

设置Notification的通知效果
Notification有响铃、震动、呼吸灯三种效果。可以通过setDefaults(int defualts)方法来设置。注意:一旦设置了Default效果,那么自定义效果就会失效。 Default有以下四种属性:
//设置系统默认提醒效果,一旦设置默认提醒效果,则自定义的提醒效果会全部失效。
//添加默认震动效果,需要申请震动权限
//
Notification.DEFAULT_VIBRATE

//添加系统默认声音效果,设置此值后,调用setSound()设置自定义声音无效
Notification.DEFAULT_SOUND

//添加默认呼吸灯效果,使用时须与 Notification.FLAG_SHOW_LIGHTS 结合使用,否则无效
Notification.DEFAULT_LIGHTS

//添加上述三种默认提醒效果
Notification.DEFAULT_ALL

这里我们再介绍几种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

下面我们再看一些关于通知的示例代码

/**
* 最普通的通知效果
*/
private void showNotifyOnlyText() {
   Notification.Builder builder = new Notification.Builder(this)
           .setSmallIcon(R.mipmap.ic_launcher)
           .setContentTitle("我是只有文字效果的通知")
           .setContentText("我没有铃声、震动、呼吸灯,我是最普通的通知");
   mManager.notify(1, builder.build());
}

/**
* 展示有自定义铃声效果的通知
* 补充:使用系统自带的铃声效果:Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");
*/
private void showNotifyWithRing() {
   Notification.Builder builder = new Notification.Builder(this)
           .setSmallIcon(R.mipmap.ic_launcher)
           .setContentTitle("我是伴有铃声效果的通知")
           .setContentText("叮叮叮")
           //调用系统默认响铃,设置此属性后setSound()会无效
           //.setDefaults(Notification.DEFAULT_SOUND)
           //调用系统多媒体裤内的铃声
           //.setSound(Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "2"));
           //调用自己提供的铃声,位于 /res/values/raw 目录下
           .setSound(Uri.parse("android.resource://com.test.notification/" + R.raw.sound));
   //另一种设置铃声的方法
   //Notification notify = builder.build();
   //调用系统默认铃声
   //notify.defaults = Notification.DEFAULT_SOUND;
   //调用自己提供的铃声
   //notify.sound = Uri.parse("android.resource://com.test.notification/" + R.raw.sound);
   //调用系统自带的铃声
   //notify.sound = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "2");
   //mManager.notify(2,notify);
   mManager.notify(2, builder.build());
}

/**
* 展示有震动效果的通知,需要在AndroidManifest.xml中申请震动权限
* <uses-permission android:name="android.permission.VIBRATE" />
*/
private void showNotifyWithVibrate() {
   //震动也有两种设置方法,与设置铃声一样,在此不再赘述
   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);
   mManager.notify(3, builder.build());
}

/**
* 显示带有呼吸灯效果的通知
*/
private void showNotifyWithLights() {
   final Notification.Builder builder = new Notification.Builder(this)
           .setSmallIcon(R.mipmap.ic_launcher)
           .setContentTitle("我是带有呼吸灯效果的通知")
           .setContentText("一闪一闪亮晶晶~")
           //ledARGB 表示灯光颜色、 ledOnMS 亮持续时间、ledOffMS 暗的时间
           .setLights(0xFF0000, 3000, 3000);
   Notification notify = builder.build();
   //只有在设置了标志符Flags为Notification.FLAG_SHOW_LIGHTS的时候,才支持呼吸灯提醒。
   notify.flags = Notification.FLAG_SHOW_LIGHTS;
   //设置lights参数的另一种方式
   //notify.ledARGB = 0xFF0000;
   //notify.ledOnMS = 3000;
   //notify.ledOffMS = 3000;
   mManager.notify(4, builder.build());
}

/**
* 显示带有默认铃声、震动、呼吸灯效果的通知
* 如需实现自定义效果,请参考前面三个例子
*/
private void showNotifyWithMixed() {
   Notification.Builder builder = new Notification.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());
}

/**
* 通知无限循环,直到用户取消或者打开通知栏(其实触摸就可以了),效果与FLAG_ONLY_ALERT_ONCE相反
* 注:这里没有给Notification设置PendingIntent,也就是说该通知无法响应,所以只能手动取消
*/
private void showInsistentNotify() {
   Notification.Builder builder = new Notification.Builder(this)
           .setSmallIcon(R.mipmap.ic_launcher)
           .setContentTitle("我是一个死循环,除非你取消或者响应")
           .setContentText("哈哈哈")
           .setDefaults(Notification.DEFAULT_ALL);
   Notification notify = builder.build();
   notify.flags |= Notification.FLAG_INSISTENT;
   mManager.notify(6, notify);
}

/**
* 通知只执行一次,与默认的效果一样
*/
private void showAlertOnceNotify() {
   Notification.Builder builder = new Notification.Builder(this)
           .setSmallIcon(R.mipmap.ic_launcher)
           .setContentTitle("我就执行一遍")
           .setContentText("好了,已经一遍了~")
           .setDefaults(Notification.DEFAULT_ALL);
   Notification notify = builder.build();
   notify.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
   mManager.notify(7, notify);
}

/**
* 清除所有通知
*/
private void clearNotify() {
   mManager.cancelAll();
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值