Add One Signal Notifications in Android App

Hello Guys. Today we are going to Add One Signal Notifications in Android App.

Before Adding One signal to Our App We have to Add Firebase to Our App.

If you don’t Know how to Click Here and See my Previous Post.

Open Android Studio and Create a Project.

I already created and added Firebase SDK.

Now I’m going to Add One Signal SDK to the Project.

Project level build.gradle:
buildscript {
    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.1.2"
        classpath 'com.google.gms:google-services:4.3.8'
        classpath 'gradle.plugin.com.onesignal:onesignal-gradle-plugin:[0.12.10, 0.99.99]'

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

allprojects {
    repositories {
        google()
        mavenCentral()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
app level build.gradle:
plugins {
    id 'com.onesignal.androidsdk.onesignal-gradle-plugin'
    id 'com.android.application'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.example.firebasenotificationssample"
        minSdkVersion 16
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

    implementation platform('com.google.firebase:firebase-bom:28.2.0')
    implementation 'com.google.firebase:firebase-analytics'
    implementation 'com.google.firebase:firebase-messaging'

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

}

apply plugin: 'com.google.gms.google-services'

Now Sync the Project.

Now I’m going to connect the app with one signal.

Go to One Signal Website and Create an Account.

Now Add Our App to One Signal.

In the Platform Section, Select Google Android.

You have give your Firebase Server Key and Sender ID which connected to our app.

In the Next Screen Select Native Android.

Now an Unique Key is Provided for your App. Copy that Key and Get back to Android Studio

Now Create an Application Class in your Android Project.

MyApplication.java

package com.example.firebasenotificationssample;

import android.app.Application;
import android.service.autofill.FillRequest;

import com.google.firebase.messaging.FirebaseMessaging;
import com.onesignal.OneSignal;

public class MyApplication extends Application {

    private static final String One_Signal_Id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";

    @Override
    public void onCreate() {
        super.onCreate();

        OneSignal.initWithContext(this);

        OneSignal.setAppId(One_Signal_Id);

    }
}

Note: Replace xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx with your Own OneSignal App ID.

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.firebasenotificationssample">

    <application
        android:name=".MyApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.FirebaseNotificationsSample">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

</manifest>

That’s it Guys. We Have Successfully Integrated OneSignal in Our Android App. Try to Add One Signal Notifications in your Android App.

For More Android Tutorials Click Here.

Leave a Reply

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

Back to top button
close