NotificationManager is a Swift Package to make your code easier for managing local notifications. This package is supposed to make it possible to manage notifications in a highly intuitive way. It shall also appear as minimalistic as possible.
- Xcode 15.0+
- Swift 5.9+
- macOS 10.15+
- iOS 13.0+
- visionOS 1.0+
- Copy the resource url:
https://github.com/timokoethe/NotificationManager.git
- Open your Xcode project.
- Navigate to File / Add Package Dependency.
- Paste the resource url at the top right corner in Search or Enter Package URL.
- Choose the right target under Add to project.
- To complete hit Add Package.
-
Importing the Framework
In any Swift file where you want to use NotificationManager, add the following import statement:import NotificationManager
-
Request notification authorization
Before your app can send notifications, you need to request permission from the user. This is typically done when the app first launches. Add the following code to your App struct or the place wherever you want to ask the user to permit:
import SwiftUI
import UserNotifications
import NotificationManager
@main
struct YourApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.onAppear {
requestNotificationAuthorization()
}
}
}
}
- Scheduling a Notification
Once you have authorization, you can schedule notifications. Here's an example of how to schedule a notification that should arrive after 10 seconds using NotificationManager by pushing a button:
import SwiftUI
import NotificationManager
struct ContentView: View {
var body: some View {
VStack {
Button("Schedule") {
NotificationManager.scheduleNotification(id: UUID().uuidString, title: "Title", body: "Body", triggerDate: Date()+10)
}
}
}
}
- Getting pending notifications
Once you have scheduled one or more notifications you can get all pending:
import SwiftUI
import NotificationManager
struct ContentView: View {
@State private var notifications = [UNNotificationRequest]()
var body: some View {
VStack {
Button("Get") {
Task {
notifications = await NotificationManager.getPendingNotificationRequests()
}
}
}
}
}
- Removing all pending notifications
After scheduling several notifications you can remove them easily:
import SwiftUI
import NotificationManager
struct ContentView: View {
var body: some View {
VStack {
Button("Remove") {
NotificationManager.removeAllPendingNotificationRequests()
}
}
}
}
We welcome contributions from the community to help improve NotificationManager. If you encounter any bugs, have feature requests, or would like to contribute code, please feel free to open an issue or submit a pull request on our GitHub repository.
If you have any questions, feedback, or need assistance with NotificationManager, please don't hesitate to contact us. We're here to help!
NotificationManager is released under the MIT License.
Feel free to adjust and expand upon this template to better suit your project's needs!