I originally asked this question in the forum for Push Notifications. I was wondering if someone with tvOS experience can help answer my question about receiving silent cloud kit remote notifications to update my UI.
Hi - I’m having a hard time getting push notifications to work in tvOS. I have an iOS app that updates a cloud kit database. I also have a tvOS app that subscribes to this database for updates. This should be silent. However, the subscription for the tvOS app is not receiving the push notification. I’ve tried multiple different set ups.
Here is app delegate. What is crossed out is stuff I have tried before.
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
}
I have also added these delegate methods but have also tried to run without them because I believe cloud kit should take care of registering the device token.
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)")
}
Here is where i set up cloud kit subscription
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")
}
}
I also have background mode with remote notifications and background fetch highlighted but have also tried it without background fetch highlighted.
The code I’ve written also works when I use it on an iOS app. For some reason the code is not updating the UI on tvOS.
Any help would be greatly appreciated.
Best,
Ryan