11月更文挑战|Android基础-使用Notification通知功能

这是我参与11月更文挑战的第21天,活动详情查看:2021最后一次更文挑战

前言

在应用开发中消息通知是必不可少的一部分。例如IM即时通讯应用中当收到消息时需要弹起通知栏告知用户信息简介来获取信息概要;又或者是电商应用或是新闻应用当有商品促销活动以及重要咨询推送给用户上也需要使用到Notification功能。Notification对于应用来说对于唤起应用并使用有着重要作用,好的消息通知有助于应用使用率,提高应用DAU

在经过大量系统版本迭代,Notification接口使用也发生了很大变化。原先Notification接口在旧版本是在Support包中的,后来做了兼容之后采用了NotificationCompat。由于新版通知有很多内容是服务于高版本Android系统,所以有些通知类型会在一些低版本系统中不能实现相应的效果,其次由于国内手机厂商对系统重新定制导致一些通知样式也不能到达预期效果。这些问题确实存在同时也是Android生态碎片化不可避免的,对于开发者只能在做适配上下功夫确保功能完善。

创建通知

Notification采用Builder设计模式,通知栏的参数配置在Builder中完成,再由Builder来创建通知实例,最后使用NotificationManager来完成显示通知消息。通知简单使用如下代码所示:

mNotificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(NotificationActivity.this, "001")
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setTicker("new message")
                .setContentTitle("My notification")
                .setContentText("Hello World!");

mNotificationManager.notify(1, mBuilder.build());

复制代码

但在高版本系统手机中上述的代码并不能有效启动通知栏提醒,错误日志如下所示:

image.png 在高版本系统中通知方法多了channelId的概念,需要开发者自行注册通知的channelId通道并且向注册通道发起通知才能生效。

修改后的代码如下所示,增加NotificationChannel注册操作:

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

//ChannelId为"001",ChannelName为"my_channel"
NotificationChannel channel = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
    channel = new NotificationChannel("001",
            "my_channel", NotificationManager.IMPORTANCE_DEFAULT);
    channel.enableLights(true); //是否在桌面icon右上角展示小红点
    channel.setLightColor(Color.GREEN); //小红点颜色
    channel.setShowBadge(true); //是否在桌面图标时显示此渠道的通知
    mNotificationManager.createNotificationChannel(channel);
}

NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(NotificationActivity.this, "001")
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setTicker("new message")
                .setContentTitle("My notification")
                .setContentText("Hello World!");
mNotificationManager.notify(1, mBuilder.build());
复制代码

通知Builder参数

下面是NotificationCompat.Builder比较常用的参数方法,像setSmallIconsetContentTitlesetContentText属于必选项不存在系统兼容性问题。而像setLights可能就根据系统自身硬件设备相关来决定是否生效。

Method Introduction
setSmallIcon 设置通知小图标(必须设置项)
setLargeIcon 设置通知大图标
setContentTitle 设置通知标题(必须设置项)
setContentText 设置通知内容(必须设置项)
setDefaults 消息提醒模式 (声音/震动/提示灯...)
setSound 设置自定义消息提醒音
setVibrate 设置震动频率
setLights 设置提示灯显示
setOngoing 设置通知是否可以在通知列表清楚
setOnlyAlertOnce 通知如果已经存在不再提醒
setAutoCancel 消息点击之后是否还在通知栏中显示
setProgress 设置进度,在通知栏上显示进度样式
setContent 用于设置自定义通知
setContentIntent 设置通知点击跳转的内容

创建展开布局通知

创建展开通知主要是为了能够展示更多信息内容。通用的通知样式所能承载展示的内容有限,因此用过可展开显示来提供更多信息。

BigPictureStyle

BigPictureStyle支持显示大图的通知,下面是几个可以设置的参数,通知图片显示通过bigPicture设置bitmap

  • setBigContentTitle
  • setSummaryText
  • bigLargeIcon
  • bigPicture
NotificationCompat.BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle();
bigPictureStyle.setBigContentTitle("Title");
bigPictureStyle.setSummaryText("SummaryText");
Bitmap bigPicture = BitmapFactory.decodeResource(getResources(),R.drawable.android);
bigPictureStyle.bigPicture(bigPicture);
builder.setStyle(bigPictureStyle);
复制代码

BigTextStyle

显示文本通知,可以显示更多文本内容。感觉和inboxStyle差不多。

  • setBigContentTitle
  • setSummaryText
  • bigText
NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
bigTextStyle.setBigContentTitle("Title");
bigTextStyle.bigText("BigText\nBigText\nBigText\nBigText\nBigText");
bigTextStyle.setSummaryText("SummaryText");
builder.setStyle(bigTextStyle);
复制代码

MessagingStyle

消息通知,可快速回复message的通知。好像在Android N以上才能使用。目前手上的测试机是低版本的,没有快速回复的操作项。 addMessage setConversationTitle

NotificationCompat.MessagingStyle messagingStyle = new NotificationCompat.MessagingStyle("UserName");
messagingStyle.addMessage("message",System.currentTimeMillis(),"JulyYu");
messagingStyle.setConversationTitle("Messgae Title");
builder.setStyle(messagingStyle);
复制代码

InboxStyle

可以显示多行文本的通知,通过addline并能增加新的一行文本内容。

  • setBigContentTitle
  • setSummaryText
  • addLine
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
String[] events = new String[]{"1","2","3","4"};
inboxStyle.setBigContentTitle("Event tracker details:");
inboxStyle.setSummaryText("SummaryText");
for (int i=0; i < events.length; i++) {
    inboxStyle.addLine(events[i]);
}
builder.setStyle(inboxStyle);
复制代码

MediaStyle

PS:在高版本的NotificationCompat中已经无法使用了

多媒体播放通知,快捷的多媒体操作控件。可以自定义需要显示的操作控件内容。

  • setShowActionsInCompactView
  • setMediaSession
  • setShowCancelButton
  • setCancelButtonIntent

兼容性问题

因为通知API功能在不断发展更新,在旧系统设备上依旧有在使用。因此官方推荐开发者使用NotificationCompat来创建通知功能,就不需要开发者自行去对版本做兼容性处理。当然一些新功能和API只能在高版本中使用,在旧设备中调用就是无效的。

Android4.1

  1. 支持展开式通知模式
  2. 支持在通知上增加按钮操作
  3. 支持用户在设置中关闭通知

Android4.4

  1. 支持添加通知监听器服务

Android5.0

  1. 支持锁屏提示通知
  2. 增加通知优先级概念

Android7.0

  1. 增加短信通知应用模式
  2. 用户可直接在通知栏中回复通知

Android8.0

  1. 通知使用必须有特定通知渠道注册和联动
  2. 通知优先级的设置修改成了NotificationChannel.setImportance()

参考

猜你喜欢

转载自juejin.im/post/7033011780267966472