建立及管理通知管道

從 Android 8.0 (API 等級 26) 開始,所有通知都必須指派專屬管道。針對每個管道,您可以設定套用至該管道所有通知的視覺和聽覺行為。使用者可以變更這些設定,並決定應用程式的哪些通知管道容易侵入或顯示。

請觀看以下影片,概略瞭解 Android 8.0 中的管道和其他通知功能。

系統設定中會為每個應用程式提供通知管道的使用者設定,如圖 1 所示。

圖 1 「時鐘」應用程式及其管道的通知設定。

建立通知管道後,您就無法變更通知行為。屆時使用者有完全控制權。但仍可變更頻道名稱和說明

請為需要傳送的每種通知類型建立頻道。您也可以建立通知管道來反映使用者所做的選擇。舉例來說,您可以針對使用者在訊息應用程式中建立的每個對話群組,設定不同的通知管道。

如果指定 Android 8.0 (API 級別 26) 以上版本,則必須實作一或多個通知管道。如果 targetSdkVersion 設為 25 以下,當應用程式在 Android 8.0 (API 級別 26) 以上版本上執行時,則其行為會與搭載 Android 7.1 (API 級別 25) 以下版本的裝置相同。

建立通知管道

如要建立通知管道,請按照下列步驟操作:

  1. 建構 NotificationChannel 物件,其中包含專屬管道 ID、使用者可看到的名稱和重要性等級。

  2. 或者,您也可以使用 setDescription() 指定使用者在系統設定中看見的說明。

  3. 如要註冊通知管道,請將其傳送至 createNotificationChannel()

以下範例說明如何建立及註冊通知管道:

Kotlin

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    // Create the NotificationChannel.
    val name = getString(R.string.channel_name)
    val descriptionText = getString(R.string.channel_description)
    val importance = NotificationManager.IMPORTANCE_DEFAULT
    val mChannel = NotificationChannel(CHANNEL_ID, name, importance)
    mChannel.description = descriptionText
    // Register the channel with the system. You can't change the importance
    // or other notification behaviors after this.
    val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
    notificationManager.createNotificationChannel(mChannel)
}

Java

private void createNotificationChannel() {
    // Create the NotificationChannel, but only on API 26+ because
    // the NotificationChannel class is not in the Support Library.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = getString(R.string.channel_name);
        String description = getString(R.string.channel_description);
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
        channel.setDescription(description);
        // Register the channel with the system. You can't change the importance
        // or other notification behaviors after this.
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
}

以原始值重新建立現有的通知管道不會執行任何作業,因此您可以在啟動應用程式時呼叫此程式碼。

根據預設,發布至特定管道的所有通知都會採用 NotificationManagerCompat 類別 (例如 IMPORTANCE_DEFAULTIMPORTANCE_HIGH) 的重要性等級定義的視覺和聽覺行為。如要進一步瞭解重要性等級,請參閱下一節。

如要進一步自訂頻道的預設通知行為,您可以呼叫 NotificationChannel 上的 enableLights()setLightColor()setVibrationPattern() 等方法。請注意,管道建立後即無法變更這些設定,使用者仍可控管這些行為是否有效。

也可以呼叫 createNotificationChannels(),在單一作業中建立多個通知管道。

設定重要性等級

管道重要性會影響頻道中所有通知的干擾程度。請在 NotificationChannel 建構函式中,從 IMPORTANCE_NONE(0)IMPORTANCE_HIGH(4) 這五個重要性等級中擇一指定。

如要支援搭載 Android 7.1 (API 級別 25) 以下版本的裝置,您還必須使用 NotificationCompat 類別中的優先順序常數,針對每個通知呼叫 setPriority()

重要性 (NotificationManager.IMPORTANCE_*) 和優先順序 (NotificationCompat.PRIORITY_*) 常數會對應至使用者可看見的重要性選項,如下表所示。

使用者可見的重要性等級 重要性 (Android 8.0 以上版本) 優先順序 (Android 7.1 以下版本)
緊急
會發出音效,並顯示為抬頭通知。
IMPORTANCE_HIGH PRIORITY_HIGHPRIORITY_MAX

會發出音效。
IMPORTANCE_DEFAULT PRIORITY_DEFAULT

不會發出聲音。
IMPORTANCE_LOW PRIORITY_LOW

不會發出音效,也不會顯示在狀態列中。
IMPORTANCE_MIN PRIORITY_MIN

不會發出音效,也不會顯示在狀態列或欄中。
IMPORTANCE_NONE N/A

無論重要性為何,所有通知都會顯示在不會造成乾擾的系統 UI 位置 (例如通知導覽匣),以及啟動器圖示上的標記,不過您可以修改通知標記的外觀

將管道提交至 NotificationManager 後,就無法再變更重要性等級。不過,使用者隨時可以變更應用程式的偏好設定。

如要瞭解如何選擇適當的優先順序等級,請參閱通知設計指南中的「優先等級」。

讀取通知管道設定

使用者可以修改通知管道的設定,包括震動和快訊音效等行為。如要瞭解使用者套用至通知管道的設定,請按照下列步驟操作:

  1. 呼叫 getNotificationChannel()getNotificationChannels() 取得 NotificationChannel 物件。

  2. 查詢特定管道設定,例如 getVibrationPattern()getSound()getImportance()

如果您偵測到對應用程式限制的管道設定,可以建議使用者進行變更,並提供開啟管道設定的動作,如下一節所示。

開啟通知管道設定

建立通知管道後,您無法以程式輔助方式變更通知管道的視覺和聽覺行為。只有使用者可以透過系統設定變更管道行為。如要讓使用者輕鬆存取這些通知設定,請在應用程式的設定 UI 中新增一個項目,用於開啟這些系統設定。

您可以使用使用 ACTION_CHANNEL_NOTIFICATION_SETTINGS 動作的 Intent 開啟通知管道的系統設定。

例如,以下程式碼範例說明如何將使用者重新導向至通知管道的設定:

Kotlin

val intent = Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS).apply {
    putExtra(Settings.EXTRA_APP_PACKAGE, packageName)
    putExtra(Settings.EXTRA_CHANNEL_ID, myNotificationChannel.getId())
}
startActivity(intent)

Java

Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
intent.putExtra(Settings.EXTRA_CHANNEL_ID, myNotificationChannel.getId());
startActivity(intent);

請注意,意圖需要兩個額外項目來指定應用程式套件名稱 (也稱為應用程式 ID) 和要編輯的管道。

刪除通知管道

您可以呼叫 deleteNotificationChannel() 來刪除通知管道。以下程式碼範例示範如何完成這項程序:

Kotlin

// The id of the channel.
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val id: String = "my_channel_01"
notificationManager.deleteNotificationChannel(id)

Java

NotificationManager notificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// The id of the channel.
String id = "my_channel_01";
notificationManager.deleteNotificationChannel(id);

建立通知管道群組

如要進一步整理設定 UI 中的管道外觀,您可以建立管道群組。如果應用程式支援多個使用者帳戶 (例如工作資料夾),這是個不錯的做法,因為這樣您就能為每個帳戶建立通知管道群組。如此一來,使用者就能輕鬆識別及控制名稱相同的多個通知管道。

圖 2. 提供個人和公司帳戶群組的通知管道設定。

舉例來說,社群網路應用程式可能包含對個人帳戶和公司帳戶的支援。在這種情況下,每個帳戶可能需要多個具備相同函式和名稱的通知管道,例如:

  • 個人帳戶有兩個頻道:

    • 新留言

    • 貼文推薦

  • 擁有兩個頻道的企業帳戶:

    • 新留言

    • 貼文推薦

將每個帳戶的通知管道分成不同群組,可讓使用者區分帳戶。

每個通知管道群組都需要一個 ID,在套件中不得重複,以及使用者可見的名稱。下列程式碼片段示範如何建立通知管道群組。

Kotlin

// The id of the group.
val groupId = "my_group_01"
// The user-visible name of the group.
val groupName = getString(R.string.group_name)
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannelGroup(NotificationChannelGroup(groupId, groupName))

Java

// The id of the group.
String groupId = "my_group_01";
// The user-visible name of the group.
CharSequence groupName = getString(R.string.group_name);
NotificationManager notificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannelGroup(new NotificationChannelGroup(groupId, groupName));

建立新群組後,您可以呼叫 setGroup(),將新的 NotificationChannel 物件與群組建立關聯。

將管道提交至通知管理員後,就無法變更通知管道與群組之間的關聯。