Android Notification 使用

1. 基本使用

通知 (Notification) : 当某个应用程序希望向用户发送一些信息,但是,该应用又不在前台运行.就可以借助通知来实现.

1.1 使用步骤

  • 获取NotificationManager 对象管理通知.

通过使用 ContextgetSystemService("服务名称") 方法获取,使用的服务名称是 Context.NOTIFICATION_SERVICE .

// 1. 获取NotificationManager对象.
        NotificationManager nm = (NotificationManager) getSystemService(
                Context.NOTIFICATION_SERVICE);
  • 构建Notification对象.
    由于Android各个版本中Notification的API变更较大.因此我们可以使用 support-v4包中的NotificationCompat 类,使用这个类中的 Builder 构造器来构造一个Notification对象,这样就可以保证在所有的Android系统上都可以正常工作.
// 最基本代码
Notification noti = new NotificationCompat.Buiilder(context).build();

在获取Notification之前,可以使用链式进行设置更多属性.

// 获取Notification对象之前,设置相关属性.
Notification notification = new NotificationCompat.Builder(MainActivity.this)
                // 设置通知文本标题
                .setContentTitle("This is the Content Title")
                // 设置通知文本内容
                .setContentText("This is the Content text")
                // 通知被创建的时间,以毫秒为单位.
                .setWhen(System.currentTimeMillis())
                // 设置小图标,也就是在状态栏上显示那个小的图标.
                .setSmallIcon(R.mipmap.ic_launcher)
                // 设置大图标,将状态栏展开,看到的通知图标.
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
                // 设置点击通知后启动的 PendingIntent
                .setContentIntent(pi)
                // 设置自动取消.否则状态的图标一直存在,也就是说通知没有被取消
                .setAutoCancel(true)
                // 构建出Notification对象.
                .build();

PendingIntent 和Intent非常类似,但是 Intent 更倾向于立即去执行某个动作,PendingIntent则是过一段时间去执行某件事.PendingIntnet 主要提供了一下几个getActivity(),getBroadcaset(),getService()静态方法来过去PendingIntent实例,可以根据具体需求进行选择.他们的参数都是一样的具体含义如下 :

第一个参数 : Context
第二个参数 : 通常用不到,传入0即可,
第三个参数 : 一个Intent对象.
第四个参数 : PendingIntent行为模式参数.可取值有四个
    (1) FLAG_ONE_SHOT : 这个PendingIntent只能使用一次.
    (2) FLAG_NO_CREATE : 如果PendingIntent 不存在则返回,不会去创建新的PendingIntent.
    (3) FLAG_CANCEL_CURRENT : 如果PendingIntent 启动的组件已经存在则在生成新的组件前取消正在显示的组件.
    (4) FLAG_UPDATE_CURRENT : 如果存在则重用,但是更新其中的数据.
  • 发送通知
    使用NotificationManager的 notify(int,Notification) 方法发送通知.
// 3. 发送通知
nm.notify(1,notification);
  • 取消通知

    • 构造Notification时设置 setAutoCancel(true0 .
    // 设置自动取消.
    new NotificationCompat.Builder(context)
        .setAutoCancel(true)
        .build();
    • 显示的调用NotificationManager的cancel(通知id)

    通知id 也就是在 notify(1,noti) 的第一个参数.

    // 1. 获取NotificationManager对象.
    NotificationManager nm = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);
    // 取消 id 为1 的通知.
    nm.cancel(1);

完整示例代码 :

// 测试通知的基本用法.
private void notificationTest1() {

    // 0. 构建 PendingIntent
    Intent intent = new Intent(MainActivity.this, NotiActivity.class);
    PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
    // 1. 获取NotificationManager对象.
    NotificationManager nm = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);

    // 2. 创建Notification对象.
    Notification notification = new NotificationCompat.Builder(MainActivity.this)
            .setContentTitle("This is the Content Title") // 设置标题
            .setContentText("This is the Content text")   // 设置内容
            .setWhen(System.currentTimeMillis())          // 设置创建时间
            .setSmallIcon(R.mipmap.ic_launcher)           // 设置小图标,在状态栏上的小图标.
            // 设置大图标,将状态栏下拉后的图标.
            .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
            .setContentIntent(pi) // 设置要启动的Activity.
            .setAutoCancel(true)
            .build();
    // 3. 发送通知
    nm.notify(1,notification);
}

2. 通知高级用法

2.1 设置通知声音

NotificationCompat.Builder 的 setSound(uri) 方法可以设置在通知发送的时候播放一段音频.

注意 : 在 /system/media/audio/ringtones 目录下有很多音频文件

// 设置通知声音
new NotificationCompat.Builder(context)
    .setSound(Uri.fromFile(new File("system/media/audio/ringtones/luna.ogg")))
    .build();

2.2 设置震动

使用 setVibrate(long[]) 来设置通知震动,该数组中 下标为奇数的位置是静止时间,下标为偶数的表示是震动时间段时长. 单位是毫秒.

使用震动功能需要申请权限 : <uses-permission android:name="android.permission.VIBRATE">

// 立即震动 1s , 暂停 1s , 震动 1s.
new NotificationCompat.Builder(context)
    .setVibrate(new long[]{0,1000,1000,1000})
    .build();

2.3 使用 LED 闪光灯

可以使用 setLights() 方法来实现闪光灯效果. 该方法接收三个参数;第一个参数表示LED灯的颜色;第二个参数表示LED亮起的时长;第三个参数代表LED暗去的时长.

// 设置闪光灯效果.
new NotificationCompat.Builder(context)
    .setLights(Color.GREEN,1000,1000)
    .build();

2.4 使用系统默认的通知效果.

new NotificationCompat.Builder(contex)
    .setDefaults(NotificationCompat.DEFAULT_ALL)
    .build();

2.5 设置富文本

new NotificationCompat.Builder(context)
    .setStyle(new NotificationCompat.BigTextStyle().bigText("AAAAAAAAAA,如果该文本会换行显示,而不是用省略号代替.."))
    .build();

2.6 设置富文本图片.

// 在内容区设置图片.
new NotificationCompat.Builder(context)
    .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResource(),R.drawable.big_image)))
    .build();

2.7 设置优先级

通知可以设置优先级,有5个常用的优先级值.

1. PRIORITY_DEFAULT : 默认程度,和不设置一样.
2. PRIORITY_MIN : 低优先级.
3. PRIORITY_LOW : 较低.
4. PRIORITY_HIGH : 较高.
5. PRIORITY_MAX : 最大. 会在顶部弹出,就像短信和 QQ 那样.

使用 setPriority() 方法设置

new NotificationCompat.Builder(context)
    .setPriority(NotificationCompat.PRIORITY_MAX)
    .build();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值