最基本的通知
NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext);
builder.setContentTitle("notification")
.setContentText("this is content")
.setSmallIcon(R.mipmap.ic_launcher);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(NOTIFICATION_ID,buidler.build());
說明
- 通常會選擇用
NotificationCompat來構(gòu)建一個通知,以保證兼容性。 - 一個通知必須為其設(shè)置上面的三個屬性。但是我們自己通過ReomteViews來自定義通知布局時,可以不用設(shè)置前兩個屬性,但是必須
setSmallIcon。 -
NotificationManager有兩個nofify()方法.notify(int id, Notification notification),notify(String tag, int id, Notification notification),其中tag和id用來唯一標(biāo)識一個通知。
一般混合通知
下面這個通知是一個長文本通知。包括兩個按鈕,并且可以響應(yīng)用戶點擊事件。內(nèi)容比較雜。
NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://github.com/"));
PendingIntent pendingIntent = PendingIntent.getActivity(mContext,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
Intent sendMessage = new Intent(Intent.ACTION_VIEW,Uri.parse("sms:10086"));
PendingIntent smpd = PendingIntent.getActivity(mContext,0,sendMessage,PendingIntent.FLAG_UPDATE_CURRENT);
// 為通知設(shè)置長文本樣式
android.support.v4.app.NotificationCompat.BigTextStyle style = new android.support.v4.app.NotificationCompat.BigTextStyle();
style.bigText(getResources().getString(R.string.note_text));
style.setSummaryText("English Text");
builder.setContentTitle("notification")
.setContentText("this is content")
.setSmallIcon(R.drawable.github)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.github))
.setTicker("hello")
.setDefaults(Notification.DEFAULT_ALL)
.setSound(Uri.parse("android:resource://" + context.getPackageName() + "/" + R.raw.msg))
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setPriority(Notification.PRIORITY_MAX)
.setStyle(style)
.addAction(R.drawable.accept,"send",smpd)
.addAction(R.drawable.decline,"cancel",null);
Notification notification = builder.build();
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(NOTIFICATION_ID,buidler.build());
說明
- 通過
PendingIntent來包裝Intent,然后調(diào)用builder的setContentIntent方法把PendingIntent傳進去,響應(yīng)用戶的點擊事件。 - 通過
setStyle來設(shè)置通知的樣式。例如可以設(shè)置BigTextStyle,還可以設(shè)值MediaStyle。 - 通過
addAction方法來給通知添加按鈕,并設(shè)值響應(yīng)事件。
帶進度條的通知
final NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext);
final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
builder.setContentTitle("Downloading")
.setSmallIcon(R.mipmap.ic_launcher);
new Thread(new Runnable() {
@Override
public void run() {
for (int i =0;i<100;i+=10){
// 第三個參數(shù)如果設(shè)置為true,則不會顯示精確的進度。
builder.setProgress(100,i,false);
manager.notify(NOTIFICATION_ID,builder.build());
try {
// 模擬下載
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
builder.setContentText("Complete")
// 取消進度條
.setProgress(0,0,false);
manager.notify(NOTIFICATION_ID,builder.build());
}
}).start();
自定義布局通知
自定義布局同時主要是通過ReomteViews來實現(xiàn).
NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext);
// 為RemotViews設(shè)置一個布局,這個布局需要自己在layout中實現(xiàn)。
RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.notification);
remoteViews.setTextViewText(R.id.text,"notification");
remoteViews.setImageViewResource(R.id.image,R.drawable.git);
builder.setSmallIcon(R.drawable.github);
// 將RemoteViews設(shè)置到通知中
builder.setContent(remoteViews);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(NOTIFICATION_ID,builder.build());
補充
-
RemoteViews是一個可以運行在其他進程中的View。通知是運行在系統(tǒng)進程的。 - 如果需要為
RemoteViews中的子View設(shè)置響應(yīng)事件,可以通過remoteViews.setOnClickPendingIntent方法實現(xiàn)。 - 上面通知的RemoteViews中有個TextView,如果要為它設(shè)置樣式,可以在xml文件里添加一句
android:textAppearance="@style/TextAppearance.StatusBar.EventContent.Title",這樣它的樣式就和系統(tǒng)一致了。
懸掛式通知
通知有五種優(yōu)先級,范圍從 PRIORITY_MIN (-2) 到 PRIORITY_MAX (2),如果未設(shè)置,則優(yōu)先級默認為 PRIORITY_DEFAULT (0)。
通過NotificationCompat.Builder.setPriority()來為通知設(shè)置優(yōu)先級。當(dāng)優(yōu)先級大于默認的時候時,通知都可以在其他應(yīng)用的上面,顯示一個頂部懸掛的通知。
通知的等級
通知的等級有三個
- VISIBILITY_PRIVATE, 表明當(dāng)前通知只有在沒有鎖屏的時候才會顯示。
- VISIBILITY_PUBLIC, 任何情況下都可以顯示。
- VISIBILITY_SECRET, 在沒有鎖屏的情況下才會顯示。
通過NotificationCompat.Builder.setVisibility(VISIBILITY_PUBLIC),可以讓通知在鎖屏界面上顯示。
通過NotificationCompat.Builder.setCategory(Notification.CATEGORY_MESSAGE),可以控制通知的位置。