【问题标题】:WatchOS dynamic notifications not working on Xcode 11.1 and later - didReceive not calledWatchOS 动态通知不适用于 Xcode 11.1 及更高版本 - 未调用 didReceive
【发布时间】:2019-11-18 18:47:47
【问题描述】:

我正在尝试从https://developer.apple.com/tutorials/swiftui/creating-a-watchos-app 获取 Apple Watch SwiftUI 通知教程。它适用于 Xcode 11.0,但在从 11.1 到 beta 11.3 的任何更新版本上都失败了。

收到通知时,NotificationController 中的didReceive 不会被触发。这是 NotificationController.swift 文件的代码:

import WatchKit
import SwiftUI
import UserNotifications

class NotificationController: WKUserNotificationHostingController<NotificationView> {
    var landmark: Landmark?
    var title: String?
    var message: String?

    let landmarkIndexKey = "landmarkIndex"

    override var body: NotificationView {
        NotificationView(title: title,
            message: message,
            landmark: landmark)
    }

    override func willActivate() {
        // This method is called when watch view controller is about to be visible to user
        super.willActivate()
    }

    override func didDeactivate() {
        // This method is called when watch view controller is no longer visible
        super.didDeactivate()
    }

    override func didReceive(_ notification: UNNotification) {
        let userData = UserData()

        let notificationData =
            notification.request.content.userInfo as? [String: Any]

        let aps = notificationData?["aps"] as? [String: Any]
        let alert = aps?["alert"] as? [String: Any]

        title = alert?["title"] as? String
        message = alert?["body"] as? String

        if let index = notificationData?[landmarkIndexKey] as? Int {
            landmark = userData.landmarks[index]
        }
    }
}

另外,这里是 PushNotificationPayload.apns 的内容:

{
    "aps": {
        "alert": {
            "title": "Silver Salmon Creek",
            "body": "You are within 5 miles of Silver Salmon Creek."
        },
        "category": "LandmarkNear",
        "thread-id": "5280"
    },

    "landmarkIndex": 1
}

还有其他人有这个问题吗?

【问题讨论】:

  • 我已经缩小了问题的范围。 watchOS 6.0 模拟器可以与 NotificationController 上的 didReceive 函数正常工作。在 watchOS 6.1 模拟器上运行相同的代码时,不会调用 didReceive 并且不会执行用于呈现动态通知的任何代码。对于遇到相同通知问题的其他人,我建议在 watchOS 6.0 模拟器上运行,直到此问题得到解决。
  • 只是模拟器的问题吗?我也在设备上看到了这个……watchOS 6.0 很好,watchOS 6.1 不行。
  • 6.1 的设备和模拟器都存在问题。它在 6.0 上的设备和模拟器上都可以正常工作。
  • 感谢您的确认。令人遗憾的是,Apple 发布的 watchOS 6.1 的 SwiftUI 通知完全损坏。遗憾的是,唯一的解决方案似乎是使用 WatchKit 进行通知(或者假设它在 6.2 中得到修复,将部署目标设置为 6.2,但谁愿意这样做……)

标签: ios xcode notifications watchkit apple-watch


【解决方案1】:

动态通知现已在 WatchOS beta 6.1.1 中运行

【讨论】:

    【解决方案2】:

    我在使用 Xcode 11.3 时遇到了同样的问题。它在我的代码中失败了,但在 Apple 完成的项目中却没有。仔细查看教程代码中的有效载荷(部分):

        "alert": {
            "body": "You are within 5 miles of Silver Salmon Creek."
            "title": "Silver Salmon Creek",
        },
    

    完成项目中的payload是:

        "alert": {
            "title": "Silver Salmon Creek",
            "body": "You are within 5 miles of Silver Salmon Creek."
        },
    

    他们颠倒了两条线。但是仔细观察,您会在他们的教程代码中看到“body”行末尾缺少逗号。这会导致 JSON 解析失败(静默)并且不会生成警报。加上逗号,它就可以正常工作了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-06
      • 2018-01-09
      • 2015-02-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多