您的位置:首页 > 其它

读书笔记 多媒体(一)——通知notification

2016-06-13 14:18 302 查看
Notification
思维导图结构

基本用法
创建

notification更新

notification移除

Preserving Navigation when Starting an Activity通知引起的导航问题

通知的优先级setPriorityint pri

notification通知的flag
FLAG_AUTO_CANCEL

FLAG_FOREGROUND_SERVICE

FLAG_GROUP_SUMMARY

FLAG_HIGH_PRIORITY

FLAG_INSISTENT

FLAG_LOCAL_ONLY

FLAG_NO_CLEAR

FLAG_ONGOING_EVENT

FLAG_ONLY_ALERT_ONCE

FLAG_SHOW_LIGHTS

通知类别setCategory

对显示在安全锁定屏幕上的信息的控制

Notification视觉风格
标准视图

大视图

进度条样式的通知此方法在40及以后版本才有用
有进度的setProgressmax progress false

循环流动setProgress0 0 true

自定义通知

设定提示响应
1方法setDefaultsint defaults NotificationCompatBuilder中的方法用于提示

2方法setVibratelong pattern

3方法setLightsintledARGB intledOnMS intledOffMS

通知的设计注意点

PendingIntent
pendingIntent的获取

PendingIntent的Flag

Android开发陷阱利用PendingIntent传递唯一的Intent

参考链接

demo链接

Notification

使用场景:当应用程序希望向用户发出一些提示msg,而应用程序又不在前台运行时,通过通知来实现。通知算是Android中比较常用的一个功能,可以保持自己App的长存,在用户没有进入App的时候,也提供了与用户交互的可能。

思维导图结构

思维导图的大体结构(按照各个节点延伸拓展学习)

Notificaiton – service – BroadcastReceiver – Intent(flag、Action等属性应用) – PendingIntent

感慨:

一个Notificaiton通知的拓展使用就要涉及与4大组建的配合,所以学好整体的知识体系。

联系:

1.由于service 是在后台运行,所以它意图做什么我们看不到,可以通过Notificaiton 来显示提醒(如音乐的后台播放)。

2.service服务和BroadcastReceiver广播相结合,在加上Notificaiton 显示(如程序的后台更新)。

3.Intent作为意图处理,和Notificaiton的点击时间紧密结合在了一起,并且与BroadcastReceiver和service的联系也紧密不可以分割。

(service 在后台之后通过BroadcastReceiver来通知Notificaiton 显示相关东西,在通过Intent完成用户的意图操作)

相关文档:Activity启动模式 及 Intent Flags 与 栈 的关联分析

基本用法

创建

可以在活动、广播和服务中创建,但在后两者中比较经常。(前台后台)

1、NotificationManager获取

用Context中的getSysTemService(服务名)来获取,接收一个字符串用于确定获取系统哪个服务。

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


2、Notification对象

NotificationCompat.Builder.build()来获得notification对象自己


一个notification对象需要包含如下内容:(最少)

小图标(setSmallIcon()获取)

标题(setContentTitle()获取)

详情文字(setContentText()获取)

NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!");


mBuilder.setContentTitle("测试标题")//设置通知栏标题
.setContentText("测试内容") /<span style="font-family: Arial;">/设置通知栏显示内容</span>
.setContentIntent(getDefalutIntent(Notification.FLAG_AUTO_CANCEL)) //设置通知栏点击意图
//  .setNumber(number) //设置通知集合的数量
.setTicker("测试通知来啦") //通知首次出现在通知栏,带上升动画效果的
.setWhen(System.currentTimeMillis())//通知产生的时间,会在通知信息里显示,一般是系统获取到的时间
.setPriority(Notification.PRIORITY_DEFAULT) //设置该通知优先级
//  .setAutoCancel(true)//设置这个标志当用户单击面板就可以让通知将自动取消
.setOngoing(false)//ture,设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)
.setDefaults(Notification.DEFAULT_VIBRATE)//向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置,使用defaults属性,可以组合
//Notification.DEFAULT_ALL  Notification.DEFAULT_SOUND 添加声音 // requires VIBRATE permission
.setSmallIcon(R.drawable.ic_launcher);//设置通知小ICON


3、调用notify()发送通知

notify()接收两个参数(id, notification);注意,id和通知是唯一绑定的关系。

manager.notify(1, notification);


notification更新

