Skip to content

Enable Notifications

Lumiq Creative edited this page Jul 15, 2020 · 11 revisions

You can send your users notifications via OneSignal and Firebase.

Note
Do not sync, clean, or rebuild your project while setting up notifications in code.

Configure Platforms

Follow this tutorial to create a Firebase Project and set up OneSignal. Note down the Firebase Sender ID and OneSignal App ID.

Uncomment Code

Uncomment the following code by removing the comment marks (/*, */, and //).

  • app/build.gradle:

    /*
    buildscript {
        repositories {
            maven { url 'https://plugins.gradle.org/m2/' }
        }
        dependencies {
            classpath Plugins.oneSignal
        }
    }
    apply plugin: 'com.onesignal.androidsdk.onesignal-gradle-plugin'
    */
  • NotificationService.kt (in package directory):

    /* TODO: Remove comment marks to enable
    import com.onesignal.NotificationExtenderService
    import com.onesignal.OSNotificationReceivedResult
    import dev.jahir.frames.extensions.context.preferences
    class NotificationService : NotificationExtenderService() {
        override fun onNotificationProcessing(notification: OSNotificationReceivedResult?): Boolean =
            !preferences.notificationsEnabled
    }
    */
  • MyApplication.kt (in package directory):

    // import com.onesignal.OneSignal
    /*
    OneSignal.startInit(this)
        .inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
        .unsubscribeWhenNotificationsAreDisabled(true)
        .init()
    */

Link APIs

In buildSrc/src/main/java/OneSignal.kt, replace the value of appId with your OneSignal App ID, and the value of googleProjectNumber with your Firebase Sender ID. Clean and rebuild the project.

object OneSignal {
    const val appId = "965921b5-91a2-45b8-a960-a9f28948a781"
    const val googleProjectNumber = "191533712411"
}

Set Notification Icon

To set the notification icon, place a vector drawable called ic_notifications.xml in app/src/main/res/drawable.