Push Notifications in Your Ionic App with OneSignal

This article was updated on May 3, 2017, for Angular 4 and Ionic 3.

Introduction to push notifications, what they are, how they work

Push notifications are messages sent from your server to your app’s users. They notify users of new content, even when the user is not using your application. They increase user engagement and retention in your app. An example is the WhatsApp “whistle” that notifies you of new messages received. In this tutorial, we’ll dive into integrating push notifications using OneSignal.

How push notifications work

A push notification is sent from the app server to one of the Push notification platforms for the mobile OS. Apple’s Push Notification Service for iOS and Google Cloud Messaging for Android. These push notification services relay the message to the appropriate devices.

--ADVERTISEMENT--

You may be wondering, how does your server & GCM know which device to send the notification to? Your app will include code that registers its unique device token with your server.

Keeping track of these device tokens on your server is tedious. But there are some great services out there to simplify the process. One such service is OneSignal.

Step 1: Install Ionic

To start off with, you need to have Ionic installed on your machine. You install it using the node package manager npm;

If you do not have **npm** installed, follow the instructions in the references section to install it.
## Step 2: Start a new project
Next, start a new ionic project by running either one of the following commands based on your target version of Ionic;
**Ionic 1**

$ ionic start PushDemo sidemenu“`

Ionic 2/3

The above command creates a new project in the current directory. The project folder will be called *‘PushDemo’* and it will use the *sidemenu* template.

$ cd PushDemo && ionic serve –lab“`

The code above navigates into your project folder and opens up your app for testing in a browser window.

Step 3: Register on Google Cloud Messaging (GCM)

Visit the Firebase Console and log into your Google account:

https://firebase.google.com/

Create a new project, give it a name and enter your country.

Click the Gear icon in the top left and select Project settings -> CLOUD MESSAGING

Take note of the two values highlighted in the image. The Server key, also known as the Google Server API key and the Sender ID, also known as the Google Project Number.

Step 4: Create OneSignal Account

Browse to https://www.onesignal.com and create an account.

Confirm your email address and login

Click on Add new app and name it whatever you want. I would recommend that you give it the same name as you did your ionic project.

Next, select the platform that you want to configure with OneSignal push notifications. In this tutorial, I will be configuring push notifications for Android.

Enter your Google Server API Key and Google Project Number. These are the details you got from the Firebase console in step 3.

Next, select the target SDK for your app. In our case, we are making an Ionic app so you select, “PhoneGap, Cordova, Ionic, Intel XDK”

Take note of your OneSignal App ID. You will need this when integrating OneSignal into your app code.

Step 5: Integrating OneSignal into your ionic project

Go back to the command line window and from within your project directory run;

This will add the OneSignal plugin to your ionic project. This plugin gives our ionic app an API to access Push notifications. Otherwise, they would be unavailable to hybrid apps.
### Step 5a: Integrating OneSignal into Ionic 1
Next, navigate to your main ```app.js``` file *[project-directory]/www/js/app.js*
![](https://dab1nmslvvntp.cloudfront.net/wp-content/uploads/2017/01/1484738035onesignal-code.png)
Add the following code to your ```$ionicPlatform.ready()``` function as above

Make sure you enter your **App ID** and **Google Project Number**. Save the file.
That's it! That's all you have to do to integrate the OneSignal code into your Ionic 1 or 2/3 app.
Next, connect your android testing device to your computer via USB cable.
Then compile and install the PushDemo app with the following command:

Charles Muzonzini is a Mobile App Developer specializing in cross-platform development using the Ionic Framework. He runs a Digital Agency, webgems and he writes mobile app development tutorials at Sitepoint.com and his developer blog, Muzonzini on Mobile. He is also the co-organizer of the Harare WordPress Meetup.