Open In App

Push Notifications in Android Using OneSignal

Last Updated : 09 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

We have seen so many types of notifications that we received in many of the Android apps. These notifications inform our users about the new offers, new features, and many more inside our application. In this article, we will take a look at the implementation of the OneSignal notification platform in the Android app in Android Studio. We will be building a simple application in which we will be sending notifications from the One Signal platform in our Android app.

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. The code for that has been given in both Java and Kotlin Programming Language for Android.

Step 2: Connect your App to Firebase

After creating a new project. Navigate to the Tools option on the top bar. Inside that click on Firebase. After clicking on Firebase, you can get to see the right column mentioned below in the screenshot.

Inside that column Navigate to Firebase Cloud Firestore. Click on that option and you will get to see two options Connect the app to Firebase and Add Cloud Firestore to your app. Click on Connect now option and your app will be connected to Firebase. After that click on the second option and now your App is connected to Firebase. After connecting your app to Firebase you will get to see the below screen.  

After that verify that dependency for the Firebase Firestore database has been added to our Gradle file. Navigate to the app > Gradle Scripts inside that file. Check whether the below dependency is added or not. 

Step 3: Adding Dependencies in the build.gradle file

Now we will add the dependency for using One Signal in our Gradle file. Navigate to build.gradle (:app) and add the below line in the plugins section. 

plugins {
   id 'com.android.application'
   id 'com.google.gms.google-services'
   // add below line is plugins  
   id 'com.onesignal.androidsdk.onesignal-gradle-plugin'
} 

Add the below line in the dependencies section of the same file.

implementation 'com.onesignal:OneSignal:[4.0.0, 4.99.99]'

Now we will add the below dependencies in the dependency section. Navigate to build.gradle (Your app name) and add the below code to it. 

buildscript {
    repositories {
        google()
        jcenter()
       
        // add below line in build script > repositories section.
        gradlePluginPortal()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.1.1"
        classpath 'com.google.gms:google-services:4.3.4'
        
        // add below line in dependencies section
        classpath 'gradle.plugin.com.onesignal:onesignal-gradle-plugin:[0.12.9, 0.99.99]'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
} 

After adding the above dependencies section then sync your project and now we will move toward the XML part. 

Step 4: Adding Permissions for the Internet 

<!--adding permissions for Internet-->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Step 5: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 

XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="5"
    tools:context=".MainActivity">
  
    <!--text view for displaying welcome message-->
    <TextView
        android:id="@+id/idTVWelcome"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:padding="10dp"
        android:text="Geeks for Geeks"
        android:textAlignment="center"
        android:textAllCaps="false"
        android:textColor="@color/purple_500"
        android:textSize="30sp" />
</RelativeLayout>


Step 6: Generating App ID for the Application

For generating our app ID we have to sign up to One Signal similar to that we used to sign in to GeeksforGeeks using a Google account. After signing into One Signal you will get to see the below screen to create a new app. 

Inside this screen, You have to enter your app name, select the application type as Android and click on the Next option to configure your project. After clicking on the Next option you will get to see two text fields to enter two keys. 

Step 7: Getting the Keys to Enter in One Signal Console

After adding this code go to this link to open Firebase. After clicking on this link you will get to see the below page and on this page Click on Go to Console option in the top right corner.  

 After clicking on this screen you will get to see the below screen with your all project inside that select your project.    

After clicking on your project name you have to click on the settings option and select on Project settings option. The settings options are shown below. 

After clicking on Project settings. Navigate to Cloud messaging tab which is shown below. There we will get to see the server key and sender id. You can get to see this below screen. 

You have to copy these two keys and paste them into your one signal console. 

After adding these keys click on the Next option to proceed further. You will get to see the below screen. 

Inside this screen select the Android option and click on the Next option to proceed further. You will get to see your App ID is displayed on the screen. 

Step 8: Working with the MainActivity file

Go to the MainActivity file and refer to the following code. Below is the code for the MainActivity file. Comments are added inside the code to understand the code in more detail.

Java




import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import com.onesignal.OneSignal;
  
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
          
        // OneSignal Initialization
        OneSignal.initWithContext(this);
        // on below line we are setting app id for our one signal
        OneSignal.setAppId("Enter your app id here");
    }
}


Kotlin




import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.onesignal.OneSignal
  
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        // OneSignal Initialization
        OneSignal.initWithContext(this)
        // on below line we are setting app id for our one signal
        OneSignal.setAppId("Enter your app id here")
    }
}


Now run your app and we will send our first notification in our Android app. For sending notifications in One Signal click on your app name in the top left corner of One Signal Console and then click on your app name you will get to see the Dashboard screen. 

Click on the New Push option to send a new notification. After clicking on the New Push option you will get to see the below screen. 

Inside this screen, select Send to Subscribed users option, add the title option add your Notification title in the title field, and Notification Message in the message section, and scroll down to proceed further. 

After scrolling down click on the review and send option to send your notification. After that, your notification will be sent to your app. You can get to see the output of the app on the below screen. 

Output:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads