Skip to content

Enable Notifications

Jahir Fiquitiva edited this page Dec 14, 2023 · 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.

Note
OneSignal collects some data, and this should be reflected in the “Data safety” section of your app’s Play Store listing. In the Play Console, declare that your app collects data of the “Device or other IDs” type. See the OneSignal documentation and the Play Console documentation for more information.

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 {
            google()
            mavenCentral()
            gradlePluginPortal()
        }
        dependencies {
            classpath Plugins.oneSignal
        }
    }
    apply plugin: 'com.onesignal.androidsdk.onesignal-gradle-plugin'
    */
    
    ...
    
    dependencies {
        implementation(Libs.frames) {
            transitive = true
        }
        // TODO: Remove comment marks to enable
        // implementation Libs.oneSignal
    }
    
  • NotificationServiceExtension.kt (in package directory):

      /* TODO: Remove comment marks to enable
      import android.content.Context
      import androidx.core.app.NotificationCompat
      import com.onesignal.OSNotificationReceivedEvent
      import com.onesignal.OneSignal.OSRemoteNotificationReceivedHandler
      import dev.jahir.frames.extensions.context.color
      import dev.jahir.frames.extensions.context.drawable
      import dev.jahir.frames.extensions.context.preferences
    
      class NotificationServiceExtension : OSRemoteNotificationReceivedHandler {
          override fun remoteNotificationReceived(
              context: Context,
              notificationReceivedEvent: OSNotificationReceivedEvent
          ) {
              if(!context.preferences.notificationsEnabled) {
                  notificationReceivedEvent.complete(null)
                  return
              }
    
              val notification = notificationReceivedEvent.notification
              val mutableNotification = notification.mutableCopy()
              mutableNotification.setExtender { builder: NotificationCompat.Builder ->
                  builder.color = context.color(R.color.accent)
                  builder.setSmallIcon(R.drawable.ic_notification)
              }
              notificationReceivedEvent.complete(mutableNotification)
          }
      }
      */
  • MyApplication.kt (in package directory):

    // import com.onesignal.OneSignal
    // import com.onesignal.OSNotificationReceivedEvent
    // import dev.jahir.frames.extensions.context.preferences
    /*
    OneSignal.initWithContext(this);
    OneSignal.setAppId(BuildConfig.ONESIGNAL_APP_ID);
    OneSignal.setNotificationWillShowInForegroundHandler { notificationReceivedEvent: OSNotificationReceivedEvent ->
        notificationReceivedEvent.complete(
            if (preferences.notificationsEnabled)
                notificationReceivedEvent.notification
            else null
        )
    }
    OneSignal.unsubscribeWhenNotificationsAreDisabled(true)
    OneSignal.pauseInAppMessages(true)
    OneSignal.setLocationShared(false)
    */

In file app/src/main/AndroidManifest.xml and remove the comment marks at lines 97, 98 and 101.

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_notification.xml in app/src/main/res/drawable.