Simplify User Notifications in Spezi-based applications.
SpeziNotifications simplifies interaction with user notifications by adding additional actions to the Environment of SwiftUI Views and Spezi Modules.
You can use the Notifications
module to interact with user notifications within your application. You can either define it as a dependency
of your Spezi Module
or retrieve it from the environment using the @Environment
property wrapper in your SwiftUI View.
The code example below schedules a notification request, accessing the Notifications
module from within the custom MyNotifications
module.
import Spezi
import UserNotifications
final class MyNotifications: Module {
@Dependency(Notifications.self)
private var notifications
@Application(\.notificationSettings)
private var settings
func scheduleAppointmentReminder() async throws {
let status = await settings().authorizationStatus
guard status == .authorized || status == .provisional else {
return // no authorization to schedule notification
}
let content = UNMutableNotificationContent()
content.title = "Your Appointment"
content.body = "Your appointment is in 3 hours"
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 3 * 60, repeats: false)
let request = UNNotificationRequest(identifier: "3-hour-reminder", content: content, trigger: trigger)
try await notifications.add(request: request)
}
}
The Notification module and notification-related actions are also available in the SwiftUI Environment. The code example below creates a simple notification authorization onboarding view that (1) determines the current authorization status and (2) request notification authorization when the user taps the button.
import SpeziNotifications
import SpeziViews
struct NotificationOnboarding: View {
@Environment(\.notificationSettings)
private var notificationSettings
@Environment(\.requestNotificationAuthorization)
private var requestNotificationAuthorization
@State private var viewState: ViewState = .idle
@State private var notificationsAuthorized = false
var body: some View {
VStack {
// ...
if notificationsAuthorized {
Button("Continue") {
// show next view ...
}
} else {
AsyncButton("Allow Notifications", state: $viewState) {
try await requestNotificationAuthorization(options: [.alert, .badge, .sound])
}
.environment(\.defaultErrorDescription, "Failed to request notification authorization.")
}
}
.viewStateAlert(state: $viewState)
.task {
notificationsAuthorized = await notificationSettings().authorizationStatus == .authorized
}
}
}
Important
The example above uses the AsyncButton
and the ViewState
model from SpeziViews to more
easily manage the state of asynchronous actions and handle erroneous conditions.
You need to add the SpeziNotifications Swift package to your app in Xcode or Swift package.
This project is licensed under the MIT License. See Licenses for more information.
This project is developed as part of the Stanford Mussallem Center for Biodesign at Stanford University. See CONTRIBUTORS.md for a full list of all SpeziNotifications contributors.