天天看點

水煮安卓 - Notification簡例

一、啟動Notification的簡單流程:

1.初始化NotificationManager

2.建立NotivicationChannel

String mChannelId="channel_id";
String mChannelName="channel_name";
NotificationChannel notificationChannel = new NotificationChannel(mChannelId, mChannelName, NotificationManager.IMPORTANCE_DEFAULT);
mNotificationManager.createNotificationChannel(notificationChannel);
           

3.建立Notivication執行個體

Notificationnotification = newNotificationCompat.Builder(this, mChannelId)
		.setSmallIcon(R.mipmap.ic_launcher)
		.setContentTitle("contenttitle")
		.setContentText("contexttext")
		.build();
           

4.調用NotificationManager.notify啟動Notification

注意事項

1)NotificationChannel

  • 管道控制通知的行為:靜音,響鈴,振動,閃光等。
  • 5個重要性對應不同的預設行為:
Importance 狀态欄 通知欄 最小化資訊 靜音 浮窗
NotificationManager.IMPORTANCE_NONE × × × ×
NotificationManager.IMPORTANCE_MIN × ×
NotificationManager.IMPORTANCE_LOW 完全顯示 ×
NotificationManager.IMPORTANCE_DEFAULT × 完全顯示 響鈴 ×
NotificationManager.IMPORTANCE_HIGH 完全顯示 響鈴

2)notify(intid,Notificationnotification)

如果具有相同id的通知,第一個通知如果還沒被清除,第二個通知會取代第一個通知。

否則就啟動新的通知。

Github 位址

未完待續…