NiceNotifications reimagines local notifications on Apple platforms.
It gives developers a new way to manage notification scheduling, permissions and grouping.
At its most basic form, it helps to schedule local notifications easily, in a declarative way.
At its most advanced, it introduces a whole new way of looking at local notifications, with the concept of “Notification Timelines”, similar to WidgetKit or ClockKit APIs.
WARNING! As of now, NiceNotifications is in early beta. Some APIs is likely to change between releases. Breaking changes are to be expected. Feedback on the API is very welcome!
Click File → Swift Packages → Add Package Dependency.
Enter http://github.com/nicephoton/NiceNotifications.git
Basics Guide
Scheduling a one-off notification
// `NotificationContent` is a subclass of `UNNotificationContent`.
// You can also use `UNNotificationContent` directly
let content = NotificationContent(
title: "Test Notification",
body: "This one is for a README",
sound: .default
)
LocalNotifications.schedule(
content: content,
at: Tomorrow().at(hour: 20, minute: 15),
permissionStrategy: .scheduleIfSystemAllowed
)
What is permissionStrategy?
In most cases, NiceNotifications will handle all the permission stuff for you. So you can feel free to schedule notifications at any time, and permission strategy will take care of permissions.
Basic permission strategies:
askSystemPermissionIfNeeded - if the permission was already given, will proceed to schedule. If the permission was not yet asked, it will ask for system permission, and then proceed if successful. If the permission was rejected previously, it will not proceed.
scheduleIfSystemAllowed - will only proceed to schedule if the permission was already given before. Otherwise, will do nothing.
What is Tomorrow().at( ... )?
NiceNotifications uses DateBuilder to help define notification trigger dates in a simple, clear and easily readable way. Please refer to DateBuilder README for full details.
LocalNotifications.requestPermission(strategy: .askSystemPermissionIfNeeded) { success in
if success {
print("Allowed")
}
}
Getting current system permission status
LocalNotifications.SystemAuthorization.getCurrent { status in
switch status {
case .allowed:
print("allowed")
case .deniedNow:
print("denied")
case .deniedPreviously:
print("denied and needs to enable in settings")
case .undetermined:
print("not asked yet")
}
if status.isAllowed {
print("can schedule!")
}
}
Scheduling directly with UNNotificationRequest
If you just want to use the permission portion of NiceNotifications and create UNNotificationRequest instances yourself, use .directSchedule function:
The most powerful feature of NiceNotifications is timelines within notification groups, which lets you describe your entire local notifications experience in a WidgetKit-like manner.
Case study: “Daily Quote” notifications
Let’s say we have an app that shows a different quote from a list every morning. The user can also disable / enable certain quotes, or add their own.
For that, we need to define a new class that implements LocalNotificationsGroup protocol:
public protocol LocalNotificationsGroup {
var groupIdentifier: String { get }
func getTimeline(completion: @escaping (NotificationsTimeline) -> ())
}
Groups not only allow you to have clear logical separation between different experiences, but to also have user permission on a per group basis (we’ll get to that later).
Let’s implement our DailyQuoteGroup:
final class DailyQuoteGroup: LocalNotificationsGroup {
let groupIdentifier: String = "dailyQuote"
func getTimeline(completion: @escaping (NotificationsTimeline) -> ()) {
let timeline = NotificationsTimeline {
EveryDay(forDays: 50, starting: .today)
.at(hour: 9, minute: 00)
.schedule(title: "Storms make oaks take deeper root.")
}
completion(timeline)
}
}
But this will only give us 50 identical quotes for the next 50 days. Let’s make it more interesting by giving a user an actual random quote each day:
final class DailyQuoteGroup: LocalNotificationsGroup {
let groupIdentifier: String = "dailyQuote"
func getTimeline(completion: @escaping (NotificationsTimeline) -> ()) {
let timeline = NotificationsTimeline {
EveryDay(forDays: 50, starting: .today)
.at(hour: 9, minute: 00)
.schedule(with: makeRandomQuoteContent)
}
completion(timeline)
}
private func makeRandomQuoteContent() -> NotificationContent? {
guard let randomQuote = QuoteStore.enabledQuotes.randomElement() else {
return nil
}
return NotificationContent(
title: randomQuote,
body: "Tap here for more daily inspiration"
)
}
}
Looks great! Every time makeRandomQuoteContent gets invoked, we’ll get a different quote, which is exactly what we want.
Okay, so what do we do with it now?
“Rescheduling” notification groups
Scheduling notification groups is easy:
LocalNotifications.reschedule(
group: DailyQuoteGroup(),
permissionStrategy: .askSystemPermissionIfNeeded
) // completion is optional
Why is it called “reschedule”? Because every time we inkove this function with the same group, the whole timeline will be cleaned and recreated.
Why is it useful? First of all, let’s say that the user has disabled one of the quotes from showing up. But it might’ve been already scheduled! Not a problem: we’ll simply call reschedule again, and it will no longer show up:
Since DailyQuoteGroup uses QuoteStore.enabledQuotes to generate a random quote, newly rescheduled group will not have a disabled quote anymore!
Secondly, you’ve noticed that we’ve only scheduled for 50 days since “today”. This is because we cannot use system recurring notifications (since that only allows us to have the same content for each notification), and iOS only allows us no more than 64 scheduled notifications at once.
So yes, it will require us to periodically reschedule the group to “reset” the 50 days. One of the best places for that is applicationDidFinishLaunchingWithOptions:
Alternatively, you can schedule background execution tasks to periodically refresh notifications.
Group-level permissions
Permission Strategy has two different levels:
System level: basically if user allowed the app to send notifications. This is the regular “App X wants to send notifications” permission.
Group level: this relates to whether the user has enabled a certain group. For example, user can opt in to receive a quote every evening, but not receive one in the morning.
Here’s how to make your own custom permission strategy:
Will ask user’s permission before proceeding to system level check, and will save that decission. Will only ask permission if the permission was not given before, otherwise will proceed straight to system level check.
AskPermissionMode:
.once: will only ask for permission once. If the user has denied this group, any subsequent call will not ask for permission, and will not schedule notifications
.alwaysIfNotAllowed: will always ask for permission if it was not already given
PermissionAsker:
This class is responsible for asking group-level permission. You can use .defaultAlert(on:) to show a pre-made alert (English only), use .alert(on:title:message:noActionTitle:yesActionTitle:), or create your own:
let permissionAsker = LocalNotifications.ApplicationLevelPermissionAsker { (completion) in
// ask permission, then call completion with Result<Bool, Error>
}
.ifAlreadyAllowed:
Will proceed to system level check only if the category was allowed before
.ifAllowed(other:):
Will proceed to system level check only if the other specified category is allowed
System Level
.askPermission:
Will ask system notification permission if neccessary
.ifAlreadyAllowed:
Will only proceed to schedule notifications if already allowed by the system; otherwise will not proceed
Notification Permission Switch
For UIKit, NiceNotifications provides NotificationsPermissionSwitch, a custom UIView that shows and allows to modify group-level permission for a notification group
let toggle = NotificationsPermissionSwitch(group: DailyQuoteGroup())
toggle.onEnabled = { _ in ... }
toggle.onDisabled = { _ in ... }
toggle.onDeniedBySystem = { _ in /* show "Open Settings" alert to user */ }
This saves you a lot of complexity that you usually need to implement yourself.
In case you want to show your own pre-permission when user tries to enable the category, you can use .permissionAsker property:
// make sure to not introduce retain cycles here
toggle.permissionAsker = { .defaultAlert(on: viewController) }
If you want to use any other control instead of a system UISwitch, you can write your own adapter for NotificationPermissionView. For reference, see __UISwitchAdapter in NotificationsPermissionView.swift.
Disabling a notification group
Disabling a group will remove all pending notifications, as well as prevent new reschedulings until the permission is given again:
let status = LocalNotifications.GroupLevelAuthorization.getCurrent(forGroup: DailyQuoteGroup().groupIdentifier)
switch status {
case .allowed: /* ... */
case .denied: /* ... */
case .notAsked: /* ... */
}
Performance Improvements
1. Generating content asynchronously
NotificationsTimeline allows content to be created asynchronously, using one of available schedule(with:) overloads:
final class DailyQuoteGroup: LocalNotificationsGroup {
let groupIdentifier: String = "dailyQuote"
func getTimeline(completion: @escaping (NotificationsTimeline) -> ()) {
let timeline = NotificationsTimeline {
EveryDay(forDays: 50, starting: .today)
.at(hour: 9, minute: 00)
.schedule(with: makeRandomQuoteContent(completion:))
}
completion(timeline)
}
private func makeRandomQuoteContent(completion: @escaping (NotificationContent) -> ()) {
QuoteStore.fetchRandom { (quote) in
let content = NotificationContent(
title: quote,
body: "Open app for more quotes",
sound: .default
)
completion(content)
}
}
}
By default, getTimeline will always be called on a main thread. If your app logic allows getTimeline to be called on a background queue, set preferredExecutionContext to .canRunOnAnyQueue:
final class DailyQuoteGroup: LocalNotificationsGroup {
let groupIdentifier: String = "dailyQuote"
var preferredExecutionContext: LocalNotificationsGroupContextPreference {
return .canRunOnAnyQueue
}
func getTimeline(completion: @escaping (NotificationsTimeline) -> ()) {
...
}
}
NiceNotifications
NiceNotifications reimagines local notifications on Apple platforms.
It gives developers a new way to manage notification scheduling, permissions and grouping.
At its most basic form, it helps to schedule local notifications easily, in a declarative way.
At its most advanced, it introduces a whole new way of looking at local notifications, with the concept of “Notification Timelines”, similar to
WidgetKit
orClockKit
APIs.Author & maintainer: @dreymonde
Showcase
Installation
Swift Package Manager
http://github.com/nicephoton/NiceNotifications.git
Basics Guide
Scheduling a one-off notification
What is
permissionStrategy
?In most cases, NiceNotifications will handle all the permission stuff for you. So you can feel free to schedule notifications at any time, and permission strategy will take care of permissions.
Basic permission strategies:
askSystemPermissionIfNeeded
- if the permission was already given, will proceed to schedule. If the permission was not yet asked, it will ask for system permission, and then proceed if successful. If the permission was rejected previously, it will not proceed.scheduleIfSystemAllowed
- will only proceed to schedule if the permission was already given before. Otherwise, will do nothing.What is
Tomorrow().at( ... )
?NiceNotifications uses DateBuilder to help define notification trigger dates in a simple, clear and easily readable way. Please refer to DateBuilder README for full details.
Here’s a short reference:
Scheduling multiple notifications
Scheduling recurring notifications
For recurring content based on date:
Cancelling notification groups
Asking permission without scheduling
Getting current system permission status
Scheduling directly with
UNNotificationRequest
If you just want to use the permission portion of NiceNotifications and create
UNNotificationRequest
instances yourself, use.directSchedule
function:Advanced Guide
Notification Timelines
The most powerful feature of NiceNotifications is timelines within notification groups, which lets you describe your entire local notifications experience in a WidgetKit-like manner.
Case study: “Daily Quote” notifications
Let’s say we have an app that shows a different quote from a list every morning. The user can also disable / enable certain quotes, or add their own.
For that, we need to define a new class that implements
LocalNotificationsGroup
protocol:Groups not only allow you to have clear logical separation between different experiences, but to also have user permission on a per group basis (we’ll get to that later).
Let’s implement our
DailyQuoteGroup
:But this will only give us 50 identical quotes for the next 50 days. Let’s make it more interesting by giving a user an actual random quote each day:
Looks great! Every time
makeRandomQuoteContent
gets invoked, we’ll get a different quote, which is exactly what we want.Okay, so what do we do with it now?
“Rescheduling” notification groups
Scheduling notification groups is easy:
Why is it called “reschedule”? Because every time we inkove this function with the same group, the whole timeline will be cleaned and recreated.
Why is it useful? First of all, let’s say that the user has disabled one of the quotes from showing up. But it might’ve been already scheduled! Not a problem: we’ll simply call reschedule again, and it will no longer show up:
Since
DailyQuoteGroup
usesQuoteStore.enabledQuotes
to generate a random quote, newly rescheduled group will not have a disabled quote anymore!Secondly, you’ve noticed that we’ve only scheduled for 50 days since “today”. This is because we cannot use system recurring notifications (since that only allows us to have the same content for each notification), and iOS only allows us no more than 64 scheduled notifications at once.
So yes, it will require us to periodically reschedule the group to “reset” the 50 days. One of the best places for that is
applicationDidFinishLaunchingWithOptions
:Alternatively, you can schedule background execution tasks to periodically refresh notifications.
Group-level permissions
Permission Strategy has two different levels:
Here’s how to make your own custom permission strategy:
Permission strategy will always execute group level strategy first, and if succesfull, will proceed to system level.
Group Level
.bypass
:Will skip group level permission check and go straight to system level. This will not change existing group-level permission, if present.
.allowAutomatically
:Will enable permission on a group level and will save that decision, and will then proceed to system level check.
If the user previously disabled / denied this group permission,
.allowAutomatically
will overwrite that decision..askPermission(AskPermissionMode, PermissionAsker)
:Will ask user’s permission before proceeding to system level check, and will save that decission. Will only ask permission if the permission was not given before, otherwise will proceed straight to system level check.
AskPermissionMode:
.once
: will only ask for permission once. If the user has denied this group, any subsequent call will not ask for permission, and will not schedule notifications.alwaysIfNotAllowed
: will always ask for permission if it was not already givenPermissionAsker:
This class is responsible for asking group-level permission. You can use
.defaultAlert(on:)
to show a pre-made alert (English only), use.alert(on:title:message:noActionTitle:yesActionTitle:)
, or create your own:.ifAlreadyAllowed
:Will proceed to system level check only if the category was allowed before
.ifAllowed(other:)
:Will proceed to system level check only if the other specified category is allowed
System Level
.askPermission
:Will ask system notification permission if neccessary
.ifAlreadyAllowed
:Will only proceed to schedule notifications if already allowed by the system; otherwise will not proceed
Notification Permission Switch
For
UIKit
, NiceNotifications providesNotificationsPermissionSwitch
, a customUIView
that shows and allows to modify group-level permission for a notification groupThis saves you a lot of complexity that you usually need to implement yourself.
In case you want to show your own pre-permission when user tries to enable the category, you can use
.permissionAsker
property:If you want to use any other control instead of a system
UISwitch
, you can write your own adapter forNotificationPermissionView
. For reference, see__UISwitchAdapter
inNotificationsPermissionView.swift
.Disabling a notification group
Disabling a group will remove all pending notifications, as well as prevent new reschedulings until the permission is given again:
Getting group-level authorization information
Performance Improvements
1. Generating content asynchronously
NotificationsTimeline
allows content to be created asynchronously, using one of availableschedule(with:)
overloads:Other available
.schedule
overloads:2. Creating timeline on background queue
By default,
getTimeline
will always be called on a main thread. If your app logic allowsgetTimeline
to be called on a background queue, setpreferredExecutionContext
to.canRunOnAnyQueue
:Apps that use NiceNotifications
Acknowledgments
Special thanks to: