Android通知功能

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Android通知功能

blue33sun   2022-05-24 我要评论

这篇文章并未详细介绍通知相关的api,而是作者自己对通知的一些大致总结,以便日后查看,请读者自行参考阅读。

andorid关于通知在多个sdk版本中均有修改,因此部分api涉及到版本兼容的问题。编程中我们使用NotificationCompat来实现通知的相关功能。

1.通知中添加按钮的方式

  • Notification可以通过直接调Notification.Builder.addAction(int icon, CharSequence title, PendingIntent intent)或者Notification.Builder.addAction(Action action)来添加按钮
  • 可以通过设置RemoteView自定义布局的方式来添加按钮;

2.通知的各种style

如果普通的通知样式无法满足项目需求,我们可以使用android提供的各种style。
目前style的种类包括BigTextStyle(超长文本)、InboxStyle(多行/列表)、BigPictureStyle(大图片)、MessagingStyle(多条消息)、MediaStyle(started Android Oreo)。

3.自定义的通知View

如果上面普通通知栏和各种style不能满足需求,也可以自己定义通知栏视图remoteVIew,并将其设置给通知的ContentView即可。在android随后更新的sdk版本中增加了BigContentView(started android Jelly_bean)、heasUpContentView(started android Lollipop),分别用于显示通知栏的大视图,悬挂视图。

4.锁屏时展示通知

自android Lollipop版本开始支持锁定屏幕时显示通知。用户可以通过“设置”选择是否将通知显示在锁定屏幕上,并且您可以指定您应用中的通知在锁定屏幕上是否可见。通过 setVisibility() 并指定以下值之一:

  • VISIBILITY_PUBLIC 显示通知的完整内容。
  • VISIBILITY_SECRET 不会在锁定屏幕上显示此通知的任何部分。
  • VISIBILITY_PRIVATE 显示通知图标和内容标题等基本信息,但是隐藏通知的完整内容。设置 VISIBILITY_PRIVATE 后,您还可以提供其中隐藏了某些详细信息的替换版本通知内容。例如,短信 应用可能会显示一条通知,指出“您有 3 条新短信”,但是隐藏了短信内容和发件人。要提供此替换版本的通知,请先使用 NotificationCompat.Builder 创建替换通知。创建专用通知对象时,请通过 setPublicVersion() 方法为其附加替换通知。

5.快捷回复

自android Nougat版本开始增加了通知栏的快捷回复功能,具体实现步骤:给通知栏添加一个action(一般是快捷服务按钮),该action初始化时传入PendingIntent和RemoteInput即可。

//通知快速回复
public void quickReplyClick(View view){
        NotificationCompat.Builder builder = getBuilder();

        Intent intent = new Intent(MainActivity.this, ThirdActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        RemoteInput remoteInput = new RemoteInput
                .Builder("RemoteInputKey")
                .setLabel("RemoteInputLabel")
                .build();

        NotificationCompat.Action action = new NotificationCompat.Action
                .Builder(R.drawable.air, "回复", pendingIntent)
                .addRemoteInput(remoteInput)
                .build();

        builder.addAction(action);

        // 发送该通知
        notifyDefaultPriority(++mNotificationId,builder);
    }

6.通知分组功能

自android Nougat版本开始增加了通知的分组功能。在android Nougat版本及以上,如果同一应用发出 4 条或更多条通知且未指定分组,则系统会自动将这些通知分为一组。

通知分组的实现方式(只列举关键方法):

private final String GROUP_NOTIFICATION_ONE = "GROUP1";
private final int  GROUP_NOTIFICATION_ID = 0;

    //通知分类
    public void classifyClick(View view){
        NotificationCompat.Builder builder1 = getBuilder();//创建一个普通的通知buidler对象,方法很简单
        builder1.setGroup(GROUP_NOTIFICATION_ONE);//设置group
        notifyDefaultPriority(++mNotificationId,builder1);//弹出第一条通知
        notifyDefaultPriority(++mNotificationId,builder1);//弹出第二条通知

        NotificationCompat.Builder builder2 = getBuilder();//创建一个普通的通知buidler对象,方法很简单
        builder2.setContentTitle("test classify");
        builder2.setGroup(GROUP_NOTIFICATION_ONE);//设置相同的group
        builder2.setGroupSummary(true);//这一句必须要,这条通知是作为summary notification(我的理解是将已经发送的相同group的通知进行归类)
        notifyDefaultPriority(GROUP_NOTIFICATION_ID,builder2);//这条通知的notification id是个常量,弹出通知
    }

7.通知通道NotificationChannel

自android Oreo版本开始增加了通知通道的概念,在targetSdkVersion>=26时弹出通知需要做兼容处理:为Notification设置channel,否则通知将会弹出失败。

private void setNotifyChannel(NotificationCompat.Builder builder,String channelId,String channelName,int importance){
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
            NotificationManager nm = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
            NotificationChannel notificationChannel = nm.getNotificationChannel(channelId);
            if(notificationChannel==null){
                notificationChannel = new NotificationChannel(channelId,channelName,importance);
                nm.createNotificationChannel(notificationChannel);
            }
            builder.setChannelId(channelId);
        }
    }

8.通知的重要程度

Android 利用通知的重要程度来决定通知应在多大程度上干扰用户(视觉上和听觉上)。通知的重要程度越高,干扰程度就越高。

  • 在搭载 Android Oreo(API 级别 26)及更高版本的设备上,通知的重要程度由通知发布到的渠道NotificationChannle的 importance 决定。用户可以在系统设置中更改通知渠道的重要程度。
  • 在搭载 Android 7.1(API 级别 25)及更低版本的设备上,每条通知的重要程度均由通知的 priority 决定。

以上便是通知的相关知识~
其他可参考官网

这里增加说明下可能会触发悬浮式通知的条件示例:

  • 用户的 Activity 处于全屏模式(应用使用 fullScreenIntent)。
  • 通知的优先级很高,且在搭载 Android 7.1(API 级别 25)及更低版本的设备上使用铃声或振动。
  • 在搭载 Android 8.0(API 级别 26)及更高版本的设备上,通知渠道的重要程度比较高。

对应的有三种实现方式:

为Notification设置全屏时的PendingIntent:setFullScreenIntent(PendingIntent intent, boolean highPriority)即可(第二个参数表示是否是高优先级,需传值true。在android Oreo的平台上则需要NotificationChannel的优先级设置为IMPORTANCE_HIGH或者IMPORTANCE_MAX才有效)

为Notification设置优先级setPriority(NotificationCompat.PRIORITY_HIGH)或者builder.setPriority(NotificationCompat.PRIORITY_MAX)(在android Oreo的平台上只需要NotificationChannel的优先级设置为IMPORTANCE_HIGH或者IMPORTANCE_MAX),同时android 7.1及以下的平台还需要设置setSound(Uri sound)才行(android 8.0平台无需设置震动或铃声)

在android Lollipop及以上平台,可以自定义悬挂视图remoteView,将其设置为Notification的heasUpContentView即可。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们