-
-
Notifications
You must be signed in to change notification settings - Fork 126
Enable Notifications
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.
Follow this tutorial to create a Firebase Project and set up OneSignal. Note down the Firebase Sender ID and OneSignal App ID.
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() */
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"
}
To set the notification icon, place a vector drawable called ic_notifications.xml in app/src/main/res/drawable.
Copyright 2022 by Jahir Fiquitiva. This work is licensed under the CreativeCommons Attribution-ShareAlike 4.0 International License.