当你需要为同一类型事件多次发送一个通知,要避免完全的重新创建一个notification。考虑更新之前的通知,要么修改或者增加它的一些值,或者两者都。

修改通知

To set up a notification so it can be updated, issue it with a notification ID by callingNotificationManager.notify(ID, notification). To update this notification once you’ve issued it, update or create a NotificationCompat.Builder object, build a Notification object from it, and issue the Notification with the same ID you used previously。

其实,意思就是:直接再重新创建或者去更新builder(比如用之前的builder去调用设置函数来修改),然后用这个创建一个notification,在notify()发送和需要更新的通知一样的id.

//不仅仅是有更新,还有固定持续时间进度条
int id = 1;
...
mNotifyManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(this);
mBuilder.setContentTitle("Picture Download")
.setContentText("Download in progress")
.setSmallIcon(R.drawable.ic_notification);
// Start a lengthy operation in a background thread
new Thread(
new Runnable() {
@Override
public void run() {
int incr;
// Do the "lengthy" operation 20 times
for (incr = 0; incr <= 100; incr+=5) {
// Sets the progress indicator to a max value, the
// current completion percentage, and "determinate"
// state
mBuilder.setProgress(100, incr, false);
// Displays the progress bar for the first time.
mNotifyManager.notify(id, mBuilder.build());
// Sleeps the thread, simulating an operation
// that takes time
try {
// Sleep for 5 seconds
Thread.sleep(5*1000);
} catch (InterruptedException e) {
Log.d(TAG, "sleep failure");
}
}
// When the loop is finished, updates the notification
mBuilder.setContentText("Download complete")
// Removes the progress bar
.setProgress(0,0,false);
mNotifyManager.notify(id, mBuilder.build());
}
}
// Starts the thread by calling the run() method in its Runnable
).start();


notification移除

1、Notification自己维护

在builder对象时候使用setAutoCancel()方法设置是否维护,传递一个boolean类型的数据。

2、使用NotificationManager通知管理器对象来维护,它通过notify()发送通知的时候,指定的通知标识Id来操作通知

使用cancel(int)来移除一个指定的通知,也可以使用cancelAll()移除所有的通知。

Preserving Navigation when Starting an Activity通知引起的导航问题

在大多数情况下,该位置是某个数据项目(例如消息)的详情视图,但如果是存档通知,那么也可能是摘要视图。 如果您的应用将用户带到应用顶层以下的任何位置,可将导航插入应用的返回栈,这样用户就可以通过按下系统返回按钮返回至顶层。

https://developer.android.com/training/notify-user/navigation.html

通知的优先级——setPriority(int pri)

优先级最高的通知,在抽屉最顶端。

==》测试过程,发现怎么设置都没用。。。。。有知道的小伙伴吗???



如何选择合适的优先级

1、DEFAULT、HIGH 和 MAX 是中断优先级别,在活动过程中有中断用户的风险。 为了避免打扰应用的用户,中断优先级仅保留用于以下通知 :

- 涉及另一个用户

- 时间敏感

- 可能会立即改变用户在现实世界中的行为

2、设置为 LOW 和 MIN 的通知可能仍然对用户很重要: 很多通知(如果不是绝大多数)不需要用户立即注意,也不需要振动,但仍然包含用户选择查看通知时将会觉得重要的信息。 LOW 和 MIN优先级通知的条件包括:

- 不涉及其他用户

- 不属于时间敏感型

- 包含用户可能感兴趣但可选择在空闲时浏览的内容

收到高优先级通知时,它会向用户短时间显示一个包含可选操作的展开布局。之后,通知会缩回通知栏。如果通知的优先级标志为高、最大或全屏,则会得到浮动通知。

浮动通知的范例

1. 使用设备时来电

2. 使用设备时闹铃

3. 新的短信

4. 电池电量过低

Notification.PRIORITY_DEFAULT

Notification.PRIORITY_HIGH

Notification.PRIORITY_LOW
Notification.PRIORITY_MAX

Notification.PRIORITY_MIN


notification通知的flag

功能:提醒标志符,向通知添加声音、闪灯和振动效果等设置达到通知提醒效果,可以组合多个属性。

添加方式:实例化通知栏之后通过给他添加.flags属性赋值。

Notification notification = mBuilder.build();
notification.flags = Notification.FLAG_AUTO_CANCEL;


FLAG_AUTO_CANCEL

//用户单击通知后自动消失

Constant Value: 16 (0x00000010)

FLAG_FOREGROUND_SERVICE

//表示正在运行的服务. This will normally be set for you by startForeground(int, Notification).

Constant Value: 64 (0x00000040)

FLAG_GROUP_SUMMARY

Added in API level 20

int FLAG_GROUP_SUMMARY

Bit to be bitswise-ored into the flags field that should be set if this notification is the group summary for a group of notifications. Grouped notifications may display in a cluster or stack on devices which support such rendering. Requires a group key also be set using setGroup(String).

Constant Value: 512 (0x00000200)

FLAG_HIGH_PRIORITY

//正数

Obsolete flag indicating high-priority notifications; use the priority field instead.

Constant Value: 128 (0x00000080)

FLAG_INSISTENT

//让声音、振动无限循环,直到用户响应 (取消或者打开)

Constant Value: 4 (0x00000004)

FLAG_LOCAL_ONLY

Bit to be bitswise-ored into the flags field that should be set if this notification is relevant to the current device only and it is not recommended that it bridge to other devices.

Constant Value: 256 (0x00000100)

FLAG_NO_CLEAR

//只有全部清除时,Notification才会清除 ,不清楚该通知(QQ的通知无法清除,就是用的这个)

Constant Value: 32 (0x00000020)

FLAG_ONGOING_EVENT

//发起正在运行事件(活动中)

Bit to be bitwise-ored into the flags field that should be set if this notification is in reference to something that is ongoing, like a phone call. It should not be set if this notification is in reference to something that happened at a particular point in time, like a missed phone call.

Constant Value: 2 (0x00000002)

FLAG_ONLY_ALERT_ONCE

//发起Notification后,铃声和震动均只执行一次

Bit to be bitwise-ored into the flags field that should be set if you would only like the sound, vibrate and ticker to be played if the notification was not already showing.

Constant Value: 8 (0x00000008)

FLAG_SHOW_LIGHTS

//三色灯提醒,在使用三色灯提醒时候必须加该标志符

To turn the LED off, pass 0 in the alpha channel for colorARGB or 0 for both ledOnMS and ledOffMS.

To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.

To flash the LED, pass the number of milliseconds that it should be on and off to ledOnMS and ledOffMS.

Since hardware varies, you are not guaranteed that any of the values you pass are honored exactly. Use the system defaults (TODO) if possible because they will be set to values that work on any given hardware.

The alpha channel must be set for forward compatibility.

Constant Value: 1 (0x00000001)

通知类别——setCategory

//设置通知类别

notify.setCategory(Notification.CATEGORY_MESSAGE);




对显示在安全锁定屏幕上的信息的控制

位于锁定屏幕上的通知,具有用户解锁设备后可显示的内容。

在设置安全锁定屏幕时,用户可以选择从安全锁定屏幕隐藏敏感的详细信息。 在这种情况下,系统 UI 会考虑通知的可见性级别,从而确定哪些内容可以安全地显示出来。

要控制可见性级别,可调用 Notification.Builder.setVisibility(),然后指定以下值之一:

VISIBILITY_PUBLIC。显示通知的完整内容。 在未指定可见性的情况下,此设置是系统的默认设置。

VISIBILITY_PRIVATE。在锁定屏幕上,会显示通知的基本信息,包括其图标以及发布此通知的应用名称。 剩下的通知详细信息不会显示。需要注意的一些有用建议如下:

如果您希望为通知提供不同的公用版本,供系统显示在安全锁定屏幕上,可在 Notification.publicVersion 字段中提供替换通知对象。该设置可让您的应用有机会创建有用内容的删减版本,但是不会显示个人信息。 可参考短信应用的示例,这种应用的通知包括短信的文本以及发信者的姓名和联系人图标。该通知应为 VISIBILITY_PRIVATE,但是 publicVersion 仍然可以包含“有 3 条新消息”这样的有用信息,而不会提供其他识别性详细信息。

Notification.VISIBILITY_SECRET。仅显示最为精简的信息,甚至不包括通知图标。

Notification视觉风格

Notification有两种视觉风格,一种是标准视图(Normal view)、一种是大视图(Big view)。标准视图在Android中各版本是通用的,但是对于大视图而言,仅支持Android4.1+的版本。

标准视图

一个标准视图显示的大小要保持在64dp高,宽度为屏幕标准。标准视图的通知主体内容有一下几个:



