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 MyApplication.java. 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 MyNotificationOpenedHandler.java. 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 MyNotificationReceivedHandler.java. 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 MyNotificationExtenderService.java. 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 MainActivity.java 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 AnotherActivity.java. 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 MyNotificationOpenedHandler.java, 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 MyNotificationOpenedHandler.java 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

2 thoughts on “Android Push Notification Service using OneSignal

  • December 18, 2016 at 4:50 am
    Permalink

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

    Reply
    • December 24, 2016 at 11:19 am
      Permalink

      Hello Nirav, Yes it works 🙂

      Reply

Leave a Reply

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