Android Tutorials : Push Notification Service using One Signal

OneSignal Push Service

OneSignal is a powerful and reliable push notification service for websites and mobile applications. Push notifications allow apps to reach out to users with short messages that users may respond to. Every major app uses push notifications for transactions and user engagement. In this tutorial we will learn how to set up OneSignal Android SDK in our Android application to send push notifications. OneSignal provides an online dashboard to send push notifications. OneSignal is a free service that supports unlimited devices and notifications.

Here’s the list of some of the features OneSignal offers :

  • Automated messages

  • Message templates to use for later

  • Target specific groups with user segments

  • Unlimited devices and notifications for Free

  • Build and configure a notification the way you want

  • Online dashboard to send push messages

  • Provides SDKs for every major cross-platform mobile development environment

  • Marketing tools including A/B testing, segment targeting, localization, drip marketing, and conversion tracking

  • Single UI and API to deliver messages across iOS, Android, Amazon Fire, Windows Phone, Chrome Apps, Safari, Chrome Web, and Firefox

Project on Github

Let’s dive right into creating an android application to learn more about sending Push notification using OneSignal. The steps are described in 4 detailed yet simple parts :

Note : Make sure you have the latest version of Google Play Services and Google repository installed in your Android SDK manager.

Part ! : Includes steps to create a new OneSignal account and set up OneSignal Android SDK.

1. Firstly you need to Sign up for new account at OneSignal by giving required details. Once you sign up, confirm your email and login to your OneSignal account.

OneSignal Account Creation

2. For the first time when you login to your OneSignal account, you will be shown a welcome pop as shown below.

OneSignal Welcome Popup

3. Click on next button in each slide of the pop up until you reach the last slide of the pop up. In the last slide you will have a button to Add a New App as shown below. Click on it and enter the name of your app. I have given the name as “AndroidBashOneSignal”.

OneSigna lAdd New App

OneSignal Add New App Name

4. Now you need to select a platform to configure. Select Google Android (GCM) as the platform and click on Next button.

OneSignal Platform

5. Now you will be asked to enter Google Server API Key and Google Project Number. These keys allow OneSignal to use Google’s web push services for your notifications. This can be obtained by creating a Firebase project. Visit Firebase console in another tab of your browser and create a new project by clicking on Create New Project button.

OneSignal Firebase project

6. Once you create a Firebase project, you need to click on the Gear icon in the top left and select Project settings.

OneSignal Firebase Project Settings

7. Select the Cloud Messaging tab in it. You’ll need your Server key, also known as the Google Server API key and your Sender ID, also known as the Google Project Number.

OneSignal Firebase Cloud Tab

8. Now go back to OneSignal dashboard and paste your Server key and your Sender ID as shown below. Click on Save button.

OneSignal Google Key

9. Now you need to select Native Android as the target SDK and click on Next button.

OneSignal Native SDK

10. You will be shown a OneSignal App Id which you will be requiring in your android application. Please make a note of your OneSignal App Id. Now when you click on Check Subscribed Users button, as there are no subscribed users, it will display an error message saying “No subscribed users are detected”.

OneSignal App Id
OneSignal App Id

11. Now we need to create an Android App and run it, so that there will be at least one subscribed user. Upon each install in a new device your subscriber count gets increased. We will move on to Part 2 to create a new Android application in Android Studio and set up OneSignal push service SDK in it. Once we run our Android app, we can come back and click on “Check Subscribed Users”

Part !! : Includes steps to create a new Android application and set up OneSignal SDK in it.

1. Create a new Android Project in Android Studio. Give it a name and select the Minimum SDK on which your app will run on. I chose API 16 : Android 4.1 (JELLY_BEAN).

2. When you are prompted to add an activity to your application choose Blank Activity and click on next button.

3. In the next step click on Finish and in few seconds your application should be loaded in Android Studio.

4. Open build.gradle (Module:App) file of your application and add the following dependencies inside dependencies section of build.gradle (Module:App) file.

5. In the same build.gradle file, add the following in your android -> defaultConfig section. Update “YOUR ONESIGNAL APP ID HERE” with your OneSignal app id which we obtained in previous part. Whenever you make any changes to your build.gradle file you need to sync the project or re-build the project.

