- Notifications
You must be signed in to change notification settings - Fork0
A tiny Swift package that makes it easy to embed strongly-typed values in notifications.
License
samsonjs/NotificationSmuggler
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
NotificationSmuggler is a tiny Swift package that makes it easy to embed strongly-typed values inNotifications, and extract them out on the receiving end as well. Nothing elaborate, it sneaks the contraband in theuserInfo dictionary.
Declare a type conforming toSmuggled and then use the static methodNotification.smuggle(_:object:) when posting the notification. On the receiving side of things you can use the extension methodsNotificationCenter.notifications(for:object:) andNotificationCenter.publisher(for:object:) to observe the strongly-typed values without manually mapping them yourself.
If you haveSendable contraband then all of this will work nicely with Swift 6 and complete concurrency checking.
This package pairs nicely withAsyncMonitor for a complete notification handling system in the Swift 6 concurrency world.
structSomeNotification:Smuggled,Sendable{letanswer:Int}
Your payload doesn't have to be Sendable but if it is then you have more flexibility.
TheSmuggled protocol provides staticnotificationName anduserInfoKey properties for you, should you need them. Generally you don't though.
NotificationCenter.default.smuggle(SomeNotification(answer:42))
or
NotificationCenter.default.post(.smuggle(SomeNotification(answer:42)))
Both automatically set the.name,userInfo, and optionally.object for the notification.
Task{ // This is fine because SomeNotification is SendableforawaitnotificationinNotificationCenter.default.notifications(for:SomeNotification.self){print(notification.answer)}}
The only way to install this package is with Swift Package Manager (SPM). Pleasefile a new issue or submit a pull-request if you want to use something else.
This package is supported on iOS 18.0+ and macOS 15.0+.
When you're integrating this into an app with Xcode then go to your project's Package Dependencies and enter the URLhttps://github.com/samsonjs/NotificationSmuggler and then go through the usual flow for adding packages.
When you're integrating this using SPM on its own then add this to the list of dependencies your Package.swift file:
.package(url:"https://github.com/samsonjs/NotificationSmuggler.git",.upToNextMajor(from:"0.2.1"))
and then add"NotificationSmuggler" to the list of dependencies in your target as well.
Copyright © 2025Sami Samhurisami@samhuri.net. Released under the terms of theMIT License.
About
A tiny Swift package that makes it easy to embed strongly-typed values in notifications.
Resources
License
Uh oh!
There was an error while loading.Please reload this page.