#Android学习# 三种Notification的学习

Notification 可以让我们在获取信息的时候,在状态栏、锁屏界面来显示相应的信息。如果没有 Notification,那我们的 QQ 以及 微信以及其他应用就无法主动通知我们,我们就需要时时地看手机来检查是否有新的信息和提醒,如果真的这样子的话,那 Android 的应用的体验就实在是太糟糕了。这也体现了 Notification 在应用中的重要性。今天学习三种 Notification,分别是普通 Notification、折叠式 Notification 和 悬挂式 Notification:

三种 Notification 的特点:

  • 普通 Notification: 首先显示在手机的通知栏、需要下拉通知页才可以看到、显示内容有 APP 图标、标题、部分内容;
  • 折叠式 Notification: 首先显示在手机的通知栏、同样需要下拉通知页才可以看到、显示的内容相对普通 Notificaton 要多;
  • 悬挂式 Notification: 和普通的 Notification 显示的内容相同,但是不需要下拉通知页就直接悬挂在屏幕上方,并且焦点不变,仍在用户操作的界面;

三种 Notification 的效果图:

三种 Notification 的效果图

实现 Notification 的一般步骤:

  • [1.0] 通过 getSystemService() 方法把 NOTIFICATION_SERVICE 传进去,获取系统的通知管理者 notificationManager;
  • [2.0] 通过 Notification 的对象的 Builder 类 Android 3.0(API level 11),提供一种方便的方法来设置通知的各个字段,并且使用 Android 系统平台的通知布局的布局模板来生成 Notification 的提示框架;
  • [3.0] 通过 Notification 的构建者设置内容的意图(用于跳转)、图标、内容等一系列的属性;
  • [4.0] 最后通过 NotificationManager 通知管理者的 notify() 发起通知;

1.0 实现普通 Notification 的代码逻辑:

//[1.0] 获取系统的通知管理者 notificationManager
        notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

NOTIFICATION_SERVICE 字符串是 Content 类里面的一个静态常量;

public void sendNomalNotification() {
        /**
         *  Builder class for Notification objects.
         * notification对象的 Builder 类,提供一种方便的方法来设置通知的各个字段,
         * 并使用平台的通知的布局模板生成内容视图
         */
        builder = new Notification.Builder(mContext);
        //[2.0] 创建一个意图
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://haimabai.github.io/"));
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        //[3.0] 给 notification 的构建者设置内容意图
        builder.setContentIntent(pendingIntent);
        builder.setSmallIcon(R.mipmap.ic_launcher_round);
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),
        R.mipmap.ic_launcher_round));
        builder.setAutoCancel(true);
        builder.setContentTitle("普通通知");
        //设置可见性
        //VISIBILITY_PRIVATE:展示这个通知锁屏的情况下,但是隐藏敏感的或者私有的信息在安全锁屏;
        //VISIBILITY_PUBLIC:展示这个通知在整一个锁屏;
        //VISIBILITY_SECRET:没有揭示任何通知的信息在锁屏的情况下;
        builder.setVisibility(Notification.VISIBILITY_PUBLIC);
        builder.setContentText("这是普通通知的内容");
        notificationManager.notify(0, builder.build());
    }

以上是普通 Notification 的详细实现,主要的实现逻辑是,按钮通过实现点击实现,然后在监听接口的实现方法 onClick() 方法里面调用 sendNomalNotification() 就可以了。
需要注意的一点是,setVisibility()方法的使用,主要是为了根据通知用户信息的重要性设置不同的可见性等级:
- VISIBILITY_PRIVATE:展示这个通知锁屏的情况下,但是隐藏敏感的或者私有的信息在安全锁屏;
- VISIBILITY_PUBLIC:展示这个通知在整一个锁屏;
- VISIBILITY_SECRET:没有通知任何通知的信息在锁屏的情况下;

还有一点,在最后一行 notificationManager.notify(0, builder.build()),notify() 方法有两个参数:
- id: notification 的身份标识符对于该应用
- notification: 一个 Notification 的对象描述展示什么样的通知给用户

2.0 实现折叠式 Notification 的代码逻辑:

public void sendFlodingNotification() {
        builder = new Notification.Builder(mContext);
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://haimabai.github.io/"));
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        builder.setContentIntent(pendingIntent);
        builder.setSmallIcon(R.mipmap.ic_launcher_round);
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),
        R.mipmap.ic_launcher_round));
        builder.setAutoCancel(true);
        builder.setContentTitle("折叠式 Notification");
        RemoteViews remoteViews = new RemoteViews(getPackageName(),
        R.layout.view_fold);
        Notification notification = builder.build();
        notification.bigContentView = remoteViews;
        notificationManager.notify(1, builder.build());
    }

折叠式 Notification 的最大的特点可以镶嵌一个布局进到 Notification 里面,使得通知更加友好,也使得用户从通知上面获得更多的信息;其中 R.layout.view_fold 便是这个镶嵌的布局:

镶嵌的布局原效果图:

镶嵌的布局原效果图

实现这个布局的代码也是挺简单的,具体代码我就不复制粘贴了;

3.0 实现悬挂式 Notification 的代码逻辑:

public void sendFloatingNotification() {
        builder = new Notification.Builder(mContext);
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://haimabai.github.io/"));
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        builder.setContentIntent(pendingIntent);
        builder.setSmallIcon(R.mipmap.ic_launcher_round);
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),
        R.mipmap.ic_launcher_round));
        builder.setAutoCancel(true);
        builder.setContentTitle("悬挂式 Notification");
        //设置点击跳转
        Intent hangIntent = new Intent();
        hangIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        hangIntent.setClass(this, MainActivity.class);
        //如果描述的PendingIntent已经存在,则在产生新的Intent之前会先取消掉当前的
        PendingIntent hangPendingIntent = PendingIntent.getActivity(this, 0,
        hangIntent, PendingIntent.FLAG_CANCEL_CURRENT);
        builder.setFullScreenIntent(hangPendingIntent, true);

        notificationManager.notify(2, builder.build());
    }

以上三个 Notification 的代码逻辑展示到移动端,只需要实现按钮的点击事件,然后直接调用对应方法就可以;

当然,目前 Android 8.0(API level 26)或者更高的系统,支持着更好的通知方式,例如:

  • APP 可以直接显示在应用的图标上面

  • 用户可以直接长按 APP 的图标去查看通知

如果你想了解、以及使用更多的 Notification,推荐去翻 Google 的官方文档,哈哈

猜你喜欢

转载自blog.csdn.net/HongHua_bai/article/details/77947587