https://github.com/noppefoxwolf/messagenotification
A Swift package that provides type-safe message passing extensions for NotificationCenter with async/await support
https://github.com/noppefoxwolf/messagenotification
async-await ios macos message-passing notificationcenter swift swift-package type-safety
Last synced: 27 days ago
JSON representation
A Swift package that provides type-safe message passing extensions for NotificationCenter with async/await support
- Host: GitHub
- URL: https://github.com/noppefoxwolf/messagenotification
- Owner: noppefoxwolf
- License: mit
- Created: 2025-07-12T06:28:05.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2025-07-12T12:06:01.000Z (11 months ago)
- Last Synced: 2025-07-12T13:26:20.300Z (11 months ago)
- Topics: async-await, ios, macos, message-passing, notificationcenter, swift, swift-package, type-safety
- Language: Swift
- Size: 34.2 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# MessageNotification
[](https://github.com/noppefoxwolf/MessageNotification/actions/workflows/test.yml)
A Swift package that provides type-safe message passing extensions for NotificationCenter with async/await support.
This is a backport of the iOS 26 NotificationCenter Message extensions for earlier versions.
## Features
- Type-safe message protocol for NotificationCenter
- Async/await observer pattern
- Automatic observation token management
- Cross-platform support (iOS 17+, macOS 14+)
## Installation
Add the package to your `Package.swift`:
```swift
dependencies: [
.package(url: "https://github.com/noppefoxwolf/MessageNotification", from: "1.0.0")
]
```
## Usage
### Define a Message Type
```swift
struct MyMessage: NotificationCenter._AsyncMessage {
typealias Subject = MyViewController
static let name = Notification.Name("MyMessage")
let data: String
static func makeMessage(_ notification: Notification) -> Self? {
guard let data = notification.userInfo?["data"] as? String else { return nil }
return MyMessage(data: data)
}
static func makeNotification(_ message: Self) -> Notification {
return Notification(name: name, userInfo: ["data": message.data])
}
}
```
### Observe Messages
```swift
let token = NotificationCenter.default.addObserver(
of: viewController,
for: MyMessage.self
) { message in
print("Received: \(message.data)")
}
```
### Post Messages
```swift
let message = MyMessage(data: "Hello World")
NotificationCenter.default.post(message, subject: viewController)
```
## Requirements
- iOS 17.0+ / macOS 14.0+
- Swift 6.1+
## License
This project is available under the MIT license.