NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);ui
notification.icon = R.drawable.ic_launcher;
notification.tickerText = "注意了,我被扔到狀態欄了";
notification.flags = Notification.FLAG_AUTO_CANCEL;// 點擊通知以後自動消失
notification.defaults=notification.DEFAULT_SOUND|notification.DEFAULT_VIBRATE; //notification被notify的時候,觸發默認聲音和默認震動
二、爲notification添加一個意圖(Intent):
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT);
notification.contentIntent = contentIntent;
因爲Notification的包裝內容爲Intent,咱們就能夠方便地爲通知被點擊的觸發的事件傳值:
openintent.putExtra("isRead", true);
openintent.putExtra("name", "CaiYu");
四、發送通知
manager.notify(notifacation_id, notification);//notifacation_id由本身指定,爲每個Notification對應的惟一標誌
五、自定義通知欄
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notify_view);//自定義的layout
contentView.setImageViewResource(R.id.notify_imageLog, R.drawable.ic_launcher);//layout中的控件
contentView.setTextViewText(R.id.notify_name, "1");
notification.contentView = contentView;
六、刪除、更改某些通知
manager.cancel(notifacation_id);
manager.cancelAll();
manager.notify(notifaction_id,newNotification);
七、用notification.builder實現
NotificationCompat.Builder builder = new Builder(context);
builder.setContent(remoteViews).setSmallIcon(R.drawable.icon).setLargeIcon(icon).setOngoing(true).setTicker("music is playing");
manager.notify(notifaction_id, builder.build());