Setting up Push Notification for iOS
3 Dec 2018Push notifications seems to be an integral feature needed when developing most mobile applications today. Like all implementations of a feature into a mobile app, it can seem quite daunting at first to integrate, but once you get through it a few times you can get the hang of it!
Important foundations
There are a few things to note before starting that should help solidify a strong foundation for the project base in order to avoid any future headaches debugging issues with why it could potentially not work.
Essentially you need to understand the capabilities of Push notifications for iOS in order for you to test it after implementing the feature. Here are some thing below to note:
- Test on a real device as simulators cannot receive push notifications
- Need an Apple developer program membership in order create the relevant certificate needed to setup push notification
- You will need a way to test your device receiving push notifications. We normally pass the relevant p12 file to our backend to handle sending the push notifications for us but you can also use online tools if you don’t have that option such as:
http://apns-gcm.bryantan.info/
http://pushtry.com/
Next you must make make it clear the setup and relationship you want between your project and the push notification certificate(s)
- Confirm the correct and number of bundle ID(s) needed for the project and identify which environment they belong to (production, staging etc)
- When creating the certificate(s) in the developer portal that you setup up the correct certificate that correlates with the correct bundle ID
Setup flow
Firstly you want to go ahead and create your APNS/ Apple Push Services certificates and make sure to link to the correct bundle ID of the project! Here’s an example of what we do in this situation. We generally have two different bundle IDs based on the two environments we have setup - Development and Production.
Although if you have your project setup with just one bundle ID for both environments (or how ever amount you have) then feel free to just create a Apple Push Service certificate as this can handle both sandbox and production services.
- Development: An example of a bundle ID we would use for this environment com.sentia.dev for this situation we we would create an APNS certificate
- Production: An example of a bundle ID we would use for this environment would be com.sentia for this situation we would create an Apple Push Service certificate
Once that is established go ahead and generate your Certificate Signing Request (CSR) file from your machine. You can do this by opening up Keychain Access and request a Certificate from a Certificate Authority
Enter the relevant information and be sure to save it to disk, as you will need to upload this certificate in the next step.
Once that is done download your the certificate and install it into your machine. Go back to Keychain Access and locate the new certificate. From here you can then go ahead and expand it -> export the two items in order to create a pem file out of it (You can enter a password to protect the file but it’s optional you can leave it blank if you wish)
Now after this step has been completed you can enter in the following commands in your terminal to convert them into the p12 files that you need to finish the process! (Enter commands)
cd
cd Desktop
openssl pkcs12 -in pushcert.p12 -out pushcert.pem -nodes -clcerts
Then you can enter the following if you wish to remove the p12 file as it shouldn’t be needed anymore.
cd
cd Desktop
rm pushcert.p12
Last step will be for you to enable push notifications in the capabilities of the Xcode project and that’s it!
Summary
In conclusion once you go through this process a few times setting up push notifications should become natural. From here you can do a bunch of things with how you handle push notifications which can be handled in your AppDelegate
class.
Other key notes to look out for is that these certificates are only valid for 12 months so once they get close to expiry then feel free to create and attach a new certificate to the bundle ID (Following the same process as earlier). You can have both certificates attached to the ID but once the old expires then it will disappear creating a seamless transition between the old and new certificates.
Sources
Apple, APNs overview
Apptutions, September, 24, 2014, Generate .pem file for PUSH Notification
Aleksandrov, Deyan, Feburary 22 2018, iOS remote push notifications in a nutshell
Wessendorf, Matthias, June 17 2015, WWDC 2015: Apple Push goes HTTP2 for APNs