Android Notification 技术详解

Notification

创建:

Google 在5.0 开始使用builder来创建Notification;

Notification noti = new Notification.Builder(mContext)
         .setContentTitle("New mail from " + sender.toString())
         .setContentText(subject)
         .setSmallIcon(R.drawable.new_mail)
         .setLargeIcon(aBitmap)
         .build();

为了向下兼容 google在V4包中提供了NotificationCompat.build()来创建

发布通知依然是通过NotificationManager notify(id ,notification);

每个通知栏有这一般固定的样式通知栏的固定高度64dp 如下图:


setSmallIcon 设置小图标

setCotent 设置内容

setContentTitle 设置标题

setLargeIcon 设置大图标 (未设置大图标时 小图标显示在左边)

setTicker 设置状态栏中提示信息

一个Notification 至少要包含 icon title message ;

除了normal view Notification 还一种 big viewNotification

在 Notification 中通过setStyle来设置样式其中包含4种样式

Notification.BigPictureStyleNotification.BigTextStyleNotification.InboxStyleNotification.MediaStyle

当大视图的通知在通知栏的顶部时会自动展开隐藏的内容,当大视图通知不处于顶部是需要通过手势来展开内容

Notifications in the notification drawer appear in two mainvisual styles, normal view and big view. The big view of a notification onlyappears when the notification is expanded. This happens when the notificationis at the top of the drawer, or the user clicks the notification.

如果要想使得大视图通知处于顶部可以通过设置通知的优先级来实现

Notification的通知共分为5个界别 :MAX HIGHDEFAULT LOW MIN

在5.0系统中 当级别较高的通知到达时 会以Heads_up Notification的形式展现给用户


When a high-priority notification arrives(see right), it is presented to users for a short period of time with anexpanded layout exposing possible actions.

After this period of time, the notificationretreats to the notification shade. If a notification's priority is flagged as High,Max, or full-screen, it gets a heads-up notification.

在5.0系统允许用户在锁屏的状态下查看通知如果需要让用户在锁屏的状态下查看app的通知,需要设置Notification的可见级别setVisiblity();可见级别分为3个,即:

VISIBILITY_PUBLIC默认

VISIBILITY_PRIVATE在锁屏状态下显示基本信息可以设置更改

Notification.VISIBILITY_SECRET. 显示最少的信息

通知不仅可以显示推送的内容还可以通过setAction 设置动作但我们应该尽量少的为通知添加动作,同时还可以处理一下事件比如 click和dismiss 均可以通过paddingintent 来处理相关的事件例如构建一个paddingintent

Intent resultIntent = new Intent(this, ResultActivity.class);
...
// Because clicking the notification opens a new ("special") activity, there's
// no need to create an artificial back stack.
PendingIntent resultPendingIntent =
    PendingIntent.getActivity(
    this,
    0,
    resultIntent,
    PendingIntent.FLAG_UPDATE_CURRENT
);

建立完成intent 只需要 setContentIntent 即可设置Notification的点击行为

同样

处理使用默认布局样式之外还可以使用RemoteView 来实现自定义的Notification 布局样式并通过setContent 来设置

http://www.android-doc.com/reference/android/widget/RemoteViews.html

Notification设计指南

1.        Make it personal  通过设置大图标 将通知设置成专人专属

2.        Navigate to the right place 导航到正确的位置

3.        Correctly set and manage notification priority 设置正确的权限

4.        Set a notification category 为通知设置分类

5.        Summarize your notifications 总结通知

6.        Make notifications optional 设置通知开关操作

Use distincticons 使用不同的图标 Do

Look at the notification icons Android appsalready provide and create notification icons for your app that aresufficiently distinct in appearance.

Do

Use the proper notificationicon style for smallicons, and the Material Light actionbar icon style for youraction icons.

Do

Keep your icons visually simple, avoidingexcessive detail that is hard to discern.

Don't

Place any additional alpha (dimming orfading) into your small icons and action icons; they can have anti-aliasededges, but because Android uses these icons as masks (that is, only the alphachannel is used), the image should generally be drawn at full opacity.

Don't

Use color to distinguish your app fromothers. Notification icons should only be a white-on-transparent backgroundimage.

7.        Pulse thenotification LED appropriately  适当通知

8.        Building Notifications That Users CareAbout 搭建用户关心的通知

9.        Interacting with Notifications  与通知相互作用

图标设计规范

Android的图标设计大小应该为 48dp和1dp的描边当规模到400% 应该为192X192dp

Androidexpects product icons to be provided at 48dp, with edges at 1dp. When youcreate the icon, maintain the 48-unit measure, but scale it to 400% at 192 x192 dp (the edge becomes 4dp).

https://www.google.com/design/spec/style/icons.html#icons-product-icons

发布了56 篇原创文章 · 获赞 3 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/haoxuezhe1988/article/details/47171713