Android Notification通知栏开发详解

来源:互联网 发布:linux 统计文件夹个数 编辑:程序博客网 时间:2024/06/04 20:07

Notification是在你的应用常规界面之外展示的消息。当app让系统发送一个消息的时候,消息首先以图表的形式显示在通知栏。要查看消息的详情需要进入通知抽屉(notificationdrawer)中查看。通知栏和通知抽屉(notificationdrawer)都是系统层面控制的,你可以随时查看,不限制于app。



图 1.通知栏的通知


图 2. notificationdrawer中的通知。


Notification 的设计 


作为android UI中很重要的组成部分,notification拥有专属于自己的设计准则。如果想了解如何设计notification以及其交互,请阅读android 设计指南的notification一章。


Notification的界面元素


在通知抽屉中的notification有两种显示方式,取决于你的android版本以及notification drawer的状态:

普通视图

这种风格是notification drawer的标准显示方式。

宽视图

指你的notification被展开的时候会显示更大的视图,这种风格是android4.1之后才有的新特性。

下面将详细介绍两种情况。


普通视图


在图通视图中,notification最高64dp,即使你创建了一个宽视图风格的notification,在未展开的情况下也是以普通大小显示出来。下面是一个普通的notification。

图 3.正常状态下的notification 


蓝色指示框所代表的的意思如下:

1.标题

2.大图标

3.通知内容

4.通知数据

5.小图标

6.Notification的发布时间。可以通过调用setWhen()设置一个明确的时间,默认是系统收到该notification的时间。


宽视图


只有当notification被展开的时候这种宽视图的notification才会出现,通过手势操作可以展开一个普通的notification(如果能展开的话)。这种风格的notification从android4.1以后才开始支持。下面的截图展示了inbox风格的notification:

图 4.宽视图notification


你应该注意到了这种notification其实跟普通的没多大差别,唯一的区别在于数字7-详情区域。不同宽视图notification这里的显示是有区别的,有如下几种风格:

大图标风格

详情区域显示一个最高位256dpbitmap

文字风格

详情区域显示一段文字

消息盒子风格(Inbox style

详情区域显示几行文字

至于如何设置宽视图风格的notification可以参考这篇文章Applyinga big view style to a notification.


创建notification


首先将notification的一些UI信息以及相关动作赋予NotificationCompat.Builder对象,然后通过NotificationCompat.Builder.build()来获得notification对象自己;然后调用NotificationManager.notify()向系统转交这个通知。一个notification对象需要包含如下内容:

小图标(setSmallIcon()获取)

标题(setContentTitle()获取)

详情文字(setContentText()获取)


除此之外,其余内容都是可选的,要了解这些可选的内容参考NotificationCompat.Builder的文档。


Notification的动作与行为


虽然这也是可选的,但是你还是应该为你的notification至少添加一种行为:允许用户通过点击notification进入一个activity中进行更多的查看或者后续操作。一个notification可以提供多种动作,而且你也应该让用户点击一个notification之后能总是有相应的响应动作,通常是打开一个activity。你还可以在notification中添加能响应点击事件的button,比如延迟一下闹钟,或者立即回复一条短消息。

在notification内部,一个动作本身是被定义在一个PendingIntent中,PendingIntent包含了一个用于启动你app中activity的intent。要将PendingIntent和一个手势联系起来,你需要调用合适的NotificationCompat.Builder方法。比如你想在点击notification文字的时候启动activity,你需要调用NotificationCompat.Builder的setContentIntent()来添加PendingIntent。启动一个activity是notification动作响应中最普遍的一类。


创建一个简单的notification


下面的代码片段演示了一个打开activity的notification,注意这里获取PendingIntent是通过创建TaskStackBuilder对象:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
NotificationCompat.Builder mBuilder =
        newNotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("My notification")
        .setContentText("Hello World!");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = newIntent(this, ResultActivity.class);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(ResultActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
        stackBuilder.getPendingIntent(
            0,
            PendingIntent.FLAG_UPDATE_CURRENT
        );
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());


就是这么简单,通过上面的代码你就能为你的用户发出一个系统通知。


notification设置宽视图样式


要获得一个能展开的宽视图样式的notification首选创建一个普通的NotificationCompat.Builder对象,然后调用Builder.setStyle()。记住这种notification只有4.1版本以上才支持。

下面的例子演示了在上面的普通notification的基础上添加宽视图风格的notification:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
NotificationCompat.Builder mBuilder = newNotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.notification_icon)
    .setContentTitle("Event tracker")
    .setContentText("Events received")
NotificationCompat.InboxStyle inboxStyle =
        newNotificationCompat.InboxStyle();
String[] events = newString[6];
// Sets a title for the Inbox style big view
inboxStyle.setBigContentTitle("Event tracker details:");
...
// Moves events into the big view
for(int i=0; i < events.length; i++) {
    inboxStyle.addLine(events[i]);
}
// Moves the big view style object into the notification object.
mBuilder.setStyle(inBoxStyle);
...
// Issue the notification here.

1 0
原创粉丝点击