我很难让推送通知在tvOS中工作。我有一个iOS应用程序,用于更新云工具包数据库。我也有一个tvOS应用程序,该应用程序订阅了此数据库以进行更新。此更新应保持沉默。但是,tvOS应用程序的订阅未收到推送通知。我尝试了多种不同的设置。
这是应用程序委托。划掉的是我以前尝试过的东西。
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions:
[UIApplication.LaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController =
UINavigationController(rootViewController: ViewController() )
let center = UNUserNotificationCenter.current()
center.delegate = self
// center.requestAuthorization(options: [.provisional]) {
(granted , error) in
// guard error == nil else {return}
// guard granted else {return}
// DispatchQueue.main.async {
// application.registerForRemoteNotifications()
// }
// }
application.registerForRemoteNotifications()
return true
}
我还添加了这些委托方法,但也尝试在没有它们的情况下运行,因为我相信云工具包应注意注册设备令牌。
func application(_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable : Any],
fetchCompletionHandler completionHandler: @escaping
(UIBackgroundFetchResult) -> Void) {
print("received remote notification")
let notification =
CKQueryNotification(fromRemoteNotificationDictionary: userInfo)
if notification.queryNotificationReason == .recordUpdated {
//update the word with local notification
NotificationCenter.default.post(name: name, object: nil)
}
}
func application(_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)
{
print("device token: \(deviceToken)")
}
func application(_ application: UIApplication,
didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("error: \(error)")
}
这是我设置云工具包订阅的地方
func setUpCloudKitSubscription() {
let alreadySaved = UserDefaults.standard.bool(forKey:
subscriptionSaved)
guard !alreadySaved else {print("slready saved")
return
}
let predicate = NSPredicate(value: true)
let subscription = CKQuerySubscription(recordType: "Word",
predicate: predicate, subscriptionID: subscriptionID, options:
[.firesOnRecordUpdate, .firesOnRecordCreation])
let info = CKSubscription.NotificationInfo()
info.shouldSendContentAvailable = true
subscription.notificationInfo = info
publicDatabase.save(subscription) { (subscription, error) in
guard error == nil else {return}
UserDefaults.standard.set(true, forKey:
self.subscriptionSaved)
print("subscription saved")
}
}
我还具有远程通知的后台模式,突出显示了后台提取,但是也尝试了不突出显示后台提取的情况。
我编写的代码在iOS应用上使用时也可以使用。由于某些原因,代码无法在tvOS上更新UI。
Any help would be greatly appreciated.
Best,
Ryan