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

Wear OS锁屏时全屏通知不显示问题求助

Wear OS手表锁屏时通知不显示的解决方案

问题背景

需求为Wear OS手表在锁屏状态下显示通知,当前实现方式为:创建带闹钟的Service,通过Receiver监听触发通知,已配置通知高优先级、CATEGORY_ALARM、VISIBILITY_PUBLIC、全屏Intent,并添加USE_FULL_SCREEN_INTENT和POST_NOTIFICATIONS权限,但锁屏时仅触发响铃震动,通知未显示。

现有代码

MainActivity

val intent = Intent(this, NotificationService::class.java)
this.startService(intent);

NotificationService

private fun startNotification() {
    val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
    val intent = Intent(this, AlarmReceiver::class.java)
    val pendingIntent = PendingIntent.getBroadcast(
        this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE or 0
    )

    // 设置闹钟触发时间
    val calendar: Calendar = Calendar.getInstance().apply {
        timeInMillis = System.currentTimeMillis()
        set(Calendar.HOUR_OF_DAY, 10)
        set(Calendar.MINUTE, 45)
    }

    // 使用setExactAndAllowWhileIdle确保在Doze模式下也能准确触发
    alarmManager.setExactAndAllowWhileIdle(
        AlarmManager.RTC_WAKEUP,
        calendar.timeInMillis,
        pendingIntent
    )
}

AlarmReceiver

override fun onReceive(context: Context, intent: Intent) {
    Log.d("RECEIVER", "ALARM");
    val intent = Intent(context, CancelActivity::class.java)
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
    val dismissIntent = Intent(context, CancelNotificationReceiver::class.java)
    val pendingIntent: PendingIntent = PendingIntent.getActivity(
        context,
        0,
        intent,
        PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE
    )
    val dismissIntentPending = PendingIntent.getBroadcast(
        context,
        0,
        dismissIntent,
        PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE
    )

    val notification = NotificationCompat.Builder(context, "alarm_channel")
        .setSmallIcon(androidx.core.R.drawable.ic_call_answer)
        .setPriority(NotificationCompat.PRIORITY_HIGH)
        .setCategory(NotificationCompat.CATEGORY_ALARM)
        .addAction(androidx.core.R.drawable.ic_call_answer, "救助要請", pendingIntent)
        .addAction(androidx.core.R.drawable.ic_call_decline, "キャンセル", dismissIntentPending)
        .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
        .setVibrate(longArrayOf(0, 1000, 500, 1000)).setFullScreenIntent(pendingIntent, true)
        .build()

    if (ActivityCompat.checkSelfPermission(
            context, Manifest.permission.POST_NOTIFICATIONS
        ) != PackageManager.PERMISSION_GRANTED
    ) {
        return
    }
    with(NotificationManagerCompat.from(context)) {
        notify(125, notification)
    }
}

解决步骤

1. 确保通知渠道配置正确

Wear OS对通知渠道的重要性要求严格,必须创建高优先级的渠道才能在锁屏显示:

// 在App启动时执行(如Application类或MainActivity.onCreate)
val channel = NotificationChannel(
    "alarm_channel",
    "闹钟通知",
    NotificationManager.IMPORTANCE_HIGH
).apply {
    description = "用于显示紧急闹钟类通知"
    enableVibration(true)
    vibrationPattern = longArrayOf(0, 1000, 500, 1000)
    lockscreenVisibility = Notification.VISIBILITY_PUBLIC // 强制锁屏显示内容
}
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)

2. 配置全屏Intent对应的Activity

给CancelActivity添加锁屏显示相关配置:

AndroidManifest.xml

<activity android:name=".CancelActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
    </intent-filter>
    <!-- 标记为全屏Intent的Activity -->
    <meta-data
        android:name="android.app.fullscreenintent"
        android:value="true" />
    <!-- 添加悬浮窗权限(部分设备需要) -->
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
</activity>

CancelActivity代码调整

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    // 设置窗口属性,确保锁屏时能显示并唤醒屏幕
    window.addFlags(
        WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
        or WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
        or WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
        or WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
    )
    setContentView(R.layout.activity_cancel)
}

3. 添加唤醒权限与屏幕唤醒逻辑

在AndroidManifest.xml中添加WAKE_LOCK权限:

<uses-permission android:name="android.permission.WAKE_LOCK" />

在AlarmReceiver中添加屏幕唤醒代码:

override fun onReceive(context: Context, intent: Intent) {
    // 唤醒屏幕
    val powerManager = context.getSystemService(Context.POWER_SERVICE) as PowerManager
    val wakeLock = powerManager.newWakeLock(
        PowerManager.SCREEN_BRIGHT_WAKE_LOCK or PowerManager.ACQUIRE_CAUSES_WAKEUP,
        "MyApp:AlarmWakeLock"
    )
    wakeLock.acquire(10 * 60 * 1000L) // 持有10分钟,可根据需求调整

    // 原有通知代码...
}

4. 验证系统通知设置

  • 打开手表的系统设置,找到你的App,确认通知权限已开启
  • 确保锁屏通知设置为显示所有通知内容,而非隐藏或仅显示标题

内容的提问来源于stack exchange,提问作者Huy Thao

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 07:20:09