//普通的消息通知
NotificationManager manager = (NotificationManager) this.getSystemService(this.NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(this);//新建Notification.Builder對(duì)象
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, TestActivity.class), 0);
builder.setContentTitle("service test");
builder.setContentText("message");
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentIntent(pendingIntent);
Notification notification = builder.getNotification();//將builder對(duì)象轉(zhuǎn)換為普通的notification
notification.flags |= Notification.FLAG_AUTO_CANCEL;//點(diǎn)擊通知后通知消失
manager.notify(1, notification);//運(yùn)行notification
startForeground(1, notification);
//自定義布局帶大圖的消息通知
NotificationManager systemService = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification);
remoteViews.setImageViewResource(R.id.iv_imageview,R.mipmap.programmer);
remoteViews.setTextViewText(R.id.tv_notification,"我是程序員杏瞻。。。。。。");
remoteViews.setImageViewResource(R.id.iv_logo,R.mipmap.ic_launcher);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setOngoing(false);
builder.setAutoCancel(false);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle("主題名稱");
builder.setContentText("測(cè)試測(cè)試測(cè)試");
Notification notification = builder.build();
notification.defaults = Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS;
notification.flags = Notification.FLAG_AUTO_CANCEL;
notification.when = System.currentTimeMillis();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
notification.bigContentView = remoteViews;
}
Intent i = new Intent(this,TestActivity.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, i, PendingIntent.FLAG_UPDATE_CURRENT);
notification.contentIntent = pendingIntent;
systemService.notify(0, notification);
startForeground(1, notification);
基本屬性
public Builder setTicker(CharSequence tickerText)
設(shè)置狀態(tài)欄開(kāi)始動(dòng)畫的文字public Builder setContentTitle(CharSequence title)
設(shè)置內(nèi)容區(qū)的標(biāo)題,必須設(shè)置public Builder setContentText(CharSequence text)
設(shè)置內(nèi)容區(qū)的內(nèi)容春哨,必須設(shè)置public Builder setContentIntent(PendingIntent intent)
設(shè)置點(diǎn)擊通知后操作(可以跳轉(zhuǎn)Activity,打開(kāi)Service恩伺,或者發(fā)送廣播)public Builder setColor(@ColorInt int argb)
這個(gè)可以設(shè)置smallIcon的背景色public Builder setSmallIcon(@DrawableRes int icon)
設(shè)置小圖標(biāo)赴背,必須設(shè)置public Builder setLargeIcon(Bitmap b)
設(shè)置打開(kāi)通知欄后的大圖標(biāo)public Builder setWhen(long when)
設(shè)置顯示通知的時(shí)間,不設(shè)置默認(rèn)獲取系統(tǒng)時(shí)間晶渠,這個(gè)值會(huì)在Notification上面顯示出來(lái)public Builder setAutoCancel(boolean autoCancel)
設(shè)置為true凰荚,點(diǎn)擊該條通知會(huì)自動(dòng)刪除,false時(shí)只能通過(guò)滑動(dòng)來(lái)刪除public Builder setPriority(int pri)
設(shè)置優(yōu)先級(jí)褒脯,級(jí)別高的排在前面public Builder setDefaults(int defaults)
設(shè)置上述鈴聲便瑟,振動(dòng),閃爍用|分隔番川,常量在Notification里public Builder setOngoing(boolean ongoing)
設(shè)置是否為一個(gè)正在進(jìn)行中的通知到涂,這一類型的通知將無(wú)法刪除
通知的提醒方式
- 聲音提醒
默認(rèn)聲音
notification.defaults |= Notification.DEFAULT_SOUND;
自定義聲音
notification.sound = Uri.parse("file:///sdcard0/notification.ogg"); - 震動(dòng)提醒
默認(rèn)振動(dòng)
notification.defaults |= Notification.DEFAULT_VIBRATE;
自定義振動(dòng)
long[] vibrate = {100, 200, 300, 400}; //震動(dòng)效果,表示在100、200颁督、300践啄、400這些時(shí)間點(diǎn)交替啟動(dòng)和關(guān)閉震動(dòng)
notification.vibrate = vibrate; - 閃爍提醒
默認(rèn)閃爍
notification.defaults |= Notification.DEFAULT_LIGHTS;
自定義閃爍
notification.ledARGB = 0xff00ff00; // LED燈的顏色,綠燈
notification.ledOnMS = 300; // LED燈顯示的毫秒數(shù)沉御,300毫秒
notification.ledOffMS = 1000; // LED燈關(guān)閉的毫秒數(shù)屿讽,1000毫秒
notification.flags |= Notification.FLAG_SHOW_LIGHTS; // 必須加上這個(gè)標(biāo)志