boostcode / Tori-APNS

Tori APNS adds support to Kitura to send easily Apple Push Notification using HTTP/2
21 stars 3 forks source link

Tori APNS

Issues codebeat badge

macOS Linux Swift 2.2 compatible Apache 2

tori-APNS is a simple lib that allows you to send Apple Push Notifications using curl HTTP/2 protocol in linux & macOS, it is compatible starting from swift 3.

Usage

A quick guide, step by step, about how to use this lib.

1- Install libcurl with http/2 support

In macOS using brew you can easily do with:

brew reinstall curl --with-openssl --with-nghttp2
brew link curl --force

2- Add tori-APNS to your project

Add this row to your Package.swift file:

.Package(url: "https://github.com/boostcode/tori-APNS.git", majorVersion: 0, minor: 2)

And then run swift fetch command.

3- Prepare certificates

Create your APNS certificates, then export as P12 file without password then proceed in this way in your shell:

openssl pkcs12 -in path.p12 -out apns-crt.pem -clcerts -nokeys
openssl pkcs12 -in path.p12 -out apns-key.pem -nocerts -nodes

4- Integrate in your app

ToriAPNS is pretty easy to be used, first of all add this line on top of your file:

import ToriAPNS

then instantiate a var to handle pushes:

let push = APNS.init(withCerts: APNSCertificate(certPath: "/path/of/your file/apns-crt.pem",
                                     keyPath: "/path/of/your file/apns-key.pem"))

APNS.init takes a second parameter inSandbox that is true by default, if you switch to false pushes will be sent using production gateway.

Then create a payload for a push message:

let payload = APNSPayload(withText: "Test")

In this new implementation we have 7 parameters that we can manage:

Finally you have to send your push using send passing the payload you created just before and the pushToken of the receiver:

push.send(payload: payload, to: "12345678")

So the overall structure in your app should look pretty similar to:

let push = APNS.init(withCerts: APNSCertificate(certPath: "/path/of/your file/apns-crt.pem",
                                     keyPath: "/path/of/your file/apns-key.pem"))

let payload = APNSPayload(withText: "Test")

push.send(payload: payload, to: "12345678")

And then use again swift build command.