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

How does data persistence work in Android?

Data persistence in Android is the ability to save data to the device so that it can be accessed later, even if the app is closed or the device is restarted. There are several ways to achieve data persistence in Android

Fragments and Activities: Two Pillars of Android Development

Fragments and Activities are both important components of Android development. Fragments are a good choice for creating reusable and dynamic UIs, while Activities are a good choice for managing the lifecycle of an app’s UI and implementing core features.

Table of Contents

Send Us A Message