Repository navigation
Enable Notifications
You can send notifications to your users, for example when you add new wallpapers. Blueprint uses OneSignal for this, together with Firebase.
This is completely optional. If you don't need notifications, skip this page.
Important
OneSignal collects some data, and you have to declare it in the "Data safety" section of your Play Store listing: your app collects "Device or other IDs". See the OneSignal documentation and the Play Console documentation.
Make sure your package name is set up: see Set Package Name. Notifications rely on it.
Follow OneSignal's guide to create a Firebase project and a OneSignal app. Write down two values:
- Your OneSignal App ID
- Your Firebase Sender ID (also called project number)
Open buildSrc/src/main/java/OneSignal.kt and replace both values with yours:
object OneSignal {
const val appId = "your-onesignal-app-id"
const val googleProjectNumber = "your-firebase-sender-id"
}The notifications code is already in your project, but it's turned off with comment marks. You'll remove them in four files.
Comment marks look like this:
-
//at the start of a line turns off that one line. -
/*and*/turn off everything between them.
Each place you need to change is marked with a TODO: Remove comment marks to enable comment. To find them all, open Edit → Find → Find in Files and search for Remove comment marks to enable.
Remove the // before implementation:
dependencies {
implementation(Libs.blueprint) {
transitive = true
}
// TODO: Remove comment marks to enable
implementation Libs.oneSignal
}Remove the // from the three import lines, and the /* and */ around the OneSignal code inside onCreate. It should end up like this:
import com.onesignal.OneSignal
import com.onesignal.OSNotificationReceivedEvent
import dev.jahir.frames.extensions.context.preferences
class MyApplication : FramesApplication(BuildConfig.ONESIGNAL_APP_ID) {
override fun onCreate() {
super.onCreate()
OneSignal.initWithContext(this);
OneSignal.setAppId(BuildConfig.ONESIGNAL_APP_ID);
// ...the rest of the OneSignal code
}
}Remove the /* near the top and the */ at the end of the file.
Then check the line that imports R. It must use your package name:
import com.yourname.yourapp.RIn the Blueprint sample it says dev.jahir.kuper.app.R, so you need to change it.
Near the end of the file, remove the <!-- line and the --> line around the OneSignal meta-data:
<!-- OneSignal Notifications Service -->
<meta-data android:name="com.onesignal.NotificationServiceExtension"
android:value="${applicationId}.NotificationServiceExtension" />- Click File → Sync Project with Gradle Files.
- Run the app on your phone or emulator and allow notifications when it asks.
- In your OneSignal dashboard, send a test notification. It should show up on your device. 🔔
The small icon in the status bar comes from app/src/main/res/drawable/ic_notification.xml. To use your own, replace that file with a vector drawable with the same name. Android shows it as a white silhouette, so use a simple shape.
-
"Unresolved reference: R" in
NotificationServiceExtension.ktThe
import ... .Rline still points to another package. Change it to your package name, as in step 3. -
"Unresolved reference: OneSignal"
implementation Libs.oneSignalinapp/build.gradleis still commented out, or you didn't sync after removing the//. -
The test notification never arrives
Check that the App ID in
OneSignal.ktmatches the one in your OneSignal dashboard, that you allowed notifications on the device, and that notifications are turned on in the app's settings.
Note
Would you like to help improve this wiki?
It is advised that you contribute to jahirfiquitiva/dashboards-wikis, which is the repository that holds the contents of this wiki. Please open an issue or a pull request in that repo instead.
Wiki by Jahir Fiquitiva, with contributions from Patryk Goworowski, Lumiq Creative and Jackson Hayes. Special thanks to Sanchith Hegde.
Licensed under the Creative Commons Attribution-ShareAlike 4.0 International License.