Skip to content
This repository has been archived by the owner on Apr 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #12 from nodes-vapor/feature/chaining-methods
Browse files Browse the repository at this point in the history
Feature/chaining methods
  • Loading branch information
rasmusebbesen authored Jan 3, 2018
2 parents d45bbc6 + 1f83ac4 commit 3fca521
Show file tree
Hide file tree
Showing 23 changed files with 857 additions and 41 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ Config/secrets/
node_modules/
bower_components/
.swift-version
CMakeLists.txt
CMakeLists.txt
Package.resolved
96 changes: 60 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# UAPusher

[![Swift Version](https://img.shields.io/badge/Swift-3-brightgreen.svg)](http://swift.org)
[![Vapor Version](https://img.shields.io/badge/Vapor-2-F6CBCA.svg)](http://vapor.codes)
[![Circle CI](https://circleci.com/gh/nodes-vapor/push-urban-airship/tree/master.svg?style=shield)](https://circleci.com/gh/nodes-vapor/push-urban-airship)
Expand All @@ -7,41 +8,36 @@
[![Readme Score](http://readme-score-api.herokuapp.com/score.svg?url=https://github.com/nodes-vapor/push-urban-airship)](http://clayallsopp.github.io/readme-score?url=https://github.com/nodes-vapor/push-urban-airship)
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/nodes-vapor/push-urban-airship/master/LICENSE)


Send push notifications with Urban Airship for Vapor.


## 📦 Installation

Update your `Package.swift` file.
```swift
.Package(url: "https://github.com/nodes-vapor/push-urban-airship.git", majorVersion: 1)
```

### Config

Create config file `uapusher.json` with following syntax

```json
{
"applicationGroups": {
"defaultGroup": {
"development": {
"appKey": "yyyy",
"masterSecret": "yyyy"
},
"staging": {
"appKey": "yyyy",
"masterSecret": "yyyy"
}
}
}
"applicationGroups": {
"defaultGroup": {
"development": {
"appKey": "yyyy",
"masterSecret": "yyyy"
},
"staging": {
"appKey": "yyyy",
"masterSecret": "yyyy"
}
}
}
}
```

You can define multiple apps like in the example. Else just delete one of groups.


## Getting started 🚀
Set up the provider:

Expand All @@ -51,39 +47,67 @@ import UAPusher
try config.addProvider(UAPusher.Provider.self)
```

### Example

### Simple example
```swift
let body = try JSON(node: [
"audience": "all",
"device_types": [
"ios",
"android"
],
"notification": [
"alert": "hello world"
]
"audience": "all",
"device_types": [
"ios"
],
"notification": [
"alert": "hello world"
]
])

let request = UARequest(body: body)

do {
let response = try drop.uapusher?.send(request: request)
if response.status == .accepted {
print("Push send..")
}
let response = try drop.uapusher?.send(request: request)
if response.status == .accepted {
print("Push sent..")
}
} catch UAError.response(let uaResponse) {
//let response = uaResponse.response[0]
// let response = uaResponse.response[0]
}
```
Check out the api documentation (http://docs.urbanairship.com/api/ua/).

The above example will send a text push notification with the message `hello world` to all users on the `ios` platform

## 🏆 Credits
### Chain your payload
This package offers a way to easily customize the different segments of the payload sent to Urban Airship, using the UABuilder class.

```swift
...
let payload: JSON = try UABuilder()
.add(Audience(.all)
.add(Notification(.alert(value:"this is a test")))
.add(DeviceTypes(.android))
.payload()

let request: UARequest = UARequest(body: payload)
...
```

You can also provide all segments in a list
```swift
...
let payload: JSON = try UABuilder().add([
Audience(.all),
DeviceTypes(.android),
Notification(.alert(value:"this is a test"))
]).payload()

let request: UARequest = UARequest(body: payload)
...
```

The above examples will define a text push notification with the message `this is a test` to all users.

UABuilder currently lets you set `audience`, `campaigns`, `device_type`, `ìn_app`, `message` and `notification`. Method overloads allow you to set the payload segments directly using custom JSON or using a preset value. For more information see the [Urban Airship documentation](https://docs.urbanairship.com/api/ua/#push-object) about the push object or check out the [full api documentation](http://docs.urbanairship.com/api/ua/)

## 🏆 Credits
This package is developed and maintained by the Vapor team at [Nodes](https://www.nodesagency.com).
The package owner for this project is [Rasmus](https://github.com/rasmusebbesen).


## 📄 License

This package is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT).
63 changes: 63 additions & 0 deletions Sources/UAPusher/Builder/Audience.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import JSON

/// Audience segment of an Urban Airship push notification payload
/// See: https://docs.urbanairship.com/api/ua/#audience-selectors
public struct Audience: Segment {

// MARK: Class fields

public let key: String = "audience"
public let payload: JSON

// MARK: Selector values

public enum Selector: JSONRepresentable {
case all
case tag(value: String)
case tags(values: [String])
case namedUser(value: String)
case alias(value: String)

public func makeJSON() throws -> JSON {
switch self {
case .all:
return "all"
case .tag(let value):
var json: JSON = JSON()
try json.set("tag", value)
return json
case .tags(let values):
var json: JSON = JSON()
try json.set("tag", values)
return json
case .namedUser(let value):
var json: JSON = JSON()
try json.set("named_user", value)
return json
case .alias(let value):
print("\"alias\" is deprecated. Use \"named_user\" instead")
print("See https://docs.urbanairship.com/api/ua/#data-formats-1 for more info")
var json: JSON = JSON()
try json.set("named_user", value)
return json
}
}
}

// MARK: Initializers

/// Init from JSON
///
/// - Parameter payload: JSON
public init(payload: JSON) {
self.payload = payload
}

/// Init from Selector
///
/// - Parameter selector: Selector
public init(_ selector: Selector) throws {
try self.payload = selector.makeJSON()
}

}
44 changes: 44 additions & 0 deletions Sources/UAPusher/Builder/Campaigns.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

import JSON

/// Campaigns segment of an Urban Airship push notification payload
/// See: https://docs.urbanairship.com/api/ua/#campaigns-object
public struct Campaigns: Segment {

// MARK: Class fields

public let key: String = "campaigns"
public let payload: JSON

// MARK: Selector values

public enum Selector: JSONRepresentable {
case categories(values: [String])

public func makeJSON() throws -> JSON {
switch self {
case .categories(let values):
var json: JSON = JSON()
try json.set("categories", values)
return json
}
}
}

// MARK: Initializers

/// Init from JSON
///
/// - Parameter payload: JSON
public init(payload: JSON) {
self.payload = payload
}

/// Init from Selector
///
/// - Parameter selector: Selector
public init(_ selector: Selector) throws {
try self.payload = selector.makeJSON()
}

}
71 changes: 71 additions & 0 deletions Sources/UAPusher/Builder/DeviceTypes.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import JSON

/// Device type segment of an Urban Airship push notification payload
/// See: https://docs.urbanairship.com/integration/#device-types
public struct DeviceTypes: Segment {

// MARK: Class fields

public let key: String = "device_types"
public let payload: JSON

// MARK: Selector values

public enum Selector: JSONRepresentable {
case all
case ios
case android
case windows
case web

public func makeJSON() -> JSON {
switch self {
case .all:
return "all"
case .ios:
return "ios"
case .android:
return "android"
case .windows:
return "wns"
case .web:
return "web"
}
}
}

// MARK: Initializers

/// Init from JSON
///
/// - Parameter payload: JSON
public init(payload: JSON) {
self.payload = payload
}

/// Init from Selector
///
/// - Parameter selector: Selector
public init(_ selector: Selector) throws {
guard selector == .all else {
self.init([selector])
return
}

self.init(payload: selector.makeJSON())
}

/// Init from list of predefined values
///
/// - Parameter selectors: [Selector]
public init(_ selectors: [Selector]) {
var compoundJson: [JSON] = []

for selector in selectors {
compoundJson.append(selector.makeJSON())
}

self.init(payload: JSON(compoundJson))
}

}
21 changes: 21 additions & 0 deletions Sources/UAPusher/Builder/InApp.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

import JSON

/// In-app segment of an Urban Airship push notification payload
/// See: https://docs.urbanairship.com/api/ua/#api-in-app-object
public struct InApp: Segment {
// MARK: Class fields

public let key: String = "in_app"
public let payload: JSON

// MARK: Initializers

/// Init from JSON
///
/// - Parameter payload: JSON
public init(payload: JSON) {
self.payload = payload
}

}
24 changes: 24 additions & 0 deletions Sources/UAPusher/Builder/Message.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

import JSON

/// Message (rich push) segment of an Urban Airship push notification payload
/// See: https://docs.urbanairship.com/api/ua/#rich-push-api
/// NB: This requires a special plan at UA.
/// See: https://www.urbanairship.com/products/mobile-app-engagement/pricing
public struct Message: Segment {

// MARK: Class fields

public let key: String = "message"
public let payload: JSON

// MARK: Initializers

/// Init from JSON
///
/// - Parameter payload: JSON
public init(payload: JSON) {
self.payload = payload
}

}
Loading

0 comments on commit 3fca521

Please sign in to comment.