一、通知栏简介
通知栏是 Android 应用程序开发中非常重要的一部分,它可以为用户显示未读消息、提醒等,也可以让用户快速进入应用程序。您可以通过通知栏,向用户发送通知消息,比如新消息、新邮件、任务完成等等,这对于抢占用户注意力大有裨益。
二、基础通知栏功能
通知栏具有一些基本的功能,包括展示通知、清除通知、进入应用程序、通知栏折叠和展开。
1、展示通知
下面是通过函数创建的最基础的通知:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(NOTIFICATION_ID, builder.build());
2、清除通知
用户可以通过点击通知栏中的 “清除” 或者 “全部清除” 按钮来清除通知。您也可以通过以下方式来手动清除通知:
notificationManager.cancel(NOTIFICATION_ID);
3、进入应用程序
当用户点击通知栏中的通知时,应用程序会被自动唤醒。您可以使用以下代码片段打开您的应用程序:
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
4、通知栏折叠和展开
通知栏默认是展开的,用户可以通过向下滑动通知栏来切换到折叠模式。在折叠模式下,通知栏只会展示基本信息,并显示通知图标。您可以添加以下代码来支持通知栏的折叠和展开:
NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
bigTextStyle.setBigContentTitle("Big Text Notification");
bigTextStyle.bigText("This is a big text notification,……");
builder.setStyle(bigTextStyle);
三、高级功能
通知栏也具有一些高级的功能,包括声音、震动、悬挂式通知、影响性能的功能等。
1、声音和震动
通知栏支持为通知添加声音和震动效果。以下代码为通知添加了默认的声音和震动效果:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setDefaults(Notification.DEFAULT_ALL);
2、悬挂式通知
通知栏支持显示悬挂式通知,类似于 Facebook Messenger 或者搜狗输入法的方式。以下代码为通知添加了悬挂式通知效果:
Intent resultIntent = new Intent(this, ResultActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(ResultActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setContentIntent(resultPendingIntent)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setFullScreenIntent(resultPendingIntent, true);
3、影响性能的功能
通知栏也包含一些影响性能的高级功能,比如:折叠式通知、消息计数器、进度条和自定义视图。
3.1 折叠式通知
折叠式通知是一种可以折叠和展开的通知,用户可以通过向下滑动通知栏来切换是否展示详细信息。以下代码为通知添加了折叠式通知效果:
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
String[] messages = {"Hello World!", "Meeting at 6 pm!", "Don't forget to bring the document!"};
inboxStyle.setBigContentTitle("Inbox Notification");
for (int i = 0; i < messages.length; i++) {
inboxStyle.addLine(messages[i]);
}
builder.setStyle(inboxStyle);
3.2 消息计数器
消息计数器是一种非常有用的功能,它可以为用户显示未读消息个数。消息计数器通常会显示在应用程序图标上。以下代码为通知添加了消息计数器效果:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setNumber(3);
3.3 进度条
进度条通知是一种在通知栏中显示进度条的通知。以下代码为通知添加了进度条效果:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setProgress(100, 50, false);
3.4 自定义视图
还可以使用自定义视图来创建复杂的通知。以下代码为通知添加了自定义视图效果:
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.custom_notification);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setCustomContentView(remoteViews);
四、总结
通知栏是 Android 应用开发中不可或缺的一部分,它能够向用户展示未读消息、提醒等,让用户快速进入应用程序。本文介绍了通知栏的基本功能和高级功能,包括展示通知、清除通知、进入应用程序、通知栏折叠和展开、声音、震动、悬挂式通知、折叠式通知、消息计数器、进度条和自定义视图。