NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Intent intent = new Intent(this,Main2Activity.class);
PendingIntent pendingIntent= PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setContentTitle("測試标題")//設定通知欄标題
.setContentText("測試内容") //設定通知欄顯示内容
.setContentIntent(pendingIntent) //設定通知欄點選意圖
// .setNumber(number) //設定通知集合的數量
.setTicker("測試通知來啦") //通知首次出現在通知欄,帶上升動畫效果的
.setWhen(System.currentTimeMillis())//通知産生的時間,會在通知資訊裡顯示,一般是系統擷取到的時間
.setPriority(Notification.PRIORITY_DEFAULT) //設定該通知優先級
.setAutoCancel(true)//設定這個标志當使用者單擊面闆就可以讓通知将自動取消
.setOngoing(false)//ture,設定他為一個正在進行的通知。他們通常是用來表示一個背景任務,使用者積極參與(如播放音樂)或以某種方式正在等待,是以占用裝置(如一個檔案下載下傳,同步操作,主動網絡連接配接)
.setDefaults(Notification.DEFAULT_VIBRATE)//向通知添加聲音、閃燈和振動效果的最簡單、最一緻的方式是使用目前的使用者預設設定,使用defaults屬性,可以組合
//Notification.DEFAULT_ALL Notification.DEFAULT_SOUND 添加聲音 // requires VIBRATE permission
.setProgress(100,50,false)
.setSmallIcon(R.drawable.ic_launcher);//設定通知小ICON
mNotificationManager.notify(0, mBuilder.build());
//在service中使用此方法可使service運作在前台,減低被系統殺死的幾率
//這裡的id不能是0,否則不顯示出來
startForeground(1, mBuilder.build());
PendingIntent的位辨別符:
FLAG_ONE_SHOT 表示傳回的PendingIntent僅能執行一次,執行完後自動取消
FLAG_NO_CREATE 表示如果描述的PendingIntent不存在,并不建立相應的PendingIntent,而是傳回NULL
FLAG_CANCEL_CURRENT 表示相應的PendingIntent已經存在,則取消前者,然後建立新的PendingIntent,這個有利于資料保持為最新的,可以用于即時通信的通信場景
FLAG_UPDATE_CURRENT 表示更新的PendingIntent
若想更新進度條
mBuilder.setProgress(100,80,false);
mNotificationManager.notify(0, mBuilder.build());
android 26後,以上方法無法正常彈出notification,需使用以下方法
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Intent intent = new Intent(this,MainActivity.class);
PendingIntent pendingIntent= PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
@SuppressLint("WrongConstant") NotificationChannel channel = new NotificationChannel("1",
"Channel1", NotificationManager.IMPORTANCE_DEFAULT);
channel.enableLights(true); //是否在桌面icon右上角展示小紅點
channel.setLightColor(Color.GREEN); //小紅點顔色
channel.setShowBadge(true); //是否在久按桌面圖示時顯示此管道的通知
mNotificationManager.createNotificationChannel(channel);
Notification.Builder builder = new Notification.Builder(this,"1"); //與channelId對應
//icon title text必須包含,不然影響桌面圖示小紅點的展示
builder.setSmallIcon(android.R.drawable.stat_notify_chat)
.setNumber(3) //久按桌面圖示時允許的此條通知的數量
.setContentTitle("測試标題")//設定通知欄标題
.setContentText("測試内容") //設定通知欄顯示内容
.setContentIntent(pendingIntent) //設定通知欄點選意圖
// .setNumber(number) //設定通知集合的數量
.setTicker("測試通知來啦") //通知首次出現在通知欄,帶上升動畫效果的
.setWhen(System.currentTimeMillis())//通知産生的時間,會在通知資訊裡顯示,一般是系統擷取到的時間
.setPriority(Notification.PRIORITY_DEFAULT) //設定該通知優先級
.setAutoCancel(true)//設定這個标志當使用者單擊面闆就可以讓通知将自動取消
.setOngoing(true)//ture,設定他為一個正在進行的通知。他們通常是用來表示一個背景任務,使用者積極參與(如播放音樂)或以某種方式正在等待,是以占用裝置(如一個檔案下載下傳,同步操作,主動網絡連接配接)
// .setProgress(100,50,false)
.setDefaults(Notification.DEFAULT_VIBRATE);//向通知添加聲音、閃燈和振動效果的最簡單、最一緻的方式是使用目前的使用者預設設定,使用defaults屬性,可以組合
//Notification.DEFAULT_ALL Notification.DEFAULT_SOUND 添加聲音 // requires VIBRATE permission
mNotificationManager.notify(0, builder.build());
}