Hi We have a project with target for QA and target for production wrote in swift, and target for watch wrote in swiftui. in the both first targets, the push notification worked well, but in the watch target, push notification did received function called but get empty user info. I can not understand what the problem could be. Maybe a certain definition is missing, or the problem was created due to the incorrect management of the targets. Because currently most of the code related to Push is in App Delegate Please help me. Thank you
This is the class with the push notification delegate functions:
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()
}
}
So I ran into this same issue today and, at least on my end, the issue turned out to be that watchOS is seemingly handling/decoding APN payloads differently than iOS.
Our API was passing a few null
values for KVP's that could have values in some other circumstances, and while those decode fine into something like String?
on iOS, they weren't decoding on watchOS. I ended up switching out the null
values to empty strings, zeros, etc and userInfo
suddenly started working.