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.

Image for post
Image for post

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

Image for post
Image for post

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

Image for post
Image for post

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

Image for post
Image for post

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.

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.

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.

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.

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.

Image for post
Image for post

Having defined the first two parts, you now need to use your private key to build the third part of your JSON Web Token.

We’re almost done. Keep reading.

Written by

Coding for 35+ years, enjoying using and learning Swift/iOS development. Writer @ Better Programming, @The StartUp, @Mac O’Clock, Level Up Coding & More

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store