Setting up push notifications in a hybrid mobile app can be a challenging task if you don’t know the mechanics of it. Fortunately, Ionic’s ngCordova service allows us to handle push notifications easily. This article assumes that you have a basic understanding of Hybrid Mobile Applications and how to deploy them to an Android device. If not, I will try and mention the steps explicitly when I can. Please also note that this is not a Getting Started Tutorial for Ionic and you need to have a development environment setup beforehand with the Android SDK. In this article, we will mostly be discussing how to send Ionic Push Notifications.
How does it work?
We will be using a PHP script to send a push notification to our Ionic app. Our app will be running on an Android Device and it will use Google Cloud Messaging (GCM) to handle notifications.
Create a new Ionic App using the Get Started Tutorial on Ionic’s website.
Add the Android Platform to our app using:
Build out the app using the process described here so that we can get the SHA1 key. To generate the .keystore file, use this:
To get the SHA1 from the keystore, use this:
You will get something like this:
We need the SHA1 key to register our Google App in the next step.
Register a Google App in the API Console. Use any package name and enter the SHA1 that we extracted in our previous step.
After you save, you will get a screen with the following information. Copy the Client ID as we need it for future reference.
Once that is done, you also need to create a Public API Access Key that will be used in the PHP script to authorise our app.
Enable the GCM API in the APIs tab on the left. In my case, I have already activated it which is why it shows Disable API.
Last but not the least, copy your Project ID from the Overview tab. This is called the SENDER ID and we will use this in our Ionic App Controller.
Now that our Google app is all setup, we can begin writing a PHP script that will accept a GET parameter of the registration ID to send a push notification. You can include multiple IDs that are comma-separated, but in this case we will be using just one. But how do we get a Registration ID?
Before our PHP script can send push notifications, we need to register our Android Device to receive notifications. This is done with the help of the Push Notification plugin from ngCordova that we will discuss in Step 6.
Here is the PHP script:
Now that our PHP script is ready, install the Push Notifications plugin from ngCordova and then we need to write Ionic code to register our device:
The above controller allows us to register the device when the app loads and it will give us a registration ID. This is the ID we need to provide to our PHP script to send a push notification to this device. In later stages, you might want to automate all this stuff but in this article, we are only covering the basics of Push Notifications.
When you run your PHP script, pass in the Registration ID as a GET parameter like:
Before you hit Enter, minimise the app on your Android Device and return to the Home Screen so that you can see the proper effect of the Push Notification arriving. Trust me, it feels good.