Skip to content

Commit

Permalink
Add LocalNotifications module
Browse files Browse the repository at this point in the history
  • Loading branch information
Supereg committed Sep 13, 2024
1 parent c4cbf0c commit ba32953
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ extension Spezi {
/// }
/// }
/// ```
///
/// ## Topics
/// ### Action
/// - ``RequestNotificationAuthorizationAction``
public var requestNotificationAuthorization: RequestNotificationAuthorizationAction {
RequestNotificationAuthorizationAction()
}
Expand Down
82 changes: 82 additions & 0 deletions Sources/Spezi/Notifications/UserNotifications.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//
// This source file is part of the Stanford Spezi open-source project
//
// SPDX-FileCopyrightText: 2024 Stanford University and the project authors (see CONTRIBUTORS.md)
//
// SPDX-License-Identifier: MIT
//

@preconcurrency import UserNotifications


/// Interact with local notifications.
///
/// This module provides some easy to use API to schedule and manage local notifications.
///
/// ## Topics
///
/// ### Configuration
/// - ``init()``
///
/// ### Add a Notification Request
/// - ``add(isolation:request:)``
///
/// ### Notification Limits
/// - ``pendingNotificationsLimit``
/// - ``remainingNotificationLimit(isolation:)``
///
/// ### Categories
/// - ``add(isolation:categories:)``
public final class LocalNotifications: Module, DefaultInitializable, EnvironmentAccessible {
/// The total limit of simultaneously scheduled notifications.
///
/// The limit is `64`.
public static let pendingNotificationsLimit = 64

/// Configure the local notifications module.
public init() {}

/// Schedule a new notification request.
/// - Parameters:
/// - isolation: Inherits the current isolation.
/// - request: The notification request.
public func add( // swiftlint:disable:this function_default_parameter_at_end
isolation: isolated (any Actor)? = #isolation,

Check failure on line 44 in Sources/Spezi/Notifications/UserNotifications.swift

View workflow job for this annotation

GitHub Actions / Build and Test Swift Package iOS / Test using xcodebuild or run fastlane

'isolated' parameter has non-actor type '(any Actor)?'

Check failure on line 44 in Sources/Spezi/Notifications/UserNotifications.swift

View workflow job for this annotation

GitHub Actions / Build and Test Swift Package iOS / Test using xcodebuild or run fastlane

non-built-in macro cannot be used as default argument

Check failure on line 44 in Sources/Spezi/Notifications/UserNotifications.swift

View workflow job for this annotation

GitHub Actions / Build and Test Swift Package macOS / Test using xcodebuild or run fastlane

'isolated' parameter has non-actor type '(any Actor)?'

Check failure on line 44 in Sources/Spezi/Notifications/UserNotifications.swift

View workflow job for this annotation

GitHub Actions / Build and Test Swift Package macOS / Test using xcodebuild or run fastlane

non-built-in macro cannot be used as default argument

Check failure on line 44 in Sources/Spezi/Notifications/UserNotifications.swift

View workflow job for this annotation

GitHub Actions / Build and Test Swift Package tvOS / Test using xcodebuild or run fastlane

'isolated' parameter has non-actor type '(any Actor)?'

Check failure on line 44 in Sources/Spezi/Notifications/UserNotifications.swift

View workflow job for this annotation

GitHub Actions / Build and Test Swift Package tvOS / Test using xcodebuild or run fastlane

non-built-in macro cannot be used as default argument

Check failure on line 44 in Sources/Spezi/Notifications/UserNotifications.swift

View workflow job for this annotation

GitHub Actions / Build and Test Swift Package visionOS / Test using xcodebuild or run fastlane

'isolated' parameter has non-actor type '(any Actor)?'

Check failure on line 44 in Sources/Spezi/Notifications/UserNotifications.swift

View workflow job for this annotation

GitHub Actions / Build and Test Swift Package visionOS / Test using xcodebuild or run fastlane

non-built-in macro cannot be used as default argument

Check failure on line 44 in Sources/Spezi/Notifications/UserNotifications.swift

View workflow job for this annotation

GitHub Actions / Build and Test Swift Package watchOS / Test using xcodebuild or run fastlane

'isolated' parameter has non-actor type '(any Actor)?'

Check failure on line 44 in Sources/Spezi/Notifications/UserNotifications.swift

View workflow job for this annotation

GitHub Actions / Build and Test Swift Package watchOS / Test using xcodebuild or run fastlane

non-built-in macro cannot be used as default argument

Check failure on line 44 in Sources/Spezi/Notifications/UserNotifications.swift

View workflow job for this annotation

GitHub Actions / Build and Test UI Tests iOS / Test using xcodebuild or run fastlane

'isolated' parameter has non-actor type '(any Actor)?'

Check failure on line 44 in Sources/Spezi/Notifications/UserNotifications.swift

View workflow job for this annotation

GitHub Actions / Build and Test UI Tests iOS / Test using xcodebuild or run fastlane

non-built-in macro cannot be used as default argument

Check failure on line 44 in Sources/Spezi/Notifications/UserNotifications.swift

View workflow job for this annotation

GitHub Actions / Build and Test UI Tests visionOS / Test using xcodebuild or run fastlane

'isolated' parameter has non-actor type '(any Actor)?'

Check failure on line 44 in Sources/Spezi/Notifications/UserNotifications.swift

View workflow job for this annotation

GitHub Actions / Build and Test UI Tests visionOS / Test using xcodebuild or run fastlane

non-built-in macro cannot be used as default argument
request: sending UNNotificationRequest

Check failure on line 45 in Sources/Spezi/Notifications/UserNotifications.swift

View workflow job for this annotation

GitHub Actions / Build and Test Swift Package iOS / Test using xcodebuild or run fastlane

expected ',' separator

Check failure on line 45 in Sources/Spezi/Notifications/UserNotifications.swift

View workflow job for this annotation

GitHub Actions / Build and Test Swift Package iOS / Test using xcodebuild or run fastlane

unnamed parameters must be written with the empty name '_'

Check failure on line 45 in Sources/Spezi/Notifications/UserNotifications.swift

View workflow job for this annotation

GitHub Actions / Build and Test Swift Package iOS / Test using xcodebuild or run fastlane

cannot find type 'sending' in scope

Check failure on line 45 in Sources/Spezi/Notifications/UserNotifications.swift

View workflow job for this annotation

GitHub Actions / Build and Test Swift Package macOS / Test using xcodebuild or run fastlane

expected ',' separator

Check failure on line 45 in Sources/Spezi/Notifications/UserNotifications.swift

View workflow job for this annotation

GitHub Actions / Build and Test Swift Package macOS / Test using xcodebuild or run fastlane

unnamed parameters must be written with the empty name '_'

Check failure on line 45 in Sources/Spezi/Notifications/UserNotifications.swift

View workflow job for this annotation

GitHub Actions / Build and Test Swift Package macOS / Test using xcodebuild or run fastlane

cannot find type 'sending' in scope

Check failure on line 45 in Sources/Spezi/Notifications/UserNotifications.swift

View workflow job for this annotation

GitHub Actions / Build and Test Swift Package tvOS / Test using xcodebuild or run fastlane

expected ',' separator

Check failure on line 45 in Sources/Spezi/Notifications/UserNotifications.swift

View workflow job for this annotation

GitHub Actions / Build and Test Swift Package tvOS / Test using xcodebuild or run fastlane

unnamed parameters must be written with the empty name '_'

Check failure on line 45 in Sources/Spezi/Notifications/UserNotifications.swift

View workflow job for this annotation

GitHub Actions / Build and Test Swift Package tvOS / Test using xcodebuild or run fastlane

cannot find type 'sending' in scope

Check failure on line 45 in Sources/Spezi/Notifications/UserNotifications.swift

View workflow job for this annotation

GitHub Actions / Build and Test Swift Package visionOS / Test using xcodebuild or run fastlane

expected ',' separator

Check failure on line 45 in Sources/Spezi/Notifications/UserNotifications.swift

View workflow job for this annotation

GitHub Actions / Build and Test Swift Package visionOS / Test using xcodebuild or run fastlane

unnamed parameters must be written with the empty name '_'

Check failure on line 45 in Sources/Spezi/Notifications/UserNotifications.swift

View workflow job for this annotation

GitHub Actions / Build and Test Swift Package visionOS / Test using xcodebuild or run fastlane

cannot find type 'sending' in scope

Check failure on line 45 in Sources/Spezi/Notifications/UserNotifications.swift

View workflow job for this annotation

GitHub Actions / Build and Test Swift Package watchOS / Test using xcodebuild or run fastlane

expected ',' separator

Check failure on line 45 in Sources/Spezi/Notifications/UserNotifications.swift

View workflow job for this annotation

GitHub Actions / Build and Test Swift Package watchOS / Test using xcodebuild or run fastlane

unnamed parameters must be written with the empty name '_'

Check failure on line 45 in Sources/Spezi/Notifications/UserNotifications.swift

View workflow job for this annotation

GitHub Actions / Build and Test Swift Package watchOS / Test using xcodebuild or run fastlane

cannot find type 'sending' in scope

Check failure on line 45 in Sources/Spezi/Notifications/UserNotifications.swift

View workflow job for this annotation

GitHub Actions / Build and Test UI Tests iOS / Test using xcodebuild or run fastlane

expected ',' separator

Check failure on line 45 in Sources/Spezi/Notifications/UserNotifications.swift

View workflow job for this annotation

GitHub Actions / Build and Test UI Tests iOS / Test using xcodebuild or run fastlane

unnamed parameters must be written with the empty name '_'

Check failure on line 45 in Sources/Spezi/Notifications/UserNotifications.swift

View workflow job for this annotation

GitHub Actions / Build and Test UI Tests iOS / Test using xcodebuild or run fastlane

cannot find type 'sending' in scope

Check failure on line 45 in Sources/Spezi/Notifications/UserNotifications.swift

View workflow job for this annotation

GitHub Actions / Build and Test UI Tests visionOS / Test using xcodebuild or run fastlane

expected ',' separator

Check failure on line 45 in Sources/Spezi/Notifications/UserNotifications.swift

View workflow job for this annotation

GitHub Actions / Build and Test UI Tests visionOS / Test using xcodebuild or run fastlane

unnamed parameters must be written with the empty name '_'

Check failure on line 45 in Sources/Spezi/Notifications/UserNotifications.swift

View workflow job for this annotation

GitHub Actions / Build and Test UI Tests visionOS / Test using xcodebuild or run fastlane

cannot find type 'sending' in scope
) async throws {
try await UNUserNotificationCenter.current().add(request)
}

/// Retrieve the amount of notifications that can be scheduled for the app.
///
/// An application has a total limit of ``pendingNotificationsLimit`` that can be scheduled (pending). This method retrieve the reaming notifications that can be scheduled.
///
/// - Note: Already delivered notifications do not count towards this limit.
/// - Parameter isolation: Inherits the current isolation.
/// - Returns: Returns the remaining amount of notifications that can be scheduled for the application.
public func remainingNotificationLimit(isolation: isolated (any Actor)? = #isolation) async -> Int {

Check failure on line 57 in Sources/Spezi/Notifications/UserNotifications.swift

View workflow job for this annotation

GitHub Actions / Build and Test Swift Package iOS / Test using xcodebuild or run fastlane

'isolated' parameter has non-actor type '(any Actor)?'

Check failure on line 57 in Sources/Spezi/Notifications/UserNotifications.swift

View workflow job for this annotation

GitHub Actions / Build and Test Swift Package macOS / Test using xcodebuild or run fastlane

'isolated' parameter has non-actor type '(any Actor)?'

Check failure on line 57 in Sources/Spezi/Notifications/UserNotifications.swift

View workflow job for this annotation

GitHub Actions / Build and Test Swift Package tvOS / Test using xcodebuild or run fastlane

'isolated' parameter has non-actor type '(any Actor)?'

Check failure on line 57 in Sources/Spezi/Notifications/UserNotifications.swift

View workflow job for this annotation

GitHub Actions / Build and Test Swift Package visionOS / Test using xcodebuild or run fastlane

'isolated' parameter has non-actor type '(any Actor)?'

Check failure on line 57 in Sources/Spezi/Notifications/UserNotifications.swift

View workflow job for this annotation

GitHub Actions / Build and Test Swift Package watchOS / Test using xcodebuild or run fastlane

'isolated' parameter has non-actor type '(any Actor)?'

Check failure on line 57 in Sources/Spezi/Notifications/UserNotifications.swift

View workflow job for this annotation

GitHub Actions / Build and Test UI Tests iOS / Test using xcodebuild or run fastlane

'isolated' parameter has non-actor type '(any Actor)?'

Check failure on line 57 in Sources/Spezi/Notifications/UserNotifications.swift

View workflow job for this annotation

GitHub Actions / Build and Test UI Tests visionOS / Test using xcodebuild or run fastlane

'isolated' parameter has non-actor type '(any Actor)?'
let pendingRequests = await UNUserNotificationCenter.current().pendingNotificationRequests()
return max(0, Self.pendingNotificationsLimit - pendingRequests.count)
}

/// Add additional notification categories.
///
/// This method adds additional notification categories. Call this method within your configure method of your Module to ensure that categories are configured
/// as early as possible.
///
/// To receive the action that are performed for your category, implement the ``NotificationHandler/handleNotificationAction(_:)`` method of the
/// ``NotificationHandler`` protocol.
///
/// - Note: Aim to only call this method once at startup.
///
/// - Parameters:
/// - isolation: Inherits the current isolation.
/// - categories: The notification categories you support.
public func add( // swiftlint:disable:this function_default_parameter_at_end
isolation: isolated (any Actor)? = #isolation,
categories: Set<UNNotificationCategory>
) async {
let previousCategories = await UNUserNotificationCenter.current().notificationCategories()
UNUserNotificationCenter.current().setNotificationCategories(categories.union(previousCategories))
}
}
1 change: 1 addition & 0 deletions Sources/Spezi/Spezi.docc/Module/Notifications.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ implement the ``NotificationHandler/receiveRemoteNotification(_:)`` method.
## Topics

### Notifications
- ``LocalNotifications``
- ``Spezi/notificationSettings``
- ``Spezi/requestNotificationAuthorization``

Expand Down

0 comments on commit ba32953

Please sign in to comment.