diff --git a/Sources/Utils/Services/LocalNotificationService.swift b/Sources/Utils/Services/LocalNotificationService.swift index 72b3b39..adfb344 100644 --- a/Sources/Utils/Services/LocalNotificationService.swift +++ b/Sources/Utils/Services/LocalNotificationService.swift @@ -7,6 +7,26 @@ import UIKit +extension UNAuthorizationOptions { + public static var `default`: UNAuthorizationOptions { [.alert, .sound, .badge] } +} + +extension UNNotificationPresentationOptions { + public static var `default`: UNNotificationPresentationOptions { + if #available(iOS 14.0, *) { + return [.banner, .list, .sound, .badge] + } else { + return [.alert, .sound, .badge] + } + } +} + +extension UNAuthorizationStatus { + public var isDenied: Bool { self == .denied } + public var isNotDetermined: Bool { self == .notDetermined } + public var isAuthorized: Bool { self == .authorized } +} + public protocol LocalNotificationServiceDelegate: AnyObject { func localNotificationService( _ service: LocalNotificationService, @@ -19,13 +39,32 @@ public protocol LocalNotificationServiceDelegate: AnyObject { ) } +extension LocalNotificationServiceDelegate { + public func localNotificationService( + _ service: LocalNotificationService, + willPresent notification: UNNotification + ) -> UNNotificationPresentationOptions { + .default + } +} + public class LocalNotificationService: NSObject, UNUserNotificationCenterDelegate { public static let shared = LocalNotificationService() public weak var delegate: LocalNotificationServiceDelegate? + public var authorizationStatus: UNAuthorizationStatus { + get async { + await UNUserNotificationCenter.current().notificationSettings().authorizationStatus + } + } + + public func requestAuthorization(options: UNAuthorizationOptions = .default) async throws -> Bool { + try await UNUserNotificationCenter.current().requestAuthorization(options: options) + } + public func requestAuthorization( - options: UNAuthorizationOptions = [.alert, .sound, .badge], + options: UNAuthorizationOptions = .default, completion: @escaping (Result) -> Void ) { UNUserNotificationCenter.current().requestAuthorization(options: options) { success, error in diff --git a/UtilsExample/Source/UI/ExampleViewController.swift b/UtilsExample/Source/UI/ExampleViewController.swift index b7c354c..58a29cf 100644 --- a/UtilsExample/Source/UI/ExampleViewController.swift +++ b/UtilsExample/Source/UI/ExampleViewController.swift @@ -29,16 +29,17 @@ final class ExampleViewController: UIViewController { view.backgroundColor = .white - pushService.requestAuthorization { result in - switch result { - case .success(let isAuthorised): - if isAuthorised { + Task { + do { + let isAuthorized = try await LocalNotificationService.shared.requestAuthorization() + + if isAuthorized { print(Constants.successAuthorization) } else { print(Constants.nonSuccessAuthorization) } - case .failure(let error): - print(error.localizedDescription) + } catch let error { + print(error) } } } @@ -81,9 +82,10 @@ final class ExampleViewController: UIViewController { } @IBAction private func pushNotificationButtonTap(_ sender: UIButton) { - pushService.getAuthorizationStatus { [weak self] status in - if status == .authorized { - self?.createNotificationRequest() + Task { + let status = await LocalNotificationService.shared.authorizationStatus + if status.isAuthorized { + createNotificationRequest() print(Constants.successAuthorizationStatus) } else { print(Constants.nonSuccessAuthorizationStatus)