6. My build.gradle (Module: app) file looks like this, at the end of the project.

7. Create a new Java class in your package and name it as This will be your Application class. The Application class is mainly used for some Application level callbacks and for maintaining global Application state. Add the following code in it. This is where OneSignal SDK will be initialized. Also we are initializing MyNotificationOpenedHandler (This will be called when a notification is tapped on) and MyNotificationReceivedHandler (This will be called when a notification is received while your app is running). We will create these two java classes in later steps.

8. Don’t forget to initialize your Application class in AndroidManifest.xml by adding android:name=”.MyApplication” under tag. You also need to add a tag and a tag inside your AndroidManifest.xml. This is how my AndroidManifest.xml looks at the end of this project.

9. Create a new Java class in your package and name it as This will be called when a notification is tapped on. Add the following code in it. Please go through the comments written along with code.

10. Create a new Java class in your package and name it as This will be called when a notification is received while your app is running. Add the following code in it.

11. Create a new Java class in your package and name it as This can be setup to receive silent notifications when your app is not running or to override how notifications are shown in the notification shade. Add the following code in it.

12. Starting with Android Lollipop 5.0 a small notification icon is required for the icon to be visible in the status bar. You can follow this tutorial or create icons manually by giving it a name as ic_stat_onesignal_default and place it in res->drawable folder as shown below.

OneSignal Notification Icon

Part !!! : Includes steps to create Activities and Activity layouts.

1. Open your and make sure you have the following code in it.

2. Open your activity_main.xml and add the following code in it. I have added Card views to display the text in our MainActivity. For you to add card view, make sure you have added dependency in your build.gradle file.

3. Create a new Java class in your package and name it as Add the following code in it.

4. Create a new resource file under res->layout folder of your application and name it as activity_another.xml. Add the following code in it.

5. Setup is done and now you can run your android app! Make sure you are testing push notifications on a device or emulator that has Google Play services installed and updated on it. I suggest you to try on your mobile device. Once you run your app on your device or emulator, you can go back to OneSignal dashboard and click on Check Subscribed Users button. Now it displays that set up is successfully completed as shown below

OneSignal Set Up Complete

Part !V : Includes steps to send a Push notification using OneSignal dashboard

1. Visit OneSignal Dashboard for your app and click on New Push Notification button. Enter a title and content for your Push message.

OneSignal Push Message

2. You can specify an image that will be displayed with your Push message by giving an image URL as a value to the Big Picture field as shown below.

OneSignal Push Message

2.Now you can include Addtional Data that can be used in your android app to achieve some specific requirement. In this example, I am sending key->value pair, where key is “activityToBeOpened” and value is “AnotherActivity“. In our Java class, we have written code to handle this additional data. Also you can specify action buttons as shown below. Even these action buttons are specified as key->value pair and can be retrieved in your android application as shown in class.

OneSignal Push Message

Here is the final Result of this project as displayed on my emulator.

OneSignal Result

Useful links for OneSignal documentation

If you have liked our tutorial, you can help us keep this website free for all Android enthusiasts by donating any small amount to my PayPal account. If you have followed our tutorial please do let us know by commenting below. It makes us write more and more articles on android application development. In case if you get stuck somewhere please do comment below and let us know. We usually reply within a day.

You can download the project from the below link and try it out by including your OneSignal App Id in build.gradle (Module : App) file of this application.

Download Project

Project on Github

Android Push Notification Service using OneSignal

25 thoughts on “Android Push Notification Service using OneSignal

  • December 18, 2016 at 4:50 am

    Does push notification works if the app is in foreground or killed state

    • December 24, 2016 at 11:19 am

      Hello Nirav, Yes it works 🙂

  • February 28, 2017 at 2:12 pm

    Hello sir, thank you for the great tutorial.. i want something more like when app is in foreground shows notification as alertdialog , i want to use my custom style for that alertdialog..

    So i used inFocusDisplaying(OneSignal.OSInFocusDisplayOption.None)

    And on the
    public class MyNotificationReceivedHandler implements OneSignal.NotificationReceivedHandler how can i show the dialog? It does not displaying on runnable also

    Only display justnotification..
    Please help me.

    • March 2, 2017 at 2:02 am

      Hello Joy, Can you please tell me more about your requirement?

  • March 11, 2017 at 5:29 pm

    Thank you so much sir for this tutorial.. sir i want to save my notification data in my local database (sql lite ) how can i do this. can you please guide

  • March 13, 2017 at 9:08 am

    How to send from user to user

    • March 14, 2017 at 3:05 am

      There are many options available with OneSignal. Please explore their dashboard to know all the features. You can also get a list of all users from OneSignal dashboard.

  • March 25, 2017 at 11:39 pm

    Hi Sir,

    Thank you so much, this is a great tutorial.
    I followed your guide, my apps can push notications on foreground state, but when it on killed state, I can not receive any notitification? So, can you please guide me how to make OneSignal can push notification when app in killed state? Thank you so much !

    • March 26, 2017 at 6:02 am

      Hello, Onesignal notifications work even when the app is in Killed state. I use it in my own app. Can you please retry once again?

      • March 27, 2017 at 3:17 am

        I just downloaded your project and run it again and it working on killed state.
        And another question, does your project support hanlde open main_activity when user click on notification? If not, what can i do to enable this feature ?
        Thank you so much !

        • March 28, 2017 at 3:18 am

          Remove the meta-data with name com.onesignal.NotificationOpened.DEFAULT from AndroidManifest.xml to launch main Activity by default.

          • March 28, 2017 at 12:23 pm


            Thank you so much ! it working for me !

  • April 25, 2017 at 10:27 am

    how to open an activity without clicking on notification using onesignal or fcm ?

    • April 27, 2017 at 2:33 am

      It can be done using Services which run in the background and takes care of the logic to open activity when a notification is arrived. But this should not be done as it will be bad experience for the user to have an app opened without clicking on anything.

  • May 17, 2017 at 2:35 am

    Is it can be used to webview when the notification clicked then the url open in application not to browser ?

  • June 18, 2017 at 11:22 am

    Hi, thanks for the great post. I’ve done everything on the post one by one. But I have a question;
    I have a webview app, and I want to add an url ( – New message – “Launch URL” section), when the user click on the notification, the url will open in the webview app and it will show the url page, not the welcome screen in the app. If you can help me, I would be very appreciated.

  • June 28, 2017 at 2:21 pm

    Hello! The tutorial is amazing! But please, can you tell when the “Outdated Android Support Library” can appear for a user? Thank you!

  • July 5, 2017 at 6:56 am

    I have an error while receiving notifications
    onNotificationProcessing throw an exception. Extended notification displayed but custom processing did not finish.
    java.lang.NullPointerException: Attempt to invoke virtual method ‘android.content.res.Resources android.content.Context.getResources()’ on a null object reference
    at iq.osamasw.cinerama.service.MyNotificationExtenderService$1.extend(MyNotificationExtenderServ

    and its points me to
    Bitmap icon = BitmapFactory.decodeResource(getContext().getResources(),

    but the resource is found and I have no error in code;

    • August 3, 2017 at 2:02 am

      It is getting context as null. Please verify this class :

      public class MyApplication extends Application {

      private static Context context;

      public static Context getContext() {
      return context;

      public void onCreate() {
      context = getApplicationContext();
      //MyNotificationOpenedHandler : This will be called when a notification is tapped on.
      //MyNotificationReceivedHandler : This will be called when a notification is received while your app is running.
      .setNotificationOpenedHandler(new MyNotificationOpenedHandler())
      .setNotificationReceivedHandler( new MyNotificationReceivedHandler() )


  • July 13, 2017 at 7:36 am

    After running app, it is showing “We haven’t detected any subscribed users yet for this platform”

    • August 3, 2017 at 2:00 am

      Make sure you are running the app in a device which has updated Google Play services. Have you pasted your OneSignal App Id in build.gradle (Module: app) file?

  • August 5, 2017 at 10:44 am

    I am unable to send image in push notification only text messages are displaying

    • August 8, 2017 at 6:16 am

      when i send both message and image as a push notification i am only receiving message only i need image also

      • September 27, 2017 at 2:26 am

        Hope your Issue is resolved by now. Sorry for the delay in reply. Thank You.

  • August 5, 2017 at 10:52 am

    Please help me i am unable to send images only text notifications are delivered


Leave a Reply

Your email address will not be published. Required fields are marked *