Trying to set the delegate of UNUserNotificationCenterDelegate as AppDelegate but delegate is none

34 Views Asked by At

I have this problem that I need to have a method to encapsulate setting the delegate of UNUserNotificationCenterDelegate inside my LocalNotificationsManager class. Basically my code is:

weak public var delegate: UNUserNotificationCenterDelegate?

public func configureUserNotificationsDelegate(_ delegate: UNUserNotificationCenterDelegate) {
        center.delegate = delegate
    }

Here is my extension in my manager class:

extension LocalNotificationsManager: UNUserNotificationCenterDelegate {
    public func userNotificationCenter(_ center: UNUserNotificationCenter,
                                       willPresent notification: UNNotification
    ) async -> UNNotificationPresentationOptions {
        return [.banner, .sound]
    }
}

And I need to use it in my app Delegate like this:

 private func configureUserNotificationsDelegate() {
        // some code to configure notificationManager...
        notificationManager.configureUserNotificationsDelegate(self)
    }

After that I call my configure method in the didFinishLaunchingWithOptions method of the app delegate. The problem is that my delegate has a value of none and I do not understand why is it so, it's not nil but none does not work either. IN my deinit the delegate becomes nil. What am I doing wrong? I need to be able to see local notifications while the app is on foreground.

Thanks in advance.

0

There are 0 best solutions below