亲宝软件园·资讯

展开

Android通知功能

blue33sun 人气:0

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

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

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

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() 并指定以下值之一:

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 利用通知的重要程度来决定通知应在多大程度上干扰用户(视觉上和听觉上)。通知的重要程度越高,干扰程度就越高。

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

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

对应的有三种实现方式:

为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即可。

加载全部内容

相关教程
猜你喜欢
用户评论