Push Notifications in Android Using One Signal

Push Notifications in Android can be quite tricky if you create everything from back-end to mobile application.

So there exist a ready made solution called One Signal. It uses Firebase as its dependency to deliver the notification.

Let us see how we can setup everything step by step. And this tutorial assumes you have already created account in Firebase and One Signal.

If you are here just for the code navigate to heading no 4 and follow along.

Creating a new Firebase project

Here I will outline the step by step process to creating a new firebase project.

Step 1: Head over to firebase console.

Step 2: Create project by clicking Add Project

firebase create project for push notifications

Step 3: Fill in the name for your project and click Continue

setup project name firebase

Step 4: Enable the checkbox and Continue.

enable google analytics on firebase project

Step 5: Select default account for firebase and click Continue.

select analytics account for firebase

We have successfully setup our project. Let us move forward to adding a new application to the project.

It can either be Android, Web or iOS. For us we are interested in Android so let us carry on with setup.

Adding Android application to firebase project

In the previous section we have created the new firebase project. Let us add an Android application to the fresh project here in this section.

Navigate to Project Overview in the bottom left side as shown in the image below. And then click on the Android Icon.

firebase console dashboard

Step 1: Find the Android package name from the AndroidManifest.XML. In the package="" portion. Take that and fill the boxes below.

add new application firebase console

Step 2: Click on Download google-services.json click Next. A file will be downloaded.

download google-services.json firebase console

Step 4: Copy the file from download location and put it inside the app directory.

adding firebase-service.json file to project

Step 5: Add the code below in the build.gradle file located in the root directory.

classpath 'com.google.gms:google-services:4.3.4'Code language: Groovy (groovy)

The full file will look something like this.

Also in the app > build.gradle file add the code below.

// use this if your app has structure similar to the one shown below.
id 'com.google.gms.google-services'

// else use this
apply plugin: 'com.google.gms.google-services'

// add in dependencies block
implementation platform('com.google.firebase:firebase-bom:26.1.0')
// implementation 'com.google.firebase:firebase-analytics' // for java
implementation 'com.google.firebase:firebase-analytics-ktx' // for kotlinCode language: JavaScript (javascript)

Then hit sync, we have added the android application to firebase project. Now it’s time to create our one Signal App.

We are almost done setting up push notifications in our Android application.

Creating One Signal application

Let us see how we can create one signal application. Because we will need to create one.

Step 1: Find the Server key and Sender Id from setting of firebase project.

Step 2: Login to One Signal dashboard and click New App / Website

Step 3: Fill in the name and click on Android icon and click Next

Step 4: Fill in the Server key and Sender Id from Step 1 and click Next.

Step 5: Click on Native Android and click Next.

Step 6: Copy the App Id for now and that’s it.

Adding One Signal SDK to Android Project

Now that we have successfully create a new One Signal project. Let us add it in our application.

Follow along to add the one signal SDK.

Add the following code to build.gradle file in your project directory.

// in repositories
maven { url 'https://plugins.gradle.org/m2/'}

// in dependencies
classpath 'gradle.plugin.com.onesignal:onesignal-gradle-plugin:[0.12.9, 0.99.99]'Code language: JavaScript (javascript)

Also we have to add the code below in our app > build.gradle.

implementation 'com.onesignal:OneSignal:[3.15.4, 3.99.99]'Code language: JavaScript (javascript)

Add the code above in dependencies block of app > build.gradle

Also we have to add the app id that we have after creating one signal project.

Add the code below in app > build.gradle‘s defaultConfig { } block. As shown in the image below.

Hit the sync now button and let gradle build. Now if we run our application at this point everything will work as expected.

If not just change the position of plugins as shown in the image below.

Initializing One Signal SDK

Now that everything is working as expected let us initialize the SDK.

I have created a class PniaApplication that extends Application class. Copy the code in the onCreate() of the class.

class PniaApplication : Application() {

    override fun onCreate() {
        super.onCreate()

        OneSignal.setLogLevel(OneSignal.LOG_LEVEL.VERBOSE, OneSignal.LOG_LEVEL.NONE);

        OneSignal.startInit(this)
            .inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
            .unsubscribeWhenNotificationsAreDisabled(true)
            .init();

    }

}Code language: Kotlin (kotlin)

And in our AndroidManifest.XML we have to add the following line. See in the image below and add it.

What it does is that our custom application class gets attached to android application life-cycle.

Testing push notifications from One Signal dashboard

Now if you have already run the application. You will see subscribed users 1 in your one signal dashboard.

number of one signal subscribed user

Head over to Messages > Messages and click on New Push.

composing firebase push notificaion

Fill in the title and message shown in the image below and scroll to the bottom. Click on Confirm to send the notification.

composing push notification one signal

Check the device we will see a notification in our status bar.

push notification success

That is it we have successfully added push notifications using One Signal. If you have any confusion or questions. Comment below.

Related Posts

Learn how to use fileprovider in Android with example

Implement SMS Broadcast Receiver in Android

Async Task in Android is Deprecated: There are Better Ways

Using Bottom Navigation Bar in Android: An Easy Guide

Show or Hide Soft Keyboard in Android Application And More

Related Posts