通知标题。

大图标。

通知内容。

通知消息。

小图标。

通知的时间,一般为系统时间,也可以使用setWhen()设置。

大视图

This happens when the notification is at the top of the drawer, or the user clicks the notification.只会发生在通知在抽屉顶端或者是用户点击了通知的时候。

大视图(Big View),它的细节区域只能显示256dp高度的内容,并且只对Android4.1+之后的设备才支持,它比标准视图不一样的地方,均需要使用setStyle()方法设定,它大致的效果如下:



  setStyle()传递一个NotificationCompat.Style对象,它是一个抽象类,Android为我们提供了三个实现类,用于显示不同的场景。分别是:

NotificationCompat.BigPictureStyle, 在细节部分显示一个256dp高度的位图。

NotificationCompat.BigTextStyle,在细节部分显示一个大的文本块。

NotificationCompat.InboxStyle,在细节部分显示一段行文本。

  如果仅仅显示一个图片,使用BigPictureStyle是最方便的;如果需要显示一个富文本信息,则可以使用BigTextStyle;如果仅仅用于显示一个文本的信息,那么使用InboxStyle即可。

示例代码
btnBigViewNotification.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

Bitmap btm = BitmapFactory.decodeResource(getResources(),
R.drawable.msg);
Intent intent = new Intent(MainActivity.this,
ResultActivity.class);

PendingIntent pendingIntent = PendingIntent.getActivity(
MainActivity.this, 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT);

Notification noti = new NotificationCompat.Builder(MainActivity.this)
.setSmallIcon(R.drawable.msg)
.setLargeIcon(btm)
.setNumber(13)
.setContentIntent(pendingIntent)
.setStyle(
new NotificationCompat.InboxStyle()
.addLine(
"M.Twain (Google+) Haiku is more than a cert...")
.addLine("M.Twain Reminder")
.addLine("M.Twain Lunch?")
.addLine("M.Twain Revised Specs")
.addLine("M.Twain ")
.addLine(
"Google Play Celebrate 25 billion apps with Goo..")
.addLine(
"Stack Exchange StackOverflow weekly Newsl...")
.setBigContentTitle("6 new message")
.setSummaryText("mtwain@android.com"))
.build();

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, noti);
}
});




代码示例

//先创建通知的builder以及其点击事件
Intent resultIntent = new Intent(this, ResultActivity.class);
resultIntent.putExtra(CommonConstants.EXTRA_MESSAGE, msg);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TASK);

// Because clicking the notification launches 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
);

// This sets the pending intent that should be fired when the user clicks the
// notification. Clicking the notification launches a new activity.
builder.setContentIntent(resultPendingIntent);

//创建大视图当中两个按钮的处理事件
// Sets up the Snooze and Dismiss action buttons that will appear in the
// big view of the notification.
Intent dismissIntent = new Intent(this, PingService.class);
dismissIntent.setAction(CommonConstants.ACTION_DISMISS);
PendingIntent piDismiss = PendingIntent.getService(this, 0, dismissIntent, 0);

Intent snoozeIntent = new Intent(this, PingService.class);
snoozeIntent.setAction(CommonConstants.ACTION_SNOOZE);
PendingIntent piSnooze = PendingIntent.getService(this, 0, snoozeIntent, 0);

//创建大视图
// Constructs the Builder object.
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_notification)
.setContentTitle(getString(R.string.notification))
.setContentText(getString(R.string.ping))
.setDefaults(Notification.DEFAULT_ALL) // requires VIBRATE permission
/*
* Sets the big view "big text" style and supplies the
* text (the user's reminder message) that will be displayed
* in the detail area of the expanded notification.
* These calls are ignored by the support library for
* pre-4.1 devices.
*/
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.addAction (R.drawable.ic_stat_dismiss,
getString(R.string.dismiss), piDismiss)
.addAction (R.drawable.ic_stat_snooze,
getString(R.string.snooze), piSnooze);


进度条样式的通知——此方法在4.0及以后版本才有用

  对于一个标准通知,有时候显示的消息并不一定是静态的,还可以设定一个进度条用于显示事务完成的进度。

  Notification.Builder类中提供一个setProgress(int max,int progress,boolean indeterminate)方法用于设置进度条,max用于设定进度的最大数,progress用于设定当前的进度,indeterminate用于是否是一个确定进度只的进度条。通过indeterminate的设置,可以实现两种不同样式的进度条,一种是有进度的(false),一种是循环流动的(true)。两种进度,操作结束时,调用setProgress(0, 0, false)并更新通知以移除指示条。

有进度的setProgress(max, progress, false)

  


  代码实现看上面更新的示例、

循环流动setProgress(0, 0, true)



修改下面的一点点代码就行

mBuilder.setProgress(0, 0, true);
// Issues the notification
mNotifyManager.notify(id, mBuilder.build());


自定义通知

 和Toast一样,通知也可以使用自定义的XML来自定义样式,但是对于通知而言,因为它的全局性,并不能简单的通过inflate膨胀出一个View,因为可能触发通知的时候,响应的App已经关闭,无法获取当指定的XML布局文件。所以需要使用单独的一个RemoteViews类来操作。

  RemoteViews,描述了一个视图层次的结构,可以显示在另一个进程。层次结构也是从布局文件中“膨胀”出一个视图,这个类,提供了一些基本的操作求改其膨胀的内容。

  RemoteViews提供了多个构造函数,一般使用RemoteViews(String packageName,int layoutId)。第一个参数为包的名称,第二个为layout资源的Id。当获取到RemoteViews对象之后,可以使用它的一系列setXxx()方法通过控件的Id设置控件的属性。最后使用NotificationCompat.Builder.setContent(RemoteViews)方法设置它到一个Notification中。

btnCustomNotification.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RemoteViews contentViews = new RemoteViews(getPackageName(),
R.layout.custom_notification);
//通过控件的Id设置属性
contentViews
.setImageViewResource(R.id.imageNo, R.drawable.btm1);
contentViews.setTextViewText(R.id.titleNo, "自定义通知标题");
contentViews.setTextViewText(R.id.textNo, "自定义通知内容");

Intent intent = new Intent(MainActivity.this,
ResultActivity.class);

PendingIntent pendingIntent = PendingIntent.getActivity(
MainActivity.this, 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
MainActivity.this).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My notification")
.setTicker("new message");
mBuilder.setAutoCancel(true);

mBuilder.setContentIntent(pendingIntent);
mBuilder.setContent(contentViews);
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(10, mBuilder.build());
}
});


设定提示响应

 对于有些通知,需要调用一些设备的资源,使用户能更快的发现有新通知,一般可设定的响应有:铃声、闪光灯、震动。对于这三个属性,NotificationCompat.Builder提供了三个方法设定:

setSound(Uri sound):设定一个铃声,用于在通知的时候响应。传递一个Uri的参数,格式为“file:///mnt/sdcard/Xxx.mp3”。

setLights(int argb, int onMs, int offMs):设定前置LED灯的闪烁速率,持续毫秒数,停顿毫秒数。

setVibrate(long[] pattern):设定震动的模式,以一个long数组保存毫秒级间隔的震动。大多数时候,我们并不需要设定一个特定的响应效果,只需要遵照用户设备上系统通知的效果即可,那么可以使用setDefaults(int)方法设定默认响应参数,在Notification中,对它的参数使用常量定义了,我们只需使用即可:

DEFAULT_ALL:铃声、闪光、震动均系统默认。
DEFAULT_SOUND:系统默认铃声。
DEFAULT_VIBRATE:系统默认震动。
DEFAULT_LIGHTS:系统默认闪光。


  而在Android中,如果需要访问硬件设备的话,是需要对其进行授权的,所以需要在清单文件AndroidManifest.xml中增加两个授权,分别授予访问振动器与闪光灯的权限:

<!-- 闪光灯权限 -->
<uses-permission android:name="android.permission.FLASHLIGHT"/>
<!-- 振动器权限 -->
<uses-permission android:name="android.permission.VIBRATE"/>


(1)方法:.setDefaults(int defaults) (NotificationCompat.Builder中的方法,用于提示)

功能:向通知添加声音、闪灯和振动效果的最简单、使用默认(defaults)属性,可以组合多个属性(和方法1中提示效果一样的)

对应属性:

Notification.DEFAULT_VIBRATE //添加默认震动提醒 需要 VIBRATE permission

Notification.DEFAULT_SOUND // 添加默认声音提醒

Notification.DEFAULT_LIGHTS// 添加默认三色灯提醒

Notification.DEFAULT_ALL// 添加默认以上3种全部提醒

(2)方法:setVibrate(long[] pattern)

功能:设置震动方式。

使用:

.setVibrate(new long[] {0,300,500,700});


实现效果:延迟0ms,然后振动300ms,在延迟500ms,接着在振动700ms。

以上方法的还有种写法是

mBuilder.build().vibrate = new long[] {0,300,500,700};


如果希望设置默认振动方式,设置了方法(1)中默认为DEFAULT_VIBRATE 即可。

(3)方法:.setLights(intledARGB ,intledOnMS ,intledOffMS )

功能:Android支持三色灯提醒,这个方法就是设置不同场景下的不同颜色的灯。

描述:其中ledARGB 表示灯光颜色、 ledOnMS 亮持续时间、ledOffMS 暗的时间。

注意:1)只有在设置了标志符Flags为Notification.FLAG_SHOW_LIGHTS的时候,才支持三色灯提醒。

2)这边的颜色跟设备有关,不是所有的颜色都可以,要看具体设备。

使用:

.setLights(0xff0000ff, 300, 0)


如果希望使用默认的三色灯提醒,设置了方法(1)中默认为DEFAULT_LIGHTS即可。

Notification notify = mBuilder.build();
notify.flags = Notification.FLAG_SHOW_LIGHTS;
notify.ledARGB = 0xff0000ff;
notify.ledOnMS = 300;
notify.ledOffMS = 300;


(4)方法:.setSound(Uri sound)

功能:设置默认或则自定义的铃声,来提醒。

//获取默认铃声
.setDefaults(Notification.DEFAULT_SOUND)
//获取自定义铃声
.setSound(Uri.parse("file:///sdcard/xx/xx.mp3"))
//获取Android多媒体库内的铃声
.setSound(Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "5"))


同理相同效果的另一种设置方法这边就不讲, 和上面的都是一样的。

通知的设计注意点

http://adchs.github.io/patterns/notifications.html,写的不错,和md设计规范差不多吧,比较老了,4.4。

md在线链接:http://wiki.jikexueyuan.com/project/material-design/

PendingIntent

它是一个系统级的全局的通知,并不确定这个意图被执行的时间。当在应用外部执行PendingIntent时,因为它保存了触发App的Context,使得外部App可以如果当前App一样执行PendingIntent里的Intent,就算执行时触发通知的App已经不存在了,也能通过存在PendingIntent里的Context照常执行Intent,并且还可以处理Intent所带来的额外的信息。

定义notification’s Action

虽然actions是可选的,但是我们应该至少添加一个action给我们的通知。一个action可以把用户从通知带到自身应用的某个activity(用户可以知道为啥通知会发生的活动页面,或者是接下来的工作界面。), Inside a notification, the action itself is defined by a PendingIntent containing an Intent that starts an Activity in your application.

//设置点击事件
PendingIntent resultPendingIntent;
...
mBuilder.setContentIntent(resultPendingIntent);


用户点击一个notification之后能总是有相应的响应动作,通常是打开一个activity。你还可以在notification中添加能响应点击事件的button,比如延迟一下闹钟,或者立即回复一条短消息。

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

pendingIntent的获取

PendingIntent提供了多个静态的getXxx()方法,用于获得适用于不同场景的PendingIntent对象。



PendingIntent的Flag

flag参数,用于标识PendingIntent的构造选择:

•FLAG_CANCEL_CURRENT:如果构建的PendingIntent已经存在,则取消前一个,重新构建一个。

•FLAG_NO_CREATE:如果前一个PendingIntent已经不存在了,将不再构建它。

•FLAG_ONE_SHOT:表明这里构建的PendingIntent只能使用一次。

•FLAG_UPDATE_CURRENT:如果构建的PendingIntent已经存在,则替换它,常用。

Android开发陷阱:利用PendingIntent传递唯一的Intent

http://zhiweiofli.iteye.com/blog/1972513

参考链接

http://blog.csdn.net/x_i_a_o_h_a_i/article/details/32966767

官方链接:https://developer.android.com/training/notify-user/index.html

https://developer.android.com/reference/android/app/Notification.html

翻译:https://developer.android.com/design/patterns/notifications.html

http://adchs.github.io/patterns/notifications.html

http://bbs.9ria.com/thread-241146-1-1.html

demo链接

http://download.csdn.net/detail/x_i_a_o_h_a_i/7532505别人写的,很详细了,我自己写了一个,自定义报错没法查错,所以,还是用这个做备份吧
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: