Skip to content

Repository files navigation

NotificationManager

License: MITBuildTestSwift versionsPlatforms

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{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()

Scheduling

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)

Error Handling

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:

  • invalidTimeInterval
  • repeatingTimeIntervalTooShort
  • triggerDateMustBeInFuture

Pending Notifications

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

Replacing Notifications

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.

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+:

if #available(iOS 16.0, macOS 13.0, visionOS 1.0,*){tryawaitNotificationManager.setBadge(badge:3)tryawaitNotificationManager.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

Contributing

Security policy

Stars

7 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages