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
extra 设置为 OPEN_ON_PHONE_ANIMATION
。
此方法可引导用户在手机上进行互动,并遵循启动后台进程的平台要求。
注意:您不能使用 BroadcastReceiver
作为通知操作的目标。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):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."]]