stanfordspezi/spezinotifications
This source file is part of the SpeziNotifications open source project
Overview
SpeziNotifications simplifies interaction with user notifications by adding additional actions to the Environment of SwiftUI Views and Spezi Modules.
Schedule Notifications
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)
}
}Requesting Authorization in SwiftUI
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
AsyncButtonand theViewStatemodel from SpeziViews to more easily manage the state of asynchronous actions and handle erroneous conditions.
Setup
You need to add the SpeziNotifications Swift package to your app in Xcode or Swift package.
License
This project is licensed under the MIT License. See Licenses for more information.
Contributors
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.
[Stanford Mussallem Center for Biodesign Logo] [Stanford Mussallem Center for Biodesign Logo]
Package Metadata
Repository: stanfordspezi/spezinotifications
Default branch: main
README: README.md