Skip to content

Commit 8d434ef

Browse files
Add notification permission options
1 parent 9824629 commit 8d434ef

File tree

3 files changed

+73
-8
lines changed

3 files changed

+73
-8
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//
2+
// NotificationAccess+userNotifcationAuthorizationOptions.swift
3+
//
4+
//
5+
// Created by Jonas Richard Richter on 18.06.24.
6+
//
7+
8+
import PermissionsKit
9+
import UserNotifications
10+
11+
extension Permission.NotificationAccess {
12+
var userNotifcationAuthorizationOptions: UNAuthorizationOptions {
13+
switch self {
14+
case .badge:
15+
.badge
16+
case .sound:
17+
.sound
18+
case .alert:
19+
.alert
20+
case .carPlay:
21+
.carPlay
22+
case .criticalAlert:
23+
.criticalAlert
24+
case .providesAppNotificationSettings:
25+
.providesAppNotificationSettings
26+
case .provisional:
27+
.provisional
28+
case .announcement:
29+
if #available(iOS 13.0, watchOS 6.0, *) {
30+
.announcement
31+
} else {
32+
.alert
33+
}
34+
case .timeSensitive:
35+
if #available(iOS 15.0, tvOS 15.0, watchOS 8.0, *) {
36+
.timeSensitive
37+
} else {
38+
.alert
39+
}
40+
}
41+
}
42+
}

Sources/NotificationPermission/NotificationPermission.swift

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,18 @@ import PermissionsKit
2727
import UserNotifications
2828

2929
public extension Permission {
30-
31-
static var notification: NotificationPermission {
32-
return NotificationPermission()
30+
static func notification(access: Set<NotificationAccess> = [.alert, .badge, .sound]) -> NotificationPermission {
31+
return NotificationPermission(kind: .notification(access: access))
3332
}
3433
}
3534

3635
public class NotificationPermission: Permission {
36+
private var _kind: Permission.Kind
37+
open override var kind: Permission.Kind { self._kind }
3738

38-
open override var kind: Permission.Kind { .notification }
39+
init(kind: Permission.Kind) {
40+
self._kind = kind
41+
}
3942

4043
public override var status: Permission.Status {
4144
guard let authorizationStatus = fetchAuthorizationStatus() else { return .notDetermined }
@@ -64,10 +67,15 @@ public class NotificationPermission: Permission {
6467

6568
public override func request(completion: @escaping () -> Void) {
6669
let center = UNUserNotificationCenter.current()
67-
center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
68-
DispatchQueue.main.async {
69-
completion()
70+
switch _kind {
71+
case .notification(let access):
72+
center.requestAuthorization(options: UNAuthorizationOptions(access.map { $0.userNotifcationAuthorizationOptions })) { (granted, error) in
73+
DispatchQueue.main.async {
74+
completion()
75+
}
7076
}
77+
default:
78+
fatalError()
7179
}
7280
}
7381
}

Sources/PermissionsKit/Permission.swift

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ open class Permission {
102102
public enum Kind {
103103

104104
case camera
105-
case notification
105+
case notification(access: Set<NotificationAccess>)
106106
case photoLibrary
107107
case microphone
108108
case calendar(access: CalendarAccess)
@@ -171,4 +171,19 @@ open class Permission {
171171
case whenInUse
172172
case always
173173
}
174+
175+
public enum NotificationAccess {
176+
case badge
177+
case sound
178+
case alert
179+
case carPlay
180+
case criticalAlert
181+
case providesAppNotificationSettings
182+
case provisional
183+
184+
@available(iOS, introduced: 13.0, deprecated: 15.0, message: "Only from iOS 13.0 to 15.0")
185+
case announcement
186+
@available(iOS, introduced: 15.0, deprecated: 15.0, message: "Only with iOS 15.0")
187+
case timeSensitive
188+
}
174189
}

0 commit comments

Comments
 (0)