基本通知通常包含標題、一行文字,以及使用者可依回應執行的動作。如要提供更多資訊,您可以套用本文件中的任一通知範本,建立可展開的大型通知。
首先,請按照建立通知中所述的所有基本內容來建立通知。接著,使用樣式物件呼叫 setStyle()
,並提供與各範本相對應的資訊,如以下範例所示。
新增大型圖片
如要在通知中新增圖片,請將 NotificationCompat.BigPictureStyle
的例項傳遞至 setStyle()
。
Kotlin
val notification = NotificationCompat.Builder(context, CHANNEL_ID) .setSmallIcon(R.drawable.new_post) .setContentTitle(imageTitle) .setContentText(imageDescription) .setStyle(NotificationCompat.BigPictureStyle() .bigPicture(myBitmap)) .build()
Java
Notification notification = new NotificationCompat.Builder(context, CHANNEL_ID) .setSmallIcon(R.drawable.new_post) .setContentTitle(imageTitle) .setContentText(imageDescription) .setStyle(new NotificationCompat.BigPictureStyle() .bigPicture(myBitmap)) .build();
如果只想在收合通知時顯示圖片,請呼叫 setLargeIcon()
並傳遞圖片 (如下圖所示)。接著,呼叫 BigPictureStyle.bigLargeIcon()
並傳遞 null
,這樣大型圖示就會在展開通知時消失:
Kotlin
val notification = NotificationCompat.Builder(context, CHANNEL_ID) .setSmallIcon(R.drawable.new_post) .setContentTitle(imageTitle) .setContentText(imageDescription) .setLargeIcon(myBitmap) .setStyle(NotificationCompat.BigPictureStyle() .bigPicture(myBitmap) .bigLargeIcon(null)) .build()
Java
Notification notification = new NotificationCompat.Builder(context, CHANNEL_ID) .setSmallIcon(R.drawable.new_post) .setContentTitle(imageTitle) .setContentText(imageDescription) .setLargeIcon(myBitmap) .setStyle(new NotificationCompat.BigPictureStyle() .bigPicture(myBitmap) .bigLargeIcon(null)) .build();
新增大塊文字
套用 NotificationCompat.BigTextStyle
,在通知的展開內容區域顯示文字:
Kotlin
val notification = NotificationCompat.Builder(context, CHANNEL_ID) .setSmallIcon(R.drawable.new_mail) .setContentTitle(emailObject.getSenderName()) .setContentText(emailObject.getSubject()) .setLargeIcon(emailObject.getSenderAvatar()) .setStyle(NotificationCompat.BigTextStyle() .bigText(emailObject.getSubjectAndSnippet())) .build()
Java
Notification notification = new NotificationCompat.Builder(context, CHANNEL_ID) .setSmallIcon(R.drawable.new_mail) .setContentTitle(emailObject.getSenderName()) .setContentText(emailObject.getSubject()) .setLargeIcon(emailObject.getSenderAvatar()) .setStyle(new NotificationCompat.BigTextStyle() .bigText(emailObject.getSubjectAndSnippet())) .build();
建立收件匣樣式通知
如果您想新增多個簡短摘要 (例如收到的電子郵件中的摘要),請在通知中套用 NotificationCompat.InboxStyle
。這可讓您新增多行內容文字,並將每個文字截斷成一行,而非 NotificationCompat.BigTextStyle
所提供的一行連續文字。
如要新增一行,最多可呼叫 addLine()
六次,如以下範例所示。如果新增超過六行,則只會顯示前六行。
Kotlin
val notification = NotificationCompat.Builder(context, CHANNEL_ID) .setSmallIcon(R.drawable.baseline_email_24) .setContentTitle("5 New mails from Frank") .setContentText("Check them out") .setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.logo)) .setStyle( NotificationCompat.InboxStyle() .addLine("Re: Planning") .addLine("Delivery on its way") .addLine("Follow-up") ) .build()
Java
Notification notification = NotificationCompat.Builder(context, CHANNEL_ID) .setSmallIcon(R.drawable.baseline_email_24) .setContentTitle("5 New mails from Frank") .setContentText("Check them out") .setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.logo)) .setStyle( NotificationCompat.InboxStyle() .addLine("Re: Planning") .addLine("Delivery on its way") .addLine("Follow-up") ) .build();
結果如下圖所示:
在通知中顯示對話
套用 NotificationCompat.MessagingStyle
,在人數不限人數之間顯示依序顯示的訊息。這適用於訊息應用程式,因為可以分別處理傳送者名稱和訊息文字,提供一致的版面配置,而且每則訊息可以有多行長。
如要新增訊息,請呼叫 addMessage()
,並傳送訊息文字、接收時間和傳送者的名稱。您也可以把這項資訊做為 NotificationCompat.MessagingStyle.Message
物件傳遞,如以下範例所示:
Kotlin
val message1 = NotificationCompat.MessagingStyle.Message( messages[0].getText(), messages[0].getTime(), messages[0].getSender()) val message2 = NotificationCompat.MessagingStyle.Message( messages[1].getText(), messages[1].getTime(), messages[1].getSender()) val notification = NotificationCompat.Builder(context, CHANNEL_ID) .setSmallIcon(R.drawable.new_message) .setStyle( NotificationCompat.MessagingStyle(resources.getString(R.string.reply_name)) .addMessage(message1) .addMessage(message2)) .build()
Java
NotificationCompat.MessagingStyle.Message message1 = new NotificationCompat.MessagingStyle.Message(messages[0].getText(), messages[0].getTime(), messages[0].getSender()); NotificationCompat.MessagingStyle.Message message2 = new NotificationCompat.MessagingStyle.Message(messages[1].getText(), messages[1].getTime(), messages[1].getSender()); Notification notification = new NotificationCompat.Builder(context, CHANNEL_ID) .setSmallIcon(R.drawable.new_message) .setStyle(new NotificationCompat.MessagingStyle(resources.getString(R.string.reply_name)) .addMessage(message1) .addMessage(message2)) .build();
使用 NotificationCompat.MessagingStyle
時,系統會忽略指定給 setContentTitle()
和 setContentText()
的任何值。
您可以呼叫 setConversationTitle()
以新增顯示在對話上方的標題。這可以是使用者建立的群組名稱;如果沒有具體名稱,也可以提供對話中的參與者清單。請勿為一對一即時通訊設定對話標題,因為系統會將這個欄位的存在
來提示對話是一個群組
這個樣式僅適用於搭載 Android 7.0 (API 級別 24) 以上版本的裝置。如前文所述,使用相容性程式庫 (NotificationCompat
) 時,包含 MessagingStyle
的通知會自動改回支援的展開通知樣式。
為即時通訊對話建構這類通知時,請新增直接回覆動作。
使用媒體控制選項建立通知
套用 MediaStyleNotificationHelper.MediaStyle
即可顯示媒體播放控制項及追蹤資訊。
在建構函式中指定相關聯的 MediaSession
。這可讓 Android 顯示正確的媒體資訊。
最多呼叫 addAction()
五次,最多可顯示五個圖示按鈕。呼叫 setLargeIcon()
可設定專輯圖片。
與其他通知樣式不同,MediaStyle
也可讓您指定在收合的檢視畫面也會顯示的三個動作按鈕,藉此修改收合大小的內容檢視畫面。如要這麼做,請將動作按鈕索引提供給 setShowActionsInCompactView()
。
以下範例說明如何建立具有媒體控制項的通知:
Kotlin
val notification = NotificationCompat.Builder(context, CHANNEL_ID) // Show controls on lock screen even when user hides sensitive content. .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) .setSmallIcon(R.drawable.ic_stat_player) // Add media control buttons that invoke intents in your media service .addAction(R.drawable.ic_prev, "Previous", prevPendingIntent) // #0 .addAction(R.drawable.ic_pause, "Pause", pausePendingIntent) // #1 .addAction(R.drawable.ic_next, "Next", nextPendingIntent) // #2 // Apply the media style template. .setStyle(MediaStyleNotificationHelper.MediaStyle(mediaSession) .setShowActionsInCompactView(1 /* #1: pause button \*/)) .setContentTitle("Wonderful music") .setContentText("My Awesome Band") .setLargeIcon(albumArtBitmap) .build()
Java
Notification notification = new NotificationCompat.Builder(context, CHANNEL_ID) // Show controls on lock screen even when user hides sensitive content. .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) .setSmallIcon(R.drawable.ic_stat_player) // Add media control buttons that invoke intents in your media service .addAction(R.drawable.ic_prev, "Previous", prevPendingIntent) // #0 .addAction(R.drawable.ic_pause, "Pause", pausePendingIntent) // #1 .addAction(R.drawable.ic_next, "Next", nextPendingIntent) // #2 // Apply the media style template. .setStyle(new MediaStyleNotificationHelper.MediaStyle(mediaSession) .setShowActionsInCompactView(1 /* #1: pause button */)) .setContentTitle("Wonderful music") .setContentText("My Awesome Band") .setLargeIcon(albumArtBitmap) .build();
其他資源
如要進一步瞭解 MediaStyle
和可展開通知,請參閱下列參考資料。