1

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()
    }
}

Rachel
  • 13
  • 2
  • Are you targeting push notifications directly to the watch app or sending them to your iPhone app and having them appear on the watch when iOS sends it there? – Paulw11 May 15 '23 at 10:27
  • I send them to iphone app and have them appear on the watch when ios sends it their. but i have those functions (willPresent func and didReceive func) in watch target as well. – Rachel May 15 '23 at 18:54

1 Answers1

0

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.

Jason Cox
  • 26
  • 3
  • Thank you. right now only the time stamp field comes null. Can this affect? – Rachel May 16 '23 at 07:46
  • Thank you so much for helping me. Indeed, this was the problem and after we removed null values from the push, the problem was solved. – Rachel May 16 '23 at 11:24