Skip to content

timokoethe/NotificationManager

Repository files navigation

NotificationManager

License: MIT Build Test Swift versions Platforms

NotificationManager is a lightweight Swift package for requesting notification authorization and managing local notifications.

Requirements

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

Installation

Add the package in Xcode using File > Add Package Dependencies and enter:

https://github.com/timokoethe/NotificationManager.git

Add NotificationManager to your target and import it:

import NotificationManager

Authorization

Request the standard alert, sound, and badge permissions at a point where the user understands why notifications are needed:

do {
    let granted = try await NotificationManager.requestAuthorizationThrowing()

    if granted {
        // Notifications are authorized.
    }
} catch {
    // Handle the authorization error.
}

To request custom options:

import UserNotifications

let granted = try await NotificationManager.requestAuthorization(
    for: [.alert, .sound, .badge, .provisional]
)

Read the current authorization status:

let status = await NotificationManager.getAuthorizationStatus()

Scheduling

The asynchronous APIs validate their input and propagate errors from UNUserNotificationCenter.

Schedule a notification after a time interval:

try await NotificationManager.scheduleNotification(
    id: UUID().uuidString,
    title: "Reminder",
    body: "Your task is due.",
    timeInterval: 10
)

Schedule a notification for a date:

let deliveryDate = Date().addingTimeInterval(60)

try await NotificationManager.scheduleNotification(
    id: "task-reminder",
    title: "Reminder",
    body: "Your task is due.",
    triggerDate: deliveryDate
)

Schedule a repeating notification:

try await NotificationManager.scheduleRepeatNotification(
    id: "hourly-reminder",
    title: "Reminder",
    body: "Take a short break.",
    timeInterval: 3_600
)

Repeating notifications require an interval of at least 60 seconds.

For compatibility, synchronous fire-and-forget overloads are also available. They print scheduling errors instead of returning them:

NotificationManager.scheduleNotification(
    id: "task-reminder",
    title: "Reminder",
    body: "Your task is due.",
    timeInterval: 10
)

Error Handling

Input validation uses NotificationManagerError:

do {
    try await NotificationManager.scheduleRepeatNotification(
        id: "reminder",
        title: "Reminder",
        body: "This interval is too short.",
        timeInterval: 30
    )
} catch NotificationManagerError.repeatingTimeIntervalTooShort {
    // Repeating intervals must be at least 60 seconds.
} catch {
    // Handle errors from the notification center.
}

Available validation errors:

  • invalidTimeInterval
  • repeatingTimeIntervalTooShort
  • triggerDateMustBeInFuture

Pending Notifications

Fetch all pending requests:

import UserNotifications

let requests: [UNNotificationRequest] =
    await NotificationManager.getPendingNotificationRequests()

Fetch only their identifiers:

let identifiers = await NotificationManager.getPendingNotificationRequestIDs()

Fetch delivered notifications and their identifiers:

let deliveredNotifications = await NotificationManager.getDeliveredNotifications()
let deliveredIDs = await NotificationManager.getDeliveredNotificationIDs()

Replacing Notifications

Replace a pending notification while keeping its identifier:

try await NotificationManager.replaceNotificationRequestFromId(
    id: "task-reminder",
    newTitle: "Updated reminder",
    newBody: "The task deadline has changed.",
    newDate: Date().addingTimeInterval(300)
)

If no pending request has the supplied identifier, the method returns without making changes.

Removing Notifications

NotificationManager.removePendingNotificationRequests(
    ids: ["task-reminder", "hourly-reminder"]
)

NotificationManager.removeDeliveredNotifications(
    ids: ["task-reminder"]
)

NotificationManager.removeAllPendingNotificationRequests()
NotificationManager.removeAllDeliveredNotificationRequests()

Badge Count

Badge updates are available on iOS 16+, macOS 13+, and visionOS 1+:

try await NotificationManager.setBadge(badge: 3)
try await NotificationManager.resetBadge()

Contributing

Bug reports, feature requests, and pull requests are welcome through the GitHub repository.

License

NotificationManager is available under the MIT License.

About

A Swift package for effortlessly managing local notifications in your app. Schedule, customize, and handle notifications with ease using our API.

Topics

Resources

License

Contributing

Security policy

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages