How To Integrate onesignal push notification in Android
Android DevelopmentStep: 1 Create a new Project in Android Studio
First, create a new Android Studio project and add the dependencies and plugin. First set up OneSignal in your project.
Add OneSignal dependency and plugin to your build.Gradle app-level.
Add OneSignal dependency and plugin to your build.Gradle app-level.
apply plugin: 'com.onesignal.androidsdk.onesignal-gradle-plugin'
dependencies {
implementation 'com.onesignal:OneSignal:3.12.6'
}
end add OneSignal dependency to your build.Gradle project-level.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.0'
classpath 'gradle.plugin.com.onesignal:onesignal-gradle-plugin:[0.12.4, 0.99.99]'
}
}
Step: 2 Create a new OneSignal Project
- login your OneSignal Account than create an app
- Enter Your Android Application Name & Select platform Google Android(FCM)
- Generate a Firebase Server Key Read the documentation.
- Enter your Firebase Server Key & Firebase Sender ID.
Step: 3 Add Your Created Onesignal App ID
Add OneSignl default config to your build.Gradle app-level.
android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
defaultConfig {
applicationId "com.myapplication.app"
minSdkVersion 17
targetSdkVersion 30
versionCode 1
versionName "1.0.0"
multiDexEnabled true
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
manifestPlaceholders = [onesignal_app_id: "Enter Your App ID",]
}
buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
The next step is to create the class MyApplication & Declare the class MyApplication in AndroidManifest.xml.
import android.app.Application;
import android.content.Context;
import androidx.multidex.MultiDex;
import com.onesignal.OneSignal;
public class MyApplication extends Application {
public static Context context;
@Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
context = this;
OneSignal.startInit(this) .inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
.unsubscribeWhenNotificationsAreDisabled(true)
.init();
}
public static Context getContext() {
return context;
}
}
Declare the class MyApplication in AndroidManifest.xml.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myapplication.app">
<uses-permission android:name="android.permission.INTERNET"/
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:name="com.myapplication.app.MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:requestLegacyExternalStorage="true"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name="com.myapplication.app.activity.SplashActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>