OneSignal Android Push Notification — Android tutorials 2023
How to send push notifications using OneSignal in an Android Studio project, following these step-by-step instructions.
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.
Adding Dependencies in the build.gradle(project) file
buildscript {
repositories {
google()
jcenter()
mavenCentral()
gradlePluginPortal()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.0.4'
classpath 'com.google.gms:google-services:4.3.10'
// add below line in dependencies section
// classpath 'gradle.plugin.com.onesignal:onesignal-gradle-plugin:[0.14.0, 0.99.99]'
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
}
}
Adding Dependencies in the build.gradle(Module app) file
apply plugin: 'com.android.application'
apply plugin: 'com.onesignal.androidsdk.onesignal-gradle-plugin'
dependencies {
implementation 'com.onesignal:OneSignal:[4.0.0, 4.99.99]'
}
Create a ApplicationClass
import android.app.Application;
import com.onesignal.OneSignal;
public class ApplicationClass extends Application {
// Replace the below with your own ONESIGNAL_APP_ID
private static final String ONESIGNAL_APP_ID = "ONESIGNAL_APP_ID";
@Override
public void onCreate() {
super.onCreate();
// Verbose Logging set to help debug issues, remove before releasing your app.
// OneSignal.getDebug().setLogLevel(LogLevel.VERBOSE);
// OneSignal Initialization
OneSignal.initWithContext(this);
// optIn will show the native Android notification permission prompt.
// We recommend removing the following code and instead using an In-App Message to prompt for notification permission (See step 7)
// OneSignal.getUser().getPushSubscription().optIn();
OneSignal.setAppId(ONESIGNAL_APP_ID);
}
}
Menifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="cloud.micampsbay">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
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/AppTheme"
android:name=".ApplicationClass"
android:networkSecurityConfig="@xml/network_security_config"
android:usesCleartextTraffic="true"
tools:ignore="GoogleAppIndexingWarning"
tools:targetApi="n">
<activity android:name="cloud.micampsbay.activity.SplashActivity"
android:exported="true"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="cloud.micampsbay.activity.MainActivity"
android:exported="true"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Here’s the official documentation link for OneSingle-platform
To learn Android app development, I recommend visiting the YouTube channel “CodingWithDev.” This channel offers content suitable for both beginners and advanced learners.