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{letgranted=tryawaitNotificationManager.requestAuthorizationThrowing()if granted {
// Notifications are authorized.
}}catch{
// Handle the authorization error.
}To request custom options:
import UserNotifications
letgranted=tryawaitNotificationManager.requestAuthorization(
for:[.alert,.sound,.badge,.provisional])Read the current authorization status:
letstatus=awaitNotificationManager.getAuthorizationStatus()The asynchronous APIs validate their input and propagate errors from UNUserNotificationCenter.
Schedule a notification after a time interval:
tryawaitNotificationManager.scheduleNotification(
id:UUID().uuidString,
title:"Reminder",
body:"Your task is due.",
timeInterval:10)Schedule a notification for a date:
letdeliveryDate=Date().addingTimeInterval(60)tryawaitNotificationManager.scheduleNotification(
id:"task-reminder",
title:"Reminder",
body:"Your task is due.",
triggerDate: deliveryDate
)Schedule a repeating notification:
tryawaitNotificationManager.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{tryawaitNotificationManager.scheduleRepeatNotification(
id:"reminder",
title:"Reminder",
body:"This interval is too short.",
timeInterval:30)}catchNotificationManagerError.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
letrequests:[UNNotificationRequest]=awaitNotificationManager.getPendingNotificationRequests()Fetch only their identifiers:
letidentifiers=awaitNotificationManager.getPendingNotificationRequestIDs()Fetch delivered notifications and their identifiers:
letdeliveredNotifications=awaitNotificationManager.getDeliveredNotifications()letdeliveredIDs=awaitNotificationManager.getDeliveredNotificationIDs()Replace a pending notification while keeping its identifier:
tryawaitNotificationManager.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+:
if #available(iOS 16.0, macOS 13.0, visionOS 1.0,*){tryawaitNotificationManager.setBadge(badge:3)tryawaitNotificationManager.resetBadge()}Bug reports, feature requests, and pull requests are welcome through the GitHub repository.
NotificationManager is available under the MIT License.