Notifications, the whole shebang III using only iOS
I should start this article with a disclaimer, it based on iOS 13, Swift 5 and Xcode 11.x. If you reading this and those numbers look dated, be forewarned.
I should also warn you that notifications, principally remote ones involve Apple’s infrastructure which means you will need an Apple Developers account to use them.
Now if you just got here, you should perhaps go back to part I and part II first, if not read on.
Now what we need to do now is install a library to help us create JSON Web Tokens. Apple use JWT to authenticate your connection to their server. The library we’re after was written by IBM. Go to google and search for this string.
swift jwt library
You should come back with this link. Click on the link and you should find yourself within github looking at this repository.

Copy the URL and move back to xCode project. Click on the blue PROJECT icon and than the Swift Packages tab.

Click on the + and paste the name of the IBM-Swift/Swift-JWT package in the dialog box that will come up.

Click next and wait and watch it verify everything. Finally click on the Finish button.

Your project should now include the SwiftJWT library. Go back to the RemoteNotifications class and add the line above your private key and token variables.
import UIKit
import SwiftJWTclass RemoteNotifications: NSObject {private var privateKey = """
-----BEGIN PRIVATE KEY-----
MIGTAgVAMBMGByqGSM49AgEGCCqGSM49AwEHBHkwdwIBAQQgl8Kij2y6acAgp1FZ
BHqI6T/Bv4bBgndxuVr1IfuYhemgBgYIKoZIzj0DAQehRANCAASweAt5jGR5H1Vf
QmlPyVVa2hn8jPLxdg0wHyP/xpXbJ5kGunlkXomLh8k+d31tWKKQF2QTzPCzckyi
p0aHWAWG
-----END PRIVATE KEY-----
"""private var token = "ae4498276c2dfc9ff33a101edc5df3f1e4ef9bbe876f717536e5504d63a38bda"
}
Compile it just check that everything worked, you should get no errors. Don’t worry that was the most nerve racking part, now it should be all plain sailing.
Create the message you plan to post. It needs to be a JSON object. Here is an example.
private var jsonObject: [String: Any] = ["aps":["badge":2,"category":"mycategory","alert":["title":"What, where, who, when, how","body":"You must be kidding"]]]
Of course we’re winging it and this is a single message, the same message we’re post each time, keep going. Now for good measure, to make sure you didn’t miss type anything here, add this code too.
func postNotification() {let valid = JSONSerialization.isValidJSONObject(jsonObject)
print("valid ",valid)
if !valid {
return
}
}
You’re ready to use the IBM library at this point. Define the two of the three parts that make up a JSON Web Token.
func postNotification() {
...let myHeader = Header(typ: "JWT", kid: "DNU7997F")
let myClaims = ClaimsStandardJWT(iss: "*", sub: nil, aud: nil, exp: nil, nbf: nil, iat: Date() , jti: nil)let myJWT = JWT(header: myHeader, claims: myClaims)
}
There are two fields here that need different values. The “kid:” is Key ID of your private Key which we described in part II. The “iss:” attribute us your team ID that you should be able to find under the Apple account membership page.

Having defined the first two parts, you now need to use your private key to build the third part of your JSON Web Token.
func postNotification() {...
let privateKeyAsData = privateKey.data(using: .utf8)
let signer = JWTSigner.es256(privateKey: privateKeyAsData!)
let jwtEncoder = JWTEncoder(jwtSigner: signer)
do {
let jwtString = try jwtEncoder.encodeToString(myJWT)
} catch {
print("failed to encode")
}
}
We’re almost done. Keep reading.