Notifications, the whole shebang II 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, read it first; if not read on.

So local notifications are good, and have their uses. But I suspect that what most apps really need/want is remote notifications. These are notifications that are fired via a server or in maybe another iOS device.

How do they work. Firstly you need to get permission to show notifications, if your not sure how to; you need to read part I. The first step for remote notifications is to get the ID of the device your running on. You need this to tell Apple where it needs to send a remote notifications. You do do with this code.

You recall from part I we had an App we called Noob. Reopen it and add this method to the appDelegate.swift.

func application( _ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let tokenParts = deviceToken.map { data in String(format: "%02.2hhx", data) }
let token = tokenParts.joined()
print("Device Token: \n\(token)\n")
}

This will produce a 64 character string. But be warned, if you delete and re-install your app; it will be a different 64 character string. A case for iCloud indeed; an option I’ll explore later perhaps.

But this is paper about remote notifications and I want to keep it focused, so for now we’re going to wing it and just print it out to the console.

Compile and run your code, it won’t work. It won’t work cause you need to click on the target of your Xcode project, move across to Signing and Capabilities and add [with the plus sign next to the work Capabilities] Push notifications.

Image for post
Image for post

You can now run the project and you should get a 64 character code in the console, under the word Device Token. As I already said, in the real world you need to store this in a database.

Now, something different. You need to login to your Apple account now and get a private key that you’ll be using to request the post notifications. Open a web browser and sign into your Apple Developers account.

On the the overview page you’ll see the tab we’re after. “Certificates, Identifiers and Profiles”.

Image for post
Image for post

Click on it and then click on the item called Keys in the left hand tab. Once there you can click on the white plus in the blue circle. You should find yourself looking at a configuration screen that looks much like this.

You need to register a new key for APNs services. Tick the ENABLE box and give your key a name. You can use the same key with multiple applications at this point, so there is no need to link the name with the project, it is name that make sense to you.

Image for post
Image for post

Click on register and wait a moment. Having registered take a special note of the Key ID that will appear on the page. You’ll need it later!

Image for post
Image for post

Download your signature. It will contained in file with an extension p8. None of the apps on a standard OS X understand what a P8 file is and you will not be able to open it normally.

But we’re winging it here and I need the contents of the p8 for the next chapter. Go to downloads, right click the file and open it with the TextEdit app. No, it isn’t a recommended app to open p8 files. You see content like this.

-----BEGIN PRIVATE KEY-----
MIGTAgVAMBMGByqGSM49AgEGCCqGSM49AwEHBHkwdwIBAQQgl8Kij2y6acAgp1FZ
BHqI6T/Bv4bBgndxuVr1IfuYhemgBgYIKoZIzj0DAQehRANCAASweAt5jGR5H1Vf
QmlPyVVa2hn8jPLxdg0wHyP/xpXbJ5kGunlkXomLh8k+d31tWKKQF2QTzPCzckyi
p0aHWAWG
-----END PRIVATE KEY-----

This is my secret private key that I will use to encrypt my communications with Apple. Now go back to Xcode and create a new file we’re going to call RemoteNotifications.swift. Add two local variables to the class which will look like this.

private var privateKey = """
-----BEGIN PRIVATE KEY-----
MIGTAgVAMBMGByqGSM49AgEGCCqGSM49AwEHBHkwdwIBAQQgl8Kij2y6acAgp1FZ
BHqI6T/Bv4bBgndxuVr1IfuYhemgBgYIKoZIzj0DAQehRANCAASweAt5jGR5H1Vf
QmlPyVVa2hn8jPLxdg0wHyP/xpXbJ5kGunlkXomLh8k+d31tWKKQF2QTzPCzckyi
p0aHWAWG
-----END PRIVATE KEY-----
"""
private var token = "ae4498276c2dfc9ff33a101edc5df3f1e4ef9bbe876f717536e5504d63a38bda"

Yes that is three quotes, you need the whole shebang. 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