Skip to content

Latest commit

 

History

History
120 lines (103 loc) · 4.32 KB

README.md

File metadata and controls

120 lines (103 loc) · 4.32 KB

NotificationManager

License: MIT Build Test

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.

Requirements

  • Xcode 15.0+
  • Swift 5.9+
  • macOS 10.15+
  • iOS 13.0+
  • visionOS 1.0+

Installation

  1. Copy the resource url:
https://github.com/timokoethe/NotificationManager.git
  1. Open your Xcode project.
  2. Navigate to File / Add Package Dependency.
  3. Paste the resource url at the top right corner in Search or Enter Package URL.
  4. Choose the right target under Add to project.
  5. To complete hit Add Package.

Setup

  1. Importing the Framework
    In any Swift file where you want to use NotificationManager, add the following import statement: import NotificationManager

  2. 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()
                }
        }
    }
}

Usage

  • 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()
            }
        }
    }
}

Contributing

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.

Support

If you have any questions, feedback, or need assistance with NotificationManager, please don't hesitate to contact us. We're here to help!

License

NotificationManager is released under the MIT License.

Feel free to adjust and expand upon this template to better suit your project's needs!