You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在WearOS上通过手机设置Firebase通知并打开手表应用

实现手机同步通知打开WearOS应用的解决方案

要让同步到WearOS的通知直接打开手表端应用,你需要针对Wear设备专门配置通知的交互逻辑,而非仅添加通用Action。以下是具体解决步骤:

核心修改要点

  • 借助NotificationCompat.WearableExtender为Wear设备单独定义通知交互,覆盖默认的手机端跳转行为
  • 确保Wear端WearMainActivity在Manifest中正确注册,保证Intent能被手表端正常解析

修改后的代码

将你的通知构建代码调整为如下形式:

/*******************************************
 * Wear Notification Block Started
 *********************************************/

Intent wearAppIntent = new Intent(this, WearMainActivity.class);
// 增加FLAG_UPDATE_CURRENT保证PendingIntent能正确更新,适配Wear端解析
PendingIntent wearAppPendingIntent = PendingIntent.getActivity(
        this, 
        108, 
        wearAppIntent, 
        PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT
);

NotificationCompat.Action wearAppAction = new NotificationCompat.Action.Builder(
        R.drawable.ic_stat_ic_notification, 
        "打开手表应用", 
        wearAppPendingIntent
).build();

/*******************************************
 * Wear Notification Block Ended
 *********************************************/

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId);
notificationBuilder
        .setSmallIcon(R.drawable.ic_stat_ic_notification)
        .setContentTitle(title)
        .setContentText(body)
        .setTicker(body)
        .setAutoCancel(true)
        .setSound(defaultSoundUri)
        .setContentIntent(pendingIntent) // 保留手机端的默认跳转逻辑
        .addAction(wearAppAction)
        // 为Wear设备配置专属交互:点击通知直接打开手表应用
        .extend(new NotificationCompat.WearableExtender()
                .setContentIntent(wearAppPendingIntent) // 覆盖Wear端的默认点击行为
                .setContentAction(0)); // 将自定义Action设为Wear端的默认操作

额外注意事项

  1. 确认WearOS应用的AndroidManifest.xml中,WearMainActivity已正确注册,无额外IntentFilter限制:
<activity
    android:name=".WearMainActivity"
    android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
  1. 保证手机与WearOS应用签名一致且包名完全相同,这是通知同步与跨设备Intent解析的必要前提
  2. 若仍不生效,可尝试更换PendingIntent的requestCode为唯一值,避免与其他PendingIntent冲突

内容的提问来源于stack exchange,提问作者Sumit Ojha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 23:12:03