August 13, 2021

Android – Implement Onesignal (Alternative for firebase)

By lj007

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()
   }
}