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

如何配置iOS本地通知Payload使watchOS显示自定义Long-Look UI?

解决watchOS扩展本地通知Long-Look UI渲染问题

嘿,你踩了两个常见的小坑:一是误用了模拟器专用的测试字段,二是userInfo的结构配置不对。咱们一步步来修正:

核心问题拆解

你用.apns测试时功能正常,是因为文件里的WatchKit Simulator Actions是仅用于模拟器调试的专属字段,真机环境下完全无效;而且你在iOS代码里把它嵌套到userInfo的userData子字典里,watchOS扩展根本识别不到。另外,自定义动作按钮不应该放在userInfo里,而是要通过通知Category的正式配置来实现。

正确配置步骤

1. 先在watchOS扩展中注册带自定义动作的通知Category

在你的watchOS Extension的ExtensionDelegate(或启动初始化代码)里,先注册对应类别,让手表能识别myCategory对应的动作和Long-Look界面:

func applicationDidFinishLaunching() {
    // 创建两个自定义动作
    let action1 = UNNotificationAction(identifier: "button1Action", title: "Button 1", options: [])
    let action2 = UNNotificationAction(identifier: "button2Action", title: "Button 2", options: [])
    
    // 创建关联动作与动态场景的Category
    let category = UNNotificationCategory(identifier: "myCategory",
                                          actions: [action1, action2],
                                          intentIdentifiers: [],
                                          options: .customDismissAction)
    
    // 注册Category到系统
    UNUserNotificationCenter.current().setNotificationCategories([category])
}

2. iOS端调度本地通知的正确代码

不需要再用WatchKit Simulator Actions字段,直接设置正确的categoryIdentifier,自定义业务数据可以直接放在userInfo顶层(如果需要传递的话):

let content = UNMutableNotificationContent()
content.title = "Early bird"
content.categoryIdentifier = "myCategory"
// 自定义数据直接放在userInfo顶层,无需嵌套
content.userInfo = [
    "customKey": "你的业务自定义数据"
]

// 配置通知触发条件(这里示例为5秒后触发)
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)

// 提交通知请求
UNUserNotificationCenter.current().add(request) { error in
    if let error = error {
        print("通知提交失败: \(error)")
    }
}

验证Long-Look UI的关键注意点

  • 确保watchOS故事板中,动态通知场景的Category Identifier已设置为myCategory(和代码中的标识完全一致)
  • 测试时需将iPhone锁定,通知会自动推送到手表,下拉通知即可看到Long-Look界面和自定义按钮

内容的提问来源于stack exchange,提问作者7ball

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 06:36:35