建立一个Notification

编写:fastcome1985 - 原文:http://developer.android.com/training/notify-user/build-notification.html

创建Notification Buider

例如:


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

定义Notification的Action(行为)

  • 尽管在Notification中Actions是可选的,但是你应该至少添加一种Action。一种Action可以让用户从Notification直接进入你应用内的Activity,在这个activity中他们可以查看引起Notification的事件或者做下一步的处理。在Notification中,action本身是由PendingIntent定义的,PendingIntent包含了一个启动你应用内ActivityIntent

  • 如何构建一个PendingIntent取决于你要启动的activity的类型。当从Notification中启动一个activity时,你必须保存用户的导航体验。在下面的代码片段中,点击Notification启动一个新的activity,这个activity有效地扩展了Notification的行为。在这种情形下,就没必要人为地去创建一个返回栈(更多关于这方面的信息,请查看 Preserving Navigation when Starting an Activity


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
);

设置Notification的点击行为

可以通过调用NotificationCompat.Builder中合适的方法,将上一步创建的PendingIntent与一个手势产生关联。比方说,当点击Notification抽屉里的Notification文本时,启动一个activity,可以通过调用setContentIntent())方法把PendingIntent添加进去。

例如:


PendingIntent resultPendingIntent;
...
mBuilder.setContentIntent(resultPendingIntent);

发布Notification

为了发布notification:

* 获取一个[NotificationManager](http://www.baidu.com/baidu?wd=NotificationManager.&tn=monline_4_dg)实例
* 使用[notify()](developer.android.com/reference/java/lang/Object.html#notify())方法发布Notification。当你调用[notify()](developer.android.com/reference/java/lang/Object.html#notify())方法时,指定一个notification ID。你可以在以后使用这个ID来更新你的notification。这在[Managing Notifications](developer.android.com/intl/zh-cn/training/notify-user/managing.html)中有更详细的描述。
* 调用[build()](developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#build())方法,会返回一个包含你的特征的[Notification](developer.android.com/reference/android/app/Notification.html)对象。

举个例子:


NotificationCompat.Builder mBuilder;
...
// Sets an ID for the notification
int mNotificationId = 001;
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr =
        (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify(mNotificationId, mBuilder.build());