平台Swift
iOS-Notification-Framework
突破 iOS 64 条限制地调度本地通知,处理点击路由,并通过 APNs 发送推送
3星标数最新版本: v2.0.0 · 更新时间: 2026年10月15日
支持平台:iOS 17macOS 14watchOS 10visionOS 1
许可证: MIT
NotificationKit 是一个 Swift package,服务于在设备端调度通知的 app,以及向其推送消息的服务器。它用值类型包装了 UserNotifications,并修复了其中会静默失败的部分:iOS 只会为一个 app 保留最近的 64 条待处理通知,其余的会在没有任何错误提示的情况下被丢弃;一些非法值会让 app 崩溃而不是抛出错误;delegate 回调在 Swift 并发中也很难使用。另一个库 NotificationKitAPNs 使用 app 读取时相同的 payload 类型,通过 APNs 发送推送。这两个库除了 Apple 的 SDK 之外都没有依赖。
功能特性
- 超过 64 条也不会丢失通知:NotificationScheduler 把最近的 64 条交给 iOS,其余的保存在一个队列中,等有空位时再依次顶上
- 静默时段和频率上限会按通知实际触发的时间来检查,因此一条在 21:00 设置、原定 23:00 触发的提醒会等到早上才发出
- 报错而不是崩溃:小于 60 秒的重复间隔、NaN 的 relevance score 或第 25 小时,都会在 UNUserNotificationCenter 看到之前抛出 NotificationValidationError
- 点击、action 和深链都作为值来处理:NotificationEventHandler 把 delegate 回调转换成 AsyncStream,DeepLinkRouter 只会从允许的 scheme 和 host 打开界面
- 推送覆盖两端:带 APNs 体积限制的类型化 PushPayload、永不完整打印的 DeviceToken、一个下载媒体的 Notification Service Extension,以及一个使用 token 认证的 APNs 客户端
- InMemoryNotificationCenter 保留了系统的 64 条通知上限,因此调度逻辑可以在普通单元测试中运行
快速开始
swift
1import NotificationKit2import SwiftUI3 4let scheduler = NotificationScheduler() // one for the whole app5 6struct ContentView: View {7 var body: some View {8 Button("Remind me in 10 seconds") {9 Task {10 do {11 try await scheduler.requestAuthorization()12 try await scheduler.schedule(LocalNotification(13 id: "water",14 title: "Time for a glass of water",15 body: "You asked for this reminder 10 seconds ago.",16 trigger: .after(seconds: 10)17 ))18 } catch {19 print("Could not schedule the reminder:", error)20 }21 }22 }23 }24}安装
Swift Package Manager
swift
1dependencies: [2 .package(url: "https://github.com/muhittincamdali/iOS-Notification-Framework.git", from: "2.0.0")3],4targets: [5 .target(name: "YourApp", dependencies: [6 .product(name: "NotificationKit", package: "iOS-Notification-Framework")7 ]),8 // A server or command-line tool that sends pushes:9 .executableTarget(name: "PushServer", dependencies: [10 .product(name: "NotificationKitAPNs", package: "iOS-Notification-Framework")11 ])12]Xcode
text
1https://github.com/muhittincamdali/iOS-Notification-Framework.git截图






时间线
仓库开放,作为一个通知 framework 的原型;早期大部分 commit 都是文档、示例和 CI
v1.0.0 发布;main 分支从一个使用 builder 风格 API 的单一 commit 重新开始
原型阶段的一些小补充:一个 SwiftUI 预览、一个 Live Activity manager,以及一种预测式推送类型
围绕一个可测试的 notification center 重建为 2.0.0;原型源码被移除,未保留任何 1.x API