Wear OS 通知
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
手錶通知與手機通知使用相同的 API,兩者的結構也相同。
通知在手錶上有兩種顯示方式:
- 由行動應用程式建立通知,然後由系統自動橋接到手錶。
- 由穿戴式應用程式建立通知。
這兩種情況都可使用 NotificationCompat.Builder
類別建立通知。您使用 builder 類別建構通知時,系統可以妥善處理顯示通知作業。舉例來說,當您從行動應用程式發出通知時,每個通知都會以卡片形式顯示在通知串中。
請查看下列範例,瞭解通知的顯示方式。
圖 1. 手機和手錶顯示相同的通知。
為求最佳結果,請使用 NotificationCompat.Style
子類別。
注意:
使用 RemoteViews
可移除自訂版面配置的通知,穿戴式裝置則只會顯示文字和圖示。
建議讓穿戴式裝置使用的通知
建議將可展開式通知做為所有通知的基礎,因為它是與穿戴式裝置使用者互動的好方法。通知列會顯示呈現收合狀態的通知,使用者可一目瞭然。當使用者輕觸通知,通知會展開並顯示更多內容和動作,使用者可以捲動查看,享有沈浸式體驗。
可展開式通知的建立方式和您在行動裝置上使用任何 NotificationCompat.Style
子類別建立通知的方式相同。舉例來說,一般使用 NotificationCompat.MessagingStyle
的通知看起來會像這樣:
圖 2. Wear OS 上的 MessagingStyle
通知範例。
您可以看到通知在展開狀態時,底部顯示了多種動作。
NotificationCompat.BigPictureStyle
、NotificationCompat.BigTextStyle
、NotificationCompat.InboxStyle
及 NotificationCompat.MessagingStyle
等範例請參考 GitHub 的通知示例。
提示:如果您的通知內有「回覆」動作 (例如訊息應用程式的通知),您可以加強通知的行為。舉例來說,您可以啟用語音輸入功能,直接從穿戴式裝置回覆,或是利用 setChoices()
輸入預先定義的文字回覆內容。詳情請參閱「新增回覆按鈕」。
避免重複通知
根據預設,系統會將通知從配對手機應用程式橋接到任何配對的手錶。如果您並未安裝任何穿戴式應用程式,建議採用這種做法。
不過,如果您建構了獨立的手錶應用程式,並有配對手機應用程式,那麼應用程式便會建立重複通知。
Wear OS 可以利用橋接 API 防止重複通知。如果應用程式在搭載 Wear OS 5 以上版本的裝置上執行,這點就格外重要,因為在行動裝置上可關閉的部分通知,在 Wear OS 裝置上無法關閉。詳情請參閱「通知的橋接設定選項」。
為通知新增穿戴式裝置特有的功能
如果您需要為通知新增穿戴式裝置專屬功能,例如從穿戴式裝置通知中隱藏應用程式圖示,或是讓使用者透過語音輸入文字回覆,可以使用 NotificationCompat.WearableExtender
類別指定選項。如要使用這個 API,請按照下列步驟操作:
-
建立
WearableExtender
的例項,為通知設定穿戴式裝置專屬選項。
- 依據本指南先前所述,建立
NotificationCompat.Builder
的例項,為通知設定您想要的屬性。
-
在通知上呼叫
extend()
,並傳遞給 WearableExtender
。這麼做會為通知套用穿戴式裝置選項。
- 呼叫
build()
以建構通知。
注意:如果您使用架構的 NotificationManager
,則 NotificationCompat.WearableExtender
的部分功能將無法使用,因此請務必使用 NotificationCompat
。
您可以在使用者的不同裝置間同步處理通知的關閉或取消行為。如果想同步處理關閉行為,請使用 setDismissalId()
方法。在每個通知呼叫 setDismissalId()
時,用字串形式傳遞全域專屬 ID。當使用者關閉通知,系統會在手錶和配對手機上,關閉使用同一個關閉 ID 的通知。如果想擷取關閉 ID,請使用 getDismissalId()
。
指定穿戴式裝置專用動作
如想在手錶和手機上提供不同動作,請使用 WearableExtender.addAction()
。使用此方法新增動作後,穿戴式裝置便不會顯示任何透過 NotificationCompat.Builder.addAction()
新增的動作。使用 WearableExtender.addAction()
新增的動作只會顯示在穿戴式裝置上,不會顯示在手機上。
從穿戴式裝置啟動手機應用程式
如果使用橋接通知,任何通知都會自動包含啟動手機應用程式的按鈕。不過,如果您使用手錶建立的本機通知,請按照下列步驟建立按鈕,在手機上啟動應用程式:
- 建立擴充
ConfirmationActivity
的新 Activity
。
- 在新
Activity
中使用 RemoteActivityHelper
啟動電話應用程式。
- 建構
Intent
以從通知啟動 Activity
時,請將 EXTRA_ANIMATION_TYPE
額外內容設為 OPEN_ON_PHONE_ANIMATION
。
這種做法會引導使用者在手機上互動,並遵循啟動背景程序的平台規定。
注意:您無法使用 BroadcastReceiver
做為通知動作的目標。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[null,null,["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Notifications on Wear OS\n\nNotifications on watches use the same APIs and have the same structure as notifications on phones.\n\n\nNotifications can appear on a watch in two ways:\n\n1. A mobile app creates a notification and the system [automatically bridges](/training/wearables/notifications/bridger) that notification to the watch.\n2. A wearable app creates a notification.\n\n\nFor both scenarios, use the\n[NotificationCompat.Builder](/reference/androidx/core/app/NotificationCompat.Builder)\nclass to create notifications. When you build notifications with the builder class, the system\ntakes care of displaying notifications properly. For example, when you issue a notification from\nyour mobile app, each notification appears as a card on the Notification Stream.\n\n\nReview the following example to see how notifications display.\n\n\n**Figure 1.** The same notification displayed on a phone and on a watch.\n\n\nUse one of the\n[NotificationCompat.Style](/reference/androidx/core/app/NotificationCompat.Style)\nsubclasses for the best results.\n\n**Note:**\nUsing [RemoteViews](/reference/android/widget/RemoteViews)\nstrips notifications of custom layouts, and the wearable only displays the text and icons.\n\nRecommended notifications for wearables\n---------------------------------------\n\n\nUse expandable notifications as the starting point for all notifications, as they are\na great way to engage wearable users. The collapsed state displays in the notification\ntray for a short, glanceable experience. If the user taps it, the notification expands,\nrevealing an immersive, scrollable experience of additional content and actions.\n\n\nYou can [Create an expandable notification](/training/notify-user/expanded)\nthe same way you would on mobile, using any of the `NotificationCompat.Style`\nsubclasses. For example, a standard notification using\n[NotificationCompat.MessagingStyle](/reference/androidx/core/app/NotificationCompat.MessagingStyle)\nlooks like this:\n\n\n**Figure 2.** Example of a `MessagingStyle` notification on Wear OS.\n\n\nYou can see the notification has multiple\n[actions](/develop/ui/views/notifications/build-notification#Actions) stacked at the bottom of the\nexpanded state.\n\n\nFor examples of\n[NotificationCompat.BigPictureStyle](/reference/androidx/core/app/NotificationCompat.BigPictureStyle),\n[NotificationCompat.BigTextStyle](/reference/androidx/core/app/NotificationCompat.BigTextStyle),\n[NotificationCompat.InboxStyle](/reference/androidx/core/app/NotificationCompat.InboxStyle),\nand [NotificationCompat.MessagingStyle](/reference/androidx/core/app/NotificationCompat.MessagingStyle),\ncheck out the [Notification sample](https://github.com/android/user-interface-samples/tree/d192c9d2000e43f63cca3eb1b9af05cf2e2ee867/Notifications)\non GitHub.\n\n\n**Tip:** If your notifications include a \"reply\" action, such as for a messaging\napp, you can enhance the behavior of the notification. For example, you can enable voice input\nreplies directly from the wearable or pre-defined text responses with\n[setChoices()](/reference/androidx/core/app/RemoteInput.Builder#setChoices(java.lang.CharSequence[])).\nFor more information, read\n[Add the reply button](/training/notify-user/build-notification#add-reply-action).\n\nAvoid duplicate Notifications\n-----------------------------\n\n\nBy default, notifications are bridged from a companion phone app to any paired watches. This is a\ngreat option if you don't have a wearable app installed.\n\n\nHowever, if you build a standalone watch app and a companion phone app, the apps\ncreate duplicate notifications.\n\n\nWear OS provides a way to stop duplicate notifications with the Bridging APIs. This is\nparticularly important for apps on devices that run Wear OS 5 or higher, because some\nnotifications that are dismissible on a mobile device aren't dismissible on the Wear OS device.\nFor more information, read\n[Bridging options for notifications](/training/wearables/notifications/bridger).\n\nAdd wearable-specific features to a notification\n------------------------------------------------\n\n\nIf you need to add wearable-specific features to a notification, such as hiding\nan app icon from the wearable notification or letting users dictate a text response\nwith voice input, you can use the\n`NotificationCompat.WearableExtender` class to specify the options.\nTo use this API, do the following:\n\n1. Create an instance of a [`WearableExtender`](/reference/androidx/core/app/NotificationCompat.WearableExtender), setting the wearable-specific options for the notification.\n2. Create an instance of `NotificationCompat.Builder`, setting the desired properties for your notification as described earlier in this guide.\n3. Call [`extend()`](/reference/androidx/core/app/NotificationCompat.Builder#extend(androidx.core.app.NotificationCompat.Extender)) on the notification and pass in the `WearableExtender`. This applies the wearable options to the notification.\n4. Call [`build()`](/reference/androidx/core/app/NotificationCompat.Builder#build()) to build the notification.\n\n**Note:**\nIf you use the framework's [NotificationManager](/reference/android/app/NotificationManager),\nsome features from\n`NotificationCompat.WearableExtender` don't work, so make sure to use\n[`NotificationCompat`](/reference/androidx/core/app/NotificationCompat).\n\n\nYou can sync dismissals or cancellations of notifications across the user's devices. To\nsync a dismissal, use the\n[`setDismissalId()`](/reference/androidx/core/app/NotificationCompat.WearableExtender#setDismissalId(java.lang.String)) method. For each notification, pass a globally unique\nID as a string when you call\n`setDismissalId()`. When the notification is dismissed, all other\nnotifications with the same dismissal ID are dismissed on the watch and on the\ncompanion phone. To retrieve a dismissal ID, use\n[`getDismissalId()`](/reference/androidx/core/app/NotificationCompat.WearableExtender#getDismissalId()).\n\nSpecify wearable-only actions\n-----------------------------\n\n\nIf you want different actions available on the watch and the phone, then use\n[`WearableExtender.addAction()`](/reference/androidx/core/app/NotificationCompat.WearableExtender#addAction(androidx.core.app.NotificationCompat.Action)). Once you add an action with this method,\nthe wearable does not display any other actions added with\n[`NotificationCompat.Builder.addAction()`](/reference/androidx/core/app/NotificationCompat.Builder#addAction(androidx.core.app.NotificationCompat.Action)). The actions added with\n`WearableExtender.addAction()` appear only on the wearable, not\non the phone.\n\nLaunch your phone app from a wearable device\n--------------------------------------------\n\n\nIf you are using bridged notifications, any notification automatically includes a button to launch\nthe app on the phone. However, if you are using a local notification, created on the watch, use\nthe following steps to create a button that launches the app on the phone:\n\n1. Create a new `Activity` that extends [`ConfirmationActivity`](/reference/androidx/wear/activity/ConfirmationActivity).\n2. Use [`RemoteActivityHelper`](/reference/androidx/wear/remote/interactions/RemoteActivityHelper) in the new `Activity` to launch the phone app.\n3. When building the `Intent` to launch the `Activity` from the notification, set the [`EXTRA_ANIMATION_TYPE`](/reference/androidx/wear/activity/ConfirmationActivity#EXTRA_ANIMATION_TYPE()) extra to [`OPEN_ON_PHONE_ANIMATION`](/reference/androidx/wear/activity/ConfirmationActivity#OPEN_ON_PHONE_ANIMATION()).\n\nThis approach guides the user to interacting on their phone and follows the platform requirements for launching background processes.\n\n\u003cbr /\u003e\n\n\n**Note:** You can't use a `BroadcastReceiver` as the target\nof the notification action."]]