Node.JS is a nice technology for building high-performance and efficient backend applications. Thus it is also a very popular technology to be used as a mobile application’s backend server, where performance impacts user engagement highly. For a mobile application, push notification is a highly important aspect. Implementing push notifications includes both iOS and server-side implementation efforts. Today, in this tutorial, I will try to show how we can have the apple push notification backend server implementation set up and running in a while very easily.
The Workflow Architecture:
To implement the back end, it’s essential that we do understand how the push notification functionality actually works behind the scene. Let’s see the architecture, and I will describe the workflow steps below:
- iOS App Device Token: each iOS app has a unique device token for the user. If an application needs to implement push notifications, it must have this device token. Thus your iOS app needs to send the token to the server.
- Store the device token: After an app sends the token to the server via our backend REST API, we need to store it to use later for sending push notifications to this user specifically
- Event Trigger: When some certain interesting event occurs, we need to send a message to our back-end worker(preferably via the message queue system) that contains the main functionality for sending the push notification.
- Retrieve Device token: The back-end worker needs to retrieve the device token for the specified user to send the push notification accordingly.
- Sending Push Notification: The backend worker will now use the device token and use apple’s push notification API to make an API call to apple’s push notification server.
- Bingo!: The push notification will pop up on the corresponding iOS device.
As part of the workflow, step 5 includes the main server-side implementations specific to sending a push notification, and I will discuss that part only. Other parts, like saving/retrieving device tokens etc, I am leaving them up to you.
Implementation With The APN library for NodeJS:
There is a very nice wrapper library for APN in NodeJS named ‘apn’. This will give you a very easy-to-understand API interface to interact with. Here is a simple code snippet that will give you a brief idea of start using this:
Let’s know about the different components in brief:
- Certificate Files: We will need a valid certificate pair from apple to get this working properly. If you have a .p12 file, you can convert it to the expected .pem format using these instructions. If you have no idea, start with the official apple site for push certificates
- Error Handler: You need to pass an error handler if something bad happens. You can use convenient logging of the errors there.
- Feedback Handler: The feedback handler is attached and used to handle failed notifications. You may want to resend or log/keep track of them for future use.
- Notification Settings: It includes various settings to configure how apple interprets your notification. To know more details about them, check the tables 3.1 and 3.2 on apple’s official documentation here.
However, there is possibly an issue with this library. The certificate and key files are required to be put in the root directory. If you place them somewhere else and try to add the paths in the ‘options,’ you might get an error. Most probably, the issue is related to this GitHub ticket.
I am not an iOS developer and thus tried to keep the scope of this apple push notification backend tutorial specific to server-side implementation only. If you don’t understand the part described/shown above, feel free to ask me via commenting below. Happy coding 🙂