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
.
A quick guide, step by step, about how to use this lib.
In macOS using brew
you can easily do with:
brew reinstall curl --with-openssl --with-nghttp2
brew link curl --force
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.
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
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:
badge
: take care of the badge countertext
: is the message that will be shown in the pushttl
: is the time to live of the push that is going to be sent (0 is max value)topic
: allows you to tag a push for a type of contentid
: allows you to easily track the pushpriority
: we can manage to send push with max priority.high
or.standard
extra
: you can provide extra fields to the push message using a dictionary of type[String: String]
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.