Send push notifications with Urban Airship for Vapor.
Update your Package.swift
file.
.Package(url: "https://github.com/nodes-vapor/push-urban-airship.git", majorVersion: 2)
.package(url: "https://github.com/nodes-vapor/push-urban-airship.git", .upToNextMajor(from: "2.0.1"))
targets: [
.target(
name: "App",
dependencies: [
...
"UAPusher"
]
),
...
]
Create config file uapusher.json
with following syntax
{
"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.
Set up the provider:
import UAPusher
try config.addProvider(UAPusher.Provider.self)
let body = try JSON(node: [
"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 sent..")
}
} catch UAError.response(let uaResponse) {
// let response = uaResponse.response[0]
}
The above example will send a text push notification with the message hello world
to all users on the ios
platform.
This package offers a way to easily customize the different segments of the payload sent to Urban Airship, using the UABuilder class.
...
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
...
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 about the push object or check out the full api documentation.
This package is developed and maintained by the Vapor team at Nodes. The package owner for this project is Rasmus.
This package is open-sourced software licensed under the MIT license.