嗨,我们有一个项目,其 QA 目标和生产目标是用 swift 编写的,而 watch 目标是用 swiftui 编写的。在第一个目标中,推送通知运行良好,但在 watch 目标中,推送通知确实收到了调用的函数,但得到的用户信息为空。我不明白可能是什么问题。可能缺少某个定义,或者由于对目标的不正确管理而产生了问题。因为目前大部分与Push相关的代码都在App Delegate中请帮帮我。谢谢
这是具有推送通知委托功能的类:
import UserNotifications
import SwiftUI
class NotificationCenter: NSObject, ObservableObject {
@Published var request: UNNotificationRequest?
override init() {
super.init()
setUpNotificationPreferences()
}
func setUpNotificationPreferences() {
let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
if granted {
let category = UNNotificationCategory(identifier: "SAVE_CATEGORY", actions: [], intentIdentifiers: [], options: [])
center.setNotificationCategories(Set([category]))
} else {
print("No permission" + error.debugDescription)
}
}
}
}
extension NotificationCenter: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
self.request = notification.request
completionHandler([.alert, .sound, .badge])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
self.request = response.notification.request
completionHandler()
}
}