Node.JS is a nice technology to build a high performance and efficient backend applications and thus it is also very much popular technology to be used as mobile application’s backend server, where performance impact user engagement highly. For a mobile application, push notification is a highly important aspect. Implementing push notification includes both in iOS and server-side implementation effort. Today, in this tutorial, I will try to show how can we have the apple push notification backend server implementation setup and running in a while very easily.
The Workflow Architecture:
To implement the back-end, its essential that, we do understand how the push notification functionality actually works behind the scene. Lets see the architecture and I will describe the workflow steps below:
- iOS App Device Token: each iOS app has a unique device token for user. If an application needs to implement push notification, it must have to have this device token. Thus your iOS app need to send the token to server.
- Store the device token: After an app sends the token to server via our backend REST API, we need to store it to use later for sending push notification 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 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: 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 very easy to understand API interface to interact with. Here is a simple code snippet that will give you the 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 .p12 file, you can convert it to expected .pem format using these instructions. If you have no idea, start with 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: Feedback handler is attached and used to handle failed notifications. You may want to resend them 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 table 3.1 and 3.2 on apple official documentation here.
However, there is possibly an issue on 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.
Final Words:
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 any of the part described/shown above, feel free to ask me via commenting below. Happy coding 🙂
thanks for this is tutorial
i do that on server but i have problem
node_modules/apns/lib/file-loader.js:43
if (err) throw err;
^
Error: ENOENT, open ‘./apns/pushcert.pem’
at Error (native)
i just slove it thanks
Cool! Happy to hear that you got it solved.
How did you solve this? I’m getting the same error…
Hi,Could you please tell me How can we confirm,whether the notification sent or not
after this statement
apnConnection.pushNotification(note, myDevice);
Usage is:
pushNotifier.send instead of pushNotifier.process as it says in the example, right?
I understood how APNS works and i am able to send pushnotification through APNS. I want to create a web app/web site. In that i will type the notification and click send. How to achieve this using node.js
Hello, I would like to know how do we handle the notifications received. For instance, you click on the push notification received, it will take you to the application that includes the push notification feature. i wanna know how do i fetch the details of the notification received and push into an array.
I got the below error..
apnConnection = new apn.Connection(options);
^
TypeError: apn.Connection is not a constructor
at Object.init (/Users/Iexemplar/Documents/nodews/newpush/pushNotifier.js:28:25)
at Object. (/Users/Iexemplar/Documents/nodews/newpush/server.js:3:14)
at Module._compile (module.js:573:32)
at Object.Module._extensions..js (module.js:582:10)
at Module.load (module.js:490:32)
at tryModuleLoad (module.js:449:12)
at Function.Module._load (module.js:441:3)
at Module.runMain (module.js:607:10)
at run (bootstrap_node.js:420:7)
at startup (bootstrap_node.js:139:9)
Hi,
i want to send personalized push notification for each user, how can we do that ?
same error