Skip to content

Commit

Permalink
Merge pull request #353 from jonasrichardrichter/main
Browse files Browse the repository at this point in the history
Permission - Add requesting for specific notification types
  • Loading branch information
ivanvorobei authored Nov 18, 2024
2 parents 7fd4d6c + 430dba4 commit 80a04a4
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// NotificationAccess+userNotifcationAuthorizationOptions.swift
//
//
// Created by Jonas Richard Richter on 18.06.24.
//

import PermissionsKit
import UserNotifications

extension Permission.NotificationAccess {
var userNotifcationAuthorizationOptions: UNAuthorizationOptions {
switch self {
case .badge:
.badge
case .sound:
.sound
case .alert:
.alert
case .carPlay:
.carPlay
case .criticalAlert:
.criticalAlert
case .providesAppNotificationSettings:
.providesAppNotificationSettings
case .provisional:
.provisional
case .announcement:
if #available(iOS 13.0, watchOS 6.0, *) {
.announcement
} else {
.alert
}
case .timeSensitive:
if #available(iOS 15.0, tvOS 15.0, watchOS 8.0, *) {
.timeSensitive
} else {
.alert
}
}
}
}
22 changes: 15 additions & 7 deletions Sources/NotificationPermission/NotificationPermission.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,18 @@ import PermissionsKit
import UserNotifications

public extension Permission {

static var notification: NotificationPermission {
return NotificationPermission()
static func notification(access: Set<NotificationAccess> = [.alert, .badge, .sound]) -> NotificationPermission {
return NotificationPermission(kind: .notification(access: access))
}
}

public class NotificationPermission: Permission {
private var _kind: Permission.Kind
open override var kind: Permission.Kind { self._kind }

open override var kind: Permission.Kind { .notification }
init(kind: Permission.Kind) {
self._kind = kind
}

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

public override func request(completion: @escaping () -> Void) {
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
DispatchQueue.main.async {
completion()
switch _kind {
case .notification(let access):
center.requestAuthorization(options: UNAuthorizationOptions(access.map { $0.userNotifcationAuthorizationOptions })) { (granted, error) in
DispatchQueue.main.async {
completion()
}
}
default:
fatalError()
}
}
}
Expand Down
17 changes: 16 additions & 1 deletion Sources/PermissionsKit/Permission.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ open class Permission {
public enum Kind {

case camera
case notification
case notification(access: Set<NotificationAccess>)
case photoLibrary
case microphone
case calendar(access: CalendarAccess)
Expand Down Expand Up @@ -177,4 +177,19 @@ open class Permission {
case whenInUse
case always
}

public enum NotificationAccess {
case badge
case sound
case alert
case carPlay
case criticalAlert
case providesAppNotificationSettings
case provisional

@available(iOS, introduced: 13.0, deprecated: 15.0, message: "Only from iOS 13.0 to 15.0")
case announcement
@available(iOS, introduced: 15.0, deprecated: 15.0, message: "Only with iOS 15.0")
case timeSensitive
}
}

0 comments on commit 80a04a4

Please sign in to comment.