Notification是一种在你APP常规UI外用来指示某个事件发生的用户交互元素。用户可以在使用其它apps时查看notification,并在方便的时候做出回应。
创建Notification:
第一步创建Notification Builder
创建Notification时,可以用NotificationCompat.Builder对象指定Notification的UI内容与行为。一个Builder至少包含以下内容:一个小的icon,用setSmallIcon())方法设置;一个标题,用setContentTitle())方法设置。详细的文本,用setContentText())方法设置。
比如:
<span style="font-size:18px;">NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!");
</span>
第二步:定义Notification的Action(行为)
尽管在Notification中Actions是可选的,但是你应该至少添加一种Action。一种Action可以让用户从Notification直接进入你应用内的Activity,在这个activity中他们可以查看引起Notification的事件或者做下一步的处理。在Notification中,action本身是由PendingIntent定义的,PendingIntent包含了一个启动你应用内Activity的Intent。
如何构建一个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实例ss使用notify())方法发布Notification。当你调用notify())方法时,指定一个notification ID。你可以在以后使用这个ID来更新你的notification。这在Managing Notifications中有更详细的描述。调用build())方法,会返回一个包含你的特征的Notification对象。举个例子:
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.getNotification()););
下面代码为一个创建、发布通知具体信息:
<span style="font-size:18px;">NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
//设置UI界面特征
builder.setSmallIcon(R.drawable.m4); //通知图标
builder.setContentTitle("通知标题");
builder.setContentText("1条新消息"); //通知内容
builder.setTicker("收到1条新消息"); //刚收到通知时提示
builder.setNumber(1); //消息个数
builder.setAutoCancel(true); // 点击取消通知
builder.setDefaults(Notification.DEFAULT_ALL);// requires VIBRATE permission
builder.setWhen(System.currentTimeMillis());
builder.setSound(uri); //通知声音
//设置行为特征
Intent intent = new Intent(this, BaseActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent); //通知点击行为
NotificationManager mNotifyManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = mBuilder.build();
int notificationId = 10; //能知ID,唯一标识当前通知
//发布通知
mNotifyManager.notify(notificationId, notification);
</span>
注: pendingIntent字面意义:等待的,未决定的Intent。要得到一个pendingIntent对象,使用方法类的静态方法 getActivity(Context, int, Intent, int),getBroadcast(Context, int, Intent, int),getService(Context, int, Intent, int) 分别对应着Intent的3个行为,跳转到一个activity组件、打开一个广播组件和打开一个服务组件。可以看到,要得到这个对象,必须传入一个Intent作为参数,必须有context作为参数。
更新notifications
学习如何更新与移除notifications想要设置一个可以被更新的Notification,需要在发布它的时候调用NotificationManager.notify(ID, notification))方法为它指定一个notification ID。更新一个已经发布的Notification,需要更新或者创建一个NotificationCompat.Builder对象,并从这个对象创建一个Notification对象,然后用与先前一样的ID去发布这个Notification。
移除Notification
你在创建notification时调用了 setAutoCancel(true)点击通知时移除执行cancel(notificationId)或cancelAll()。
当Activity启动时保留导航:
学习如何为一个从notification启动的Activity执行适当的导航。通知启动的是你application工作流中的一部分Activity。其步骤如下:
1 、在manifest中定义你application的Activity层次,最终的manifest文件应该像这个:
<span style="font-size:18px;"><activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ResultActivity"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity"/>
</activity>
</span>
2 、在基于启动Activity的Intent中创建一个返回栈,比如:
<span style="font-size:18px;">int id = 1;
...
Intent resultIntent = new Intent(this, ResultActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack
stackBuilder.addParentStack(ResultActivity.class);
// Adds the Intent to the top of the stack
stackBuilder.addNextIntent(resultIntent);
// Gets a PendingIntent containing the entire back stack
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
...
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(id, builder.build());
</span>
在notification中展示进度:
学习在notification中显示某个操作的进度,既可以用于那些你可以估算已经完成多少(确定进度,determinate)的操作,也可以用于那些你无法知道完成了多少(不确定进度,indefinite )的操作展示固定长度的进度指示器调用NotificationCompat.Builder 的setProgress(max, progress, false))方法将进度条添加进notification,调用setProgress(0, 0, false))方法移除进度条,显示进度需不但改变progress值,同时更新通知。
比如:
<span style="font-size:18px;">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();
</span>
创建一个自定义Notification,其代码如下:
<span style="font-size:18px;">NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setSmallIcon(R.drawable.m8);
mBuilder.setTicker("自定义通知,你有新消息");
mBuilder.setAutoCancel(true);
// ------自定义notification界面
RemoteViews view = new RemoteViews(getPackageName(),R.layout.view_notificaction_layout);
mBuilder.setContent(view);
// ----自定义notification事件处理
Intent intent = new Intent(this, RadioTabActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 01,intent, PendingIntent.FLAG_UPDATE_CURRENT);
view.setOnClickPendingIntent(R.id.player_notification, pendingIntent);
Notification notification = mBuilder.build();
int notificationId = 29;
NotificationManager mNotifyManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyManager.notify(notificationId, notification);
</span>
以上内容多数为网上找的资料,有雷同的很正常。