Swiftpack.co - Package - kylebrowning/APNSwift

sswg:sandbox|94x20 License Build Swift

APNSwift

A non-blocking Swift module for sending remote Apple Push Notification requests to APNS built on http/2, SwiftNIO for use on server side swift platforms.

Installation

To install APNSwift, just add the package as a dependency in your Package.swift.

dependencies: [
    .package(url: "https://github.com/kylebrowning/APNSwift.git", .upToNextMinor(from: "1.3.0"))
]

Getting Started

let signer = try! APNSwiftSigner(filePath: "/Users/kylebrowning/Downloads/AuthKey_9UC9ZLQ8YW.p8")

let apnsConfig = APNSwiftConfiguration(keyIdentifier: "9UC9ZLQ8YW",
                                       teamIdentifier: "ABBM6U9RM5",
                                       signer: signer,
                                       topic: "com.grasscove.Fern",
                                       environment: .sandbox)

struct BasicNotification: APNSwiftNotification {
    var aps: APNSwiftPayload
}
let apns = try APNSwiftConnection.connect(configuration: apnsConfig, on: group.next()).wait()
let alert = APNSwiftPayload.APNSwiftAlert(title: "Hey There", subtitle: "Full moon sighting", body: "There was a full moon last night did you see it")
let aps = APNSwiftPayload(alert: alert, badge: 1, sound: .normal("cow.wav"))
let notification = BasicNotification(aps: aps)
let res = apns.send(notification, pushType: .alert, to: "de1d666223de85db0186f654852cc960551125ee841ca044fdf5ef6a4756a77e")
try apns.close().wait()
try group.syncShutdownGracefully()
exit(0)

APNSwiftConfiguration

APNSwiftConfiguration is a structure that provides the system with common configuration.

public struct APNSwiftConfiguration {
    public var keyIdentifier: String
    public var teamIdentifier: String
    public var signer: APNSwiftSigner
    public var topic: String
    public var environment: Environment
    public var tlsConfiguration: TLSConfiguration

    public var url: URL {
        switch environment {
        case .production:
            return URL(string: "https://api.push.apple.com")!
        case .sandbox:
            return URL(string: "https://api.development.push.apple.com")!
        }
    }

Example APNSwiftConfiguration

let signer = ...
let apnsConfig = try APNSwiftConfiguration(keyIdentifier: "9UC9ZLQ8YW",
                                   teamIdentifier: "ABBM6U9RM5",
                                   signer: signer),
                                   topic: "com.grasscove.Fern",
                                   environment: .sandbox)

Signer

APNSwiftSigner provides a structure to sign the payloads with. This should be loaded into memory at the configuration level. It requires the data to be in a ByteBuffer format.

let url = URL(fileURLWithPath: "/Users/kylebrowning/Downloads/AuthKey_9UC9ZLQ8YW.p8")
let data: Data
do {
    data = try Data(contentsOf: url)
} catch {
    throw APNSwiftError.SigningError.certificateFileDoesNotExist
}
var byteBuffer = ByteBufferAllocator().buffer(capacity: data.count)
byteBuffer.writeBytes(data)
let signer = try! APNSwiftSigner.init(buffer: byteBuffer)

APNSwiftConnection

APNSwiftConnection is a class with methods thats provides a wrapper to NIO's ClientBootstrap. The swift-nio-http2 dependency is utilized here. It also provides a function to send a notification to a specific device token string.

Example APNSwiftConnection

let apnsConfig = ...
let apns = try APNSwiftConnection.connect(configuration: apnsConfig, on: group.next()).wait()

Alert

Alert is the actual meta data of the push notification alert someone wishes to send. More details on the specifics of each property are provided here. They follow a 1-1 naming scheme listed in Apple's documentation

Example Alert

let alert = Alert(title: "Hey There", subtitle: "Full moon sighting", body: "There was a full moon last night did you see it")

APNSwiftPayload

APNSwiftPayload is the meta data of the push notification. Things like the alert, badge count. More details on the specifics of each property are provided here. They follow a 1-1 naming scheme listed in Apple's documentation

Example APNSwiftPayload

let alert = ...
let aps = APNSwiftPayload(alert: alert, badge: 1, sound: .normal("cow.wav"))

Putting it all together

let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
var verbose = true

let signer = try! APNSwiftSigner(filePath: "/Users/kylebrowning/Desktop/AuthKey_9UC9ZLQ8YW.p8")

let apnsConfig = APNSwiftConfiguration(keyIdentifier: "9UC9ZLQ8YW",
                                       teamIdentifier: "ABBM6U9RM5",
                                       signer: signer,
                                       topic: "com.grasscove.Fern",
                                       environment: .sandbox)

let apns = try APNSwiftConnection.connect(configuration: apnsConfig, on: group.next()).wait()

if verbose {
    print("* Connected to \(apnsConfig.url.host!) (\(apns.channel.remoteAddress!)")
}

struct AcmeNotification: APNSwiftNotification {
    let acme2: [String]
    let aps: APNSwiftPayload

    init(acme2: [String], aps: APNSwiftPayload) {
        self.acme2 = acme2
        self.aps = aps
    }
}

let alert = APNSwiftPayload.APNSwiftAlert(title: "Hey There", subtitle: "Subtitle", body: "Body")
let apsSound = APNSwiftPayload.APNSSoundDictionary(isCritical: true, name: "cow.wav", volume: 0.8)
let aps = APNSwiftPayload(alert: alert, badge: 0, sound: .critical(apsSound), hasContentAvailable: true)
let temp = try! JSONEncoder().encode(aps)
let string = String(bytes: temp, encoding: .utf8)
let notification = AcmeNotification(acme2: ["bang", "whiz"], aps: aps)

do {
    let expiry = Date().addingTimeInterval(5)
    for _ in 1...5 {
        try apns.send(notification, pushType: .alert, to: "98AAD4A2398DDC58595F02FA307DF9A15C18B6111D1B806949549085A8E6A55D", expiration: expiry, priority: 10).wait()
        try apns.send(notification, pushType: .alert, to: "98AAD4A2398DDC58595F02FA307DF9A15C18B6111D1B806949549085A8E6A55D", expiration: expiry, priority: 10).wait()
        try apns.send(notification, pushType: .alert, to: "98AAD4A2398DDC58595F02FA307DF9A15C18B6111D1B806949549085A8E6A55D", expiration: expiry, priority: 10).wait()
        try apns.send(notification, pushType: .alert, to: "98AAD4A2398DDC58595F02FA307DF9A15C18B6111D1B806949549085A8E6A55D", expiration: expiry, priority: 10).wait()
    }
} catch {
    print(error)
}

try apns.close().wait()
try group.syncShutdownGracefully()
exit(0)

Custom Notification Data

Apple provides engineers with the ability to add custom payload data to each notification. In order to facilitate this we have the APNSwiftNotification.

Example

struct AcmeNotification: APNSwiftNotification {
    let acme2: [String]
    let aps: APNSwiftPayload

    init(acme2: [String], aps: APNSwiftPayload) {
        self.acme2 = acme2
        self.aps = aps
    }
}

let apns: APNSwiftConnection: = ...
let aps: APNSwiftPayload = ...
let notification = AcmeNotification(acme2: ["bang", "whiz"], aps: aps)
let res = try apns.send(notification, to: "de1d666223de85db0186f654852cc960551125ee841ca044fdf5ef6a4756a77e").wait()

Using PEM instead of P8

var apnsConfig = try APNSwiftConfiguration(keyIdentifier: "9UC9ZLQ8YW",
                                       teamIdentifier: "ABBM6U9RM5",
                                       signer: APNSwiftSigner.init(buffer: ByteBufferAllocator().buffer(capacity: Data().count)),
                                       topic: "com.grasscove.Fern",
                                       environment: .sandbox)

let key = try NIOSSLPrivateKey(file: "/Users/kylebrowning/Projects/swift/Fern/development_com.grasscove.Fern.pkey", format: .pem)
apnsConfig.tlsConfiguration.privateKey = NIOSSLPrivateKeySource.privateKey(key)
apnsConfig.tlsConfiguration.certificateVerification = .noHostnameVerification
apnsConfig.tlsConfiguration.certificateChain = try! [.certificate(.init(file: "/Users/kylebrowning/Projects/swift/Fern/development_com.grasscove.Fern.pem", format: .pem))]

Need a completely custom arbtirary payload and dont like being typecast?

APNSwift provides the ability to send rawBytes ByteBuffer as a payload. This is to be used with caution. APNSwift cannot gurantee delivery if you do not have the correct payload. For more information see: Creating APN Payload

let notificationJsonPayload = ...
let data: Data = try! encoder.encode(notificationJsonPayload)
var buffer = ByteBufferAllocator().buffer(capacity: data.count)
buffer.writeBytes(data)
try apns.send(rawBytes: buffer, pushType: .alert, to: "<DEVICETOKEN>")

Original pitch and discussion on API

Github

link
Stars: 199
Help us keep the lights on

Used By

Total: 0

Releases

1.3.0 - Oct 4, 2019

The previous release violated server in that it added a public func but only incremented the patch.

This release does not violate semver

1.2.1 - Oct 3, 2019

A small but fun update. If you'd like to send your own raw payload and not use much of the Typecasting that APNSwift offers, you now can. Checkout the bottom of the README for more information.

  • Fixes #50 (#51)

1.2.0 - Sep 25, 2019

Official 1.2.0 release

#49 - tooManyProviderTokens #48 - Correctly generate a signature #39 - Flaky Test #43 - Support apn-push-type #47 - Support PEM certificate #44 - Support Linux OpenSSL 5.1

1.1.1 - Aug 13, 2019

#37 Fix ES256 signing

1.1.0 - Jul 27, 2019

  • thanks to #35 We now support token refresh.
  • Also includes unit tests.