测试已测试 | ✓ |
语言语言 | SwiftSwift |
许可证 | MIT |
发布最后发布 | 2016 年 12 月 |
SwiftSwift 版本 | 2.3 |
SPM支持 SPM | ✗ |
由 Klaas Pieter Annema,Nick Charlton 维护。
用于与 Courier API 集成的 iOS 框架。
使用您应用的 API 令牌和环境实例化Courier对象
let courier = Courier.init(apiToken: "[YOUR_API_TOKEN]", environment: .Development)
对于环境,如果您要向您的应用的开发构建发送通知,请选择 .Development
。如果您要向用分发证书签名的应用(TestFlight、HockeyApp、AppStore 等)发送通知,请使用 .Production
。
为远程通知注册您的应用
let notificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: .None)
application.registerUserNotificationSettings(notificationSettings)
application.registerForRemoteNotifications()
实现: application:didRegisterForRemoteNotificationsWithDeviceToken:
以发送设备令牌并将其订阅到频道
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
courier.subscribeToChannel("[CHANNEL_NAME]", withToken: deviceToken)
}
或者首先注册令牌,然后稍后订阅到频道
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
courier.deviceToken = deviceToken
}
courier.subscribeToChannel("[CHANNEL_NAME]")
Courier 使用基于您的 API 令牌的键将设备令牌存储在 用户默认值 中。只要每个 Courier 实例都使用相同的 API 令牌,就可以在您的应用中使用多个实例,这是安全的。
如果您对订阅的结果感兴趣,可以传递一个可选的完成处理程序
courier.subscribeToChannel("[CHANNEL_NAME]") { result in
switch result {
case .Success: // Handle success
case let .Error(error): // Handle the error
}
}
订阅到频道后,向其广播通知
$ curl -X POST \
-d '{"broadcast": { "payload": { "alert": "Hello From Courier" }}}' \
-H "Content-Type: application/json" \
-H "Authorization: Token token=[YOUR_API_TOKEN]" \
-H "Accept: application/json version=1" \
"https://courier.thoughtbot.com/broadcast/[CHANNEL_NAME]?environment=development"
使用 unsubscribeFromChannel("[CHANNEL_NAME]")
从频道取消订阅广播。
请参阅 CONTRIBUTING 文档。感谢 贡献者!
Courier iOS 版权所有 © 2016 thoughtbot,inc。这是自由软件,可能会根据许可证文件中指定的条款重新分发。
Courier iOS 由 thoughtbot, inc 维护和资助。thoughtbot 的名称和标志是 thoughtbot, inc 的商标。