Notification的高级使用(适配8.0)

时间:2024-03-31 09:44:50

目录

一、引言
二、效果
三、8.0通知栏适配
四、普通Notification
五、折叠Notification
六、悬挂Notification
七、Notification的显示等级

一、引言

  以前一篇博客记录了Notification的基本使用和一些设置,从纯文本到多View,到声音和震动。附出链接:https://blog.csdn.net/qi85481455/article/details/82895507
。随着手机系统的升级,Google对Notification作出了一些升级,这篇博客纪录的就是升级后通知栏的使用。这里主要讲到三种通知栏的使用(普通通知栏、折叠通知栏、悬挂通知栏)以及8.0通知栏的适配。


二、效果

Notification的高级使用(适配8.0)


三、8.0通知栏适配

  NotificationChannel是android8.0新增的特性,如果App的targetSDKVersion>=26,没有设置channel通知渠道的话,就会导致通知无法展示。
Android O 引入了 通知渠道(Notification Channels),以提供统一的系统来帮助用户管理通知,如果是针对 android O 为目标平台时,必须实现一个或者多个通知渠道,以向用户显示通知。比如聊天软件,为每个聊天组设置一个通知渠道,指定特定声音、灯光等配置。

 private void initNotificationBuild() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel("channel_id", "channel_name",
                    NotificationManager.IMPORTANCE_DEFAULT);
            //是否绕过请勿打扰模式
            channel.canBypassDnd();
            //闪光灯
            channel.enableLights(true);
            //锁屏显示通知
            channel.setLockscreenVisibility(1);
            //闪关灯的灯光颜色
            channel.setLightColor(Color.RED);
            //桌面launcher的消息角标
            channel.canShowBadge();
            //是否允许震动
            channel.enableVibration(true);
            //获取系统通知响铃声音的配置
            channel.getAudioAttributes();
            //获取通知取到组
            channel.getGroup();
            //设置可绕过  请勿打扰模式
            channel.setBypassDnd(true);
            //设置震动模式
            channel.setVibrationPattern(new long[]{100, 100, 200});
            //是否会有灯光
            channel.shouldShowLights();
            getNotificationManager().createNotificationChannel(channel);
        }
        builder = new NotificationCompat.Builder(this, "channel_id");
    }

这里找到一片讲解8.0通知权限很详细的博客,大家感兴趣的话可以去了解一下:

https://www.jianshu.com/p/b83fc1697232

四、普通Notification

首先创建一个Build对象,用PendingIntent控制跳转:

  Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

有了Builder,就可以给Notification添加各种属性:

builder.setContentIntent(pendingIntent);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setAutoCancel(true);
        builder.setContentTitle("普通通知");
        mNotificationManager.notify(0, builder.build());

五、折叠Notification

  折叠式Notification是一种自定义的Notification,用于显示长文本和一些自定义View,这里需要说明的是:展开状态下的视图显示的进程和我们创建视图的进程不在一个进程中,所以我们需要RemoteView。

 //用RemoteView来创建Notification视图
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.view_fold);
        Notification notification = builder.build();
        //指定展开时的视图
        notification.bigContentView = remoteViews;

如果不是折叠式的通知栏,就和普通通知栏显示为一样的,下面是完整代码:

 /**
     * 折叠通知栏
     */
    private void foldNotification() {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        builder.setContentIntent(pendingIntent);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
        builder.setAutoCancel(true);
        builder.setContentTitle("折叠通知");
        //用RemoteView来创建Notification视图
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.view_fold);
        Notification notification = builder.build();
        //指定展开时的视图
        notification.bigContentView = remoteViews;
        mNotificationManager.notify(1, notification);
    }

六、悬挂Notification

  悬挂式Notification是Android 5.0新增加的方式。悬挂式Notification不需要下拉通知栏就可以直接显示出来悬挂在屏幕上方,而且焦点不变,仍在用户操作界面,不会打算用户操作,过几秒会自动消失;主要是调用setFullScreenIntent来将Notification变为悬挂式通知

 Intent hangIntent = new Intent();
        hangIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        hangIntent.setClass(this, MainActivity.class);
        PendingIntent hangPendingIntent = PendingIntent.getActivity(this,0,hangIntent,PendingIntent.FLAG_CANCEL_CURRENT);
        builder.setFullScreenIntent(hangPendingIntent,true);

悬挂式Notification完整代码:

/**
     * 悬挂通知栏
     */
    private void suspendNotification() {
        Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(url));
        PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,0,intent,0);
        builder.setContentIntent(pendingIntent);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));
        builder.setAutoCancel(true);
        builder.setContentTitle("悬挂通知栏");
        //设置点击调转
        Intent hangIntent = new Intent();
        hangIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        hangIntent.setClass(this, MainActivity.class);
        PendingIntent hangPendingIntent = PendingIntent.getActivity(this,0,hangIntent,PendingIntent.FLAG_CANCEL_CURRENT);
        builder.setFullScreenIntent(hangPendingIntent,true);
        mNotificationManager.notify(2,builder.build());
    }

七、Notification的显示等级

5.0对于Notification的管理中加入了等级的显示

等级 说明
NotificationCompat.VISIBILITY_PRIVATE 只有在没有锁屏时会显示通知
NotificationCompat.VISIBILITY_PUBLIC 任何 情况下都显示通知
NotificationCompat.VISIBILITY_SECRET 在pin、password安全锁和没有锁的情况下显示通知

使用方法:

  builder.setVisibility(NotificationCompat.VISIBILITY_SECRET);
   builder.setVisibility(NotificationCompat.VISIBILITY_PRIVATE);
    builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);

以上就是Notification的全部内容了,希望能帮助到你!