Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/upup 555 async local push notification service #42

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion Sources/Utils/Services/LocalNotificationService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<Bool, Error>) -> Void
) {
UNUserNotificationCenter.current().requestAuthorization(options: options) { success, error in
Expand Down
20 changes: 11 additions & 9 deletions UtilsExample/Source/UI/ExampleViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down Expand Up @@ -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)
Expand Down