TheJadav

Android – Implement Onesignal (Alternative for firebase)

Onesignal is one of the best alternative for firebase push notification. It is easy to use, free for push notification, however it has 30K limit for subscribers. Onesignal almost support all kind of operating systems.It has 860K+ users, who are currenlty using onesignal. For more you can visit its official site.

Onesignal

Onesignal is one of the best alternative for firebase push notification. It is easy to use, free for push notification, however it has 30K limit for subscribers. Onesignal almost support all kind of operating systems.It has 860K+ users, who are currenlty using onesignal. For more you can visit its official site.

Implement Onesignal in Android

To implement onesignal we need to just impport it in android via gradle and initialze from our application class.

  • We need to create A OneSignal Account, from there we will get our appID.
  • We also need to add project in firebase, get firebase server key from there.

Add onesignal dependencies

Top level build.gredle

buildscript {
    repositories {
        maven { url 'https://plugins.gradle.org/m2/'}
    }
    dependencies {
        classpath 'gradle.plugin.com.onesignal:onesignal-gradle-plugin:[0.12.4, 0.99.99]'
    }
}
apply plugin: 'com.onesignal.androidsdk.onesignal-gradle-plugin'

repositories {
    maven { url 'https://maven.google.com' }
}

app’s build.gradle

//add defaultConfig
android {
   defaultConfig {
      manifestPlaceholders = [
          onesignal_app_id: 'PUT YOUR ONESIGNAL APP ID HERE',
          // Project number pulled from dashboard, local value is ignored.
          onesignal_google_project_number: 'REMOTE'
      ]
    }
 }

dependencies {
    implementation 'com.onesignal:OneSignal:[3.11.2, 3.99.99]'
}

It is finished. Now we only need to subscribe our app to onesignal, and onesignal will automatically send push notification. We don’t need to manage deviceToken as like firebase and we also don’t need messaging service if we are not performing any click. We can direclty add image from server in notification, display will be handled by onesignal.

Subscribe to onesignal

import com.onesignal.OneSignal

class YourAppClass : Application() {
   override fun onCreate() {
      super.onCreate()
      
      // OneSignal Initialization
      OneSignal.startInit(this)
         .inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
         .unsubscribeWhenNotificationsAreDisabled(true)
         .init()
   }
}

Share this content:

Share:

More Posts

Introduction to Kotlin: A Versatile and Modern Programming Language

Kotlin, a versatile and modern programming language, offers developers a concise, safe, and interoperable coding experience. With features like null safety, extension functions, and coroutines, Kotlin enhances productivity and readability, making it an attractive choice for Android development and beyond. Whether you’re new to programming or an experienced developer, exploring Kotlin opens up a world of possibilities for building robust and expressive applications.

Mastering the Android Activity Lifecycle: A Comprehensive Guide

The Android Activity Lifecycle is a fundamental concept for Android developers. It defines how an Activity behaves during its lifecycle, from creation to destruction. Understanding the Activity Lifecycle is crucial for managing resources efficiently and delivering a smooth user experience. In this blog post, we’ll briefly introduce the different states an Activity can be in and the main callback methods associated with each state. Let’s dive in and explore this important aspect of Android app development!

Table of Contents

Send Us A Message