Android Notification 的四种使用方式

  • 实现通知步骤
    一般实现通知需要如下步骤:
    1.获取 NotificationManager 实例管理通知;
    2.实例 Notification 对象;
    3.管理事件 Intent
    4.发送通知。
    注:如不需在通知出现时,点击时有事件执行,步骤3可以忽略。

  • 1. 普通通知
    获取 NotificationManager 实例:
    NotificationManager 对通知进行管理,调用 ContextgetSystemService() 方法获取。

NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

实例 Notification 对象:

Notification notification= new NotificationCompat.Builder(Context).build();

此时只创建了一个空的 Notification 对象,没有实际作用,可以在build() 方法之前连缀任意多的方法设置 Notification 对象。现在来设置一些基本设置

Notification notification = new NotificationCompat.Builder(Context)
            .setContentText("通知内容")
            .setContentTitle("通知标题")
            .setSmallIcon(android.R.mipmap.ic_launcher_round)
          .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_round))
            .setWhen(System.currentTimeMillis())
            .build();

以上设置了五个方法:
setSmallIcon() 用于设置通知的小图标,只能使用纯 alpha 图层的图片进行设置,小图标会显示在系统状态栏上。(alpha 图层的图片你不知道没关系,UI 会知道的,哈哈,这个我也不知P出来,这里我只是暂时用默认图标代替)
setLargeIcon() 设置通知的大图标,当下拉通知后显示的图标。
setWhen() 指定通知被创建的时间,以毫秒为单位,下拉通知后会将时间显示在相应的通知上。

现在可以发送一个基本通知了

manager.notify(1,notification);

notify() 方法接收两个参数,参数一 id 指定通知的 id,要保证每个通知的 id 是不同的;参数二 Notification 对象,传入之前创建好的即可。
当次通知出现时,点击是不会有任何效果的,这是因为没有关联 Intent,没有还不简单,给他添加一个就可以了,此时会用到 PendingIntent PendingIntent 的获取可以根据需求选择
getActivity()getBroadcast()getService() 等静态方法来获取。

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com"));
    PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
    Notification notification = new NotificationCompat.Builder(Content)
            .setContentIntent(pi)
            .build();

调用 setContentIntent() 方法,传入 PendingIntent 实例即可,当点击同时会打开浏览器进入百度主页。
我们现在在完善下 Notification

    Notification notification = new NotificationCompat.Builder(Context)
            ...
            .setAutoCancel(true)//点击通知头自动取消
            .setDefaults(NotificationCompat.DEFAULT_ALL)//设置铃声及震动效果等
            .build();

控制手机震动等还需要相应的权限

    <uses-permission android:name="android.permission.VIBRATE"/>

也可对铃声,LED灯,震动等分别进行设置

setSound()//铃声
setLights()//LED灯
setVibrate()//震动
  • 2.悬挂式通知
//在 build()之前设置 .setFullScreenIntent()
   Notification builder = new NotificationCompat.Builder(Context);
    Notification notify = builder.setSmallIcon(R.mipmap.ic_launcher_round)
            .setPriority(Notification.PRIORITY_DEFAULT)  //通知的优先级
            .setCategory(Notification.CATEGORY_MESSAGE)  //通知的类型
            .setContentTitle("通知")
            .setAutoCancel(true)
            .setContentIntent(pi)
            .setContentText("Heads - Up Notification on Android 5.0")
            .setFullScreenIntent(pi, true)  //不设置此项不会悬挂,false 不会出现悬挂
            .build();

setPriority() 方法共有5个等级:
1. PRIORITY_MIN - 最低级别(-2);
2. PRIORITY_LOW - 较低级别(-1);
3. PRIORITY_DEFAULT - 默认级别(0);
4. PRIORITY_HIGH - 较高级别(1);
5. PRIORITY_MAX - 最高级别(2);
当发出此类型的通知时,通知会以悬挂的方法显示在屏幕上。

  • 3.折叠式通知

折叠式同时需要借助 RemoteViews 来实现

Notification builder = new NotificationCompat.Builder(Context);
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.sina.com"));

    PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);

    // 未下拉的样式 R.layout.collapsed
    RemoteViews collapsed = new RemoteViews(getPackageName(), R.layout.collapsed);
    collapsed.setTextViewText(R.id.collapsed_text, "关闭状态");

    //下拉后的样式R.layout.show
    RemoteViews show = new RemoteViews(getPackageName(), R.layout.show);


    Notification notify = builder.setAutoCancel(true)
            .setSmallIcon(R.mipmap.ic_launcher_round)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
            .setContentIntent(pi)
            .setContentText("新浪微博")
            .setCustomContentView(collapsed)//下拉前
            .setCustomBigContentView(show)//下拉后
            .build();

    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    manager.notify(0, notify);
  • 4.锁屏通知
    Android 5.0(API level 21)开始,通知可以显示在锁屏上,通过设置选择是否允许敏感的通知内容显示在安全的锁屏上。
//通过 setVisibility() 方法设置即可
...
.setVisibility(VISIBILITY_PUBLIC)
.build();

setVisibility() 方法共有三个选值:
1.VISIBILITY_PRIVATE : 显示基本信息,如通知的图标,但隐藏通知的全部内容;
2.VISIBILITY_PUBLIC : 显示通知的全部内容;
3.VISIBILITY_SECRET : 不显示任何内容,包括图标。

  • 总结
    以上列举了通知的四种用法,关于 Notification 还有很多设置在这里没有写,大家可以自己去挖掘,去尝试。文章如有不足之处,欢迎大家之处,共同进步!
  • 14
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值