courier-notifications 0.2.0

courier-notifications 0.2.0

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Dec 2016
SwiftSwift Version 2.3
SPMSupports SPM

Maintained by Klaas Pieter Annema, Nick Charlton.



  • By
  • Nick Charlton, Klaas Pieter Annema and thoughtbot

Courier iOS Build status

iOS framework for integrating with the Courier API.

Installation

Usage

Instantiate a Courier instance with your app’s API token and an environment:

let courier = Courier.init(apiToken: "[YOUR_API_TOKEN]", environment: .Development)

For the environment choose .Development if you’re sending notifications to a development build of your app. If you’re sending notification to an app signed with a distribution certificate (TestFlight, HockeyApp, AppStore, etc) use .Production.

Register your app for remote notifications:

let notificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: .None)
application.registerUserNotificationSettings(notificationSettings)
application.registerForRemoteNotifications()

Send the device token and subscribe it to a channel by implementing: application:didRegisterForRemoteNotificationsWithDeviceToken::

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
  courier.subscribeToChannel("[CHANNEL_NAME]", withToken: deviceToken)
}

Alternatively register a token first, and subscribe to a channel later:

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
  courier.deviceToken = deviceToken
}
courier.subscribeToChannel("[CHANNEL_NAME]")

Courier stores the device token in user defaults using a key based on your API token. As long as each Courier instance is using the same API token, It’s safe to use multiple instances in your app.

If you’re interested in the result of the subscription you can pass an optional completion handler:

courier.subscribeToChannel("[CHANNEL_NAME]") { result in
  switch result {
  case .Success: // Handle success
  case let .Error(error): // Handle the error
  }
}

After subscribing to a channel broadcast a notification to it:

$ 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"

Unsubscribe from broadcasts to a channel using unsubscribeFromChannel("[CHANNEL_NAME]").

Contributing

See the CONTRIBUTING document. Thank you, contributors!

License

Courier iOS is Copyright © 2016 thoughtbot, inc. It is free software, and may be redistributed under the terms specified in the LICENSE file.

About

thoughtbot

Courier iOS is maintained and funded by thoughtbot, inc. The names and logos for thoughtbot are trademarks of thoughtbot, inc.

We love open source software! See our other projects or look at our product case studies and hire us to help build your iOS app.