This repository has been archived by the owner on Sep 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Status.swift
62 lines (55 loc) · 2 KB
/
Status.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import Foundation
import UserNotifications
#if canImport(WatchKit)
import WatchKit
#elseif canImport(UIKit)
import UIKit
typealias Application = UIApplication
#elseif canImport(AppKit)
import AppKit
typealias Application = NSApplication
#endif
@available(iOSApplicationExtension, unavailable)
@available(watchOSApplicationExtension, unavailable)
@available(tvOSApplicationExtension, unavailable)
@available(macCatalystApplicationExtension, unavailable)
@available(OSXApplicationExtension, unavailable)
/// Provides convenience methods for requesting and checking notifications permissions.
public class AUNotificationPermissions {
/// Check if notifications are allowed
public static var allowed: Bool {
get async {
await status == .authorized ? true : false
}
}
/// Check the notification permission status
public static var status: UNAuthorizationStatus {
get async {
await withCheckedContinuation { continuation in
UNUserNotificationCenter.current().getNotificationSettings { settings in
continuation.resume(returning: settings.authorizationStatus)
}
}
}
}
/// Request notification permissions
/// - Parameter options: Requested notification options
@discardableResult
public static func request(_ options: UNAuthorizationOptions? = nil) async throws -> Bool {
let options = options ?? [.badge, .alert, .sound]
let notificationsAllowed = try await UNUserNotificationCenter.current().requestAuthorization(
options: options
)
return notificationsAllowed
}
/// Register device with APNs
public static func registerForRemoteNotifications() async {
await MainActor.run {
#if canImport(WatchKit)
WKExtension.shared().registerForRemoteNotifications()
#else
Application.shared.registerForRemoteNotifications()
#endif
}
}
}