gpt4 book ai didi

ios - WatchOS 动态通知不适用于 Xcode 11.1 及更高版本 - didReceive 未调用

转载 作者:行者123 更新时间:2023-12-01 23:19:56 24 4
gpt4 key购买 nike

我正在尝试从 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
}

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

最佳答案

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

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

完成的项目中的有效负载是:

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

他们颠倒了两条线。然而仔细观察,您会发现在他们的教程代码中,“body”行末尾缺少逗号。这会导致 JSON 解析失败(静默)并且不会生成警报。把逗号放进去就可以了。

关于ios - WatchOS 动态通知不适用于 Xcode 11.1 及更高版本 - didReceive 未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58921038/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com