NotificationManager is a lightweight Swift package for requesting notification authorization and managing local notifications.
- Swift 5.9+
- Xcode 15.0+
- iOS 13.0+
- macOS 10.15+
- visionOS 1.0+
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 NotificationManagerRequest 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()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
)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:
invalidTimeIntervalrepeatingTimeIntervalTooShorttriggerDateMustBeInFuture
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()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.
NotificationManager.removePendingNotificationRequests(
ids: ["task-reminder", "hourly-reminder"]
)
NotificationManager.removeDeliveredNotifications(
ids: ["task-reminder"]
)
NotificationManager.removeAllPendingNotificationRequests()
NotificationManager.removeAllDeliveredNotificationRequests()Badge updates are available on iOS 16+, macOS 13+, and visionOS 1+:
try await NotificationManager.setBadge(badge: 3)
try await NotificationManager.resetBadge()Bug reports, feature requests, and pull requests are welcome through the GitHub repository.
NotificationManager is available under the MIT License.