Remote push notifications for iOS APNS

Scott
9 min readMay 17, 2018

November 17, 2017

Create your project (command shift “N” while Xcode is selected). Then click next.

Then name it.

Then click Capabilities, and turn on push notifications.

Click General
Zooming in…
Turn on push notifications.

If you are unable to turn on push notifications. You might not have created a paid Apple Developer account. Do so here:

Apple Developer Center

Oh yea…and do this too, while you’re here, otherwise you’ll get an annoying warning and sometimes your push notifications might not work.

Now go to Apple’s developer program. We will upload the request for a certificate in order to get our first certificate.

Go to your account. If you don’t have one, create one and pay the $100 annual fee or whatever it is at the time of reading this.

After signing in, click on Certificates, Identifiers, and Profiles.

Instead of going to the certificates section directly, we must first click the app id.

Click the + button to add an app id for your project.

The App ID description will help you identify your project in your list of project ids.

Use the explicit App ID if you want to be able to see that description (recommended over wildcard).

Grab the bundle id from your project.

Paste it here!

Scroll way down and select Push Notifications.

Then click continue.

If you see this warning:

you might have clicked Continue twice. Your id should show up when you click here:

Now click on App IDs regardless. Find your respective App ID, and click on it. You should see the following view.

Notice Push Notifications are yellow and configurable!

Click Edit.

Then scroll down to the Push Notifications section.

Click Create Certificate. Apple uses one server to send push notifications to iOS apps that are not downloaded from the store (called SandBox/Development) and another server (Production) for the ones that are. Your app will keep a persistent connection with the respective APNS server based on which certificate your system is using at the time. A development certificate will not work on apps downloaded from iTunes.

After creating the development and production certificates you are going to store on your server (a.k.a. Provider in the context of apns). Your development server will hold the development cert and your production server will hold your production cert. I’m going to do the Production one in this tutorial. If you do the development one, it will only work on your development environment but it won’t work the app store.

Here are Apple’s instructions… but you can follow mine instead.

Get a certificate signing request via keychain access. Its easiest to find the keychain Access application by the search icon, top right.

After opening Keychain Access, Request a certificate from a certificate authority.

Put in your email in “User Email Address.” Then select Saved to disk and you don’t have to provide an email for CA Email Address.

After clicking continue, Certificate Assistant (the program that was opened by Keychain access to help you create the certificate signing request) will ask where you want to save the certificate signing request.

I personally find the default naming convention annoying. It was designed by the school for redundancy school. I like to rename it according to my project.

Clicking save will save the document. It doesn’t look like a certificate, because it isn’t, it is just a document used to request one.

Push notifications are a kind of upstream messaging, and therefore could lead to security risks without taking certain precautions. iPhones are configured to reject messages that are not properly encrypted and verified. We are creating proper certificates (a way for all parties to confirm that each other party is who he says he is) so that our provider (your project’s server), Apple Push Notification Servers, and your user’s phone can trust that each party is who they say they are.

Now look back to the CSR Apple Developer Account web page and click Continue at the bottom.

Then grab your cert and upload it.

And then click Continue.

Then click Download.

Then double click on the downloaded file.

It should show up in your keychain access. Most of the certificates will be hard to differentiate, you can find your certificate because the end of the Name of the certificate will be your project’s bundle id.

Picture from https://www.appcoda.com/push-notification-ios/

Then click Certificates, Login, then right click your certificate.

When you have a lot of certificates, it can be confusing which one is yours, however the expiration date it 13 months from the creation. Just look for an expiration date with the month incremented and the year incremented, and decriment the day from today’s date. For example, if today’s date is 3/26/2018 the expiration date will be 4/25/2019.

Click Export

Click Save.

Click OK.

Put in your computer’s password here.

Not a problem, you should now see your glowing p12 certifcate.

Now go back to the Apple Developer website again. YAY! NOT UNDER CERTIFICATES. Go to PROVISIONING PROFILES.

Way down here. It doesn’t matter whether you click the Distribution or Development

Click the +.

because both options are provided after clicking the Plus.

We’re doing the App Store one right now.

Find the option that contains your bundle id.

Then click Continue.

Here is Apple’s quiz. Guess which one is the right one?

Choose the one with a date that is farthest in the future.

Now give it a name. Why? for emotional attachments.

Then, you guessed it…

Then click download.

Click on Downloads to access it, drag it out to your desktop, then double click it. It copied your projects bundle id that you gave on the Apple Developer Website, so it finds your app and installs itself when you double click it. Smart.

Oh and make sure your signing is automatic.

Prepare your code to request push notifications. Go to your AppDelegate.swift file. At the top import UserNotifications

import UserNotifications 

The function to define/add:

Then define getNotificationSettingsThenRegister() .

Then call the function when your app finishing launching, by placing the call within the didFinishLaunchingWithOptions AppDelegate method definition.

Add the following method in case something goes wrong. Start typing didregisterforremote at the AppDelegate class scope, and the swift compiler will autofill the function.

We’ll need the device token to

In case the registration fails for some reason, you should probably add this delegate method to your code as well.

Now lets put your app on the store. For time sake I am going to put my app on Testflight which is treated as a distribution version.

Its time to test.

Download pusher. (You can check for updates here and here).

Gotta love that creeper guy icon.

So you just have to run your app.

Copy the device token from your debugger and paste it in the device token field. Then select the corresponding push certificate from the “Select Push Certificate” drop box.

Then click “Push”.

Pusher converts p12 to pem files behind the scenes. You could go through the tedious terminal commands to convert the p12 to pem files, however I think its better practice to convince your backend engineer to use a script to do it for several reasons.

Reason 1: Debugging becomes easier. Lets say there is a problem with push notifications, they aren’t coming. Perhaps you might want to revoke and redo your certificates. This is great because all these steps to set up push notifications are fairly tedius.

Reason 2: Once there is a script developed, you can simply upload the cert to a portal. Nice.

Let me know if you had any difficulties when following this tutorial in the comments. I’ll check them.

--

--