Created
January 26, 2018 10:15
-
-
Save Sloy/54f3e8f488e14459afb011b9fd23ee28 to your computer and use it in GitHub Desktop.
Utility class to show a debug notification from anywhere in your app
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object DebugNotification { | |
private const val DEBUG_CHANNEL_ID = "debug_channel" | |
@JvmStatic | |
@JvmOverloads | |
fun show(context: Context, title: String, text: String = "Debug notification") { | |
if (BuildConfig.DEBUG) { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |
createChannel(context) | |
} | |
val notification = NotificationCompat.Builder(context, DEBUG_CHANNEL_ID) | |
.setContentTitle(title) | |
.setContentText(text) | |
.setSmallIcon(R.drawable.notification_small) | |
.setColor(context.resources.getColor(R.color.accent)) | |
.build() | |
NotificationManagerCompat.from(context) | |
.notify("debug", (Math.random() * 100).toInt(), notification) | |
} | |
} | |
@TargetApi(Build.VERSION_CODES.O) | |
private fun createChannel(context: Context) { | |
context.getSystemService(NotificationManager::class.java).createNotificationChannel( | |
NotificationChannel(DEBUG_CHANNEL_ID, "Debug", NotificationManager.IMPORTANCE_LOW) | |
.apply { | |
description = "Debug notifications" | |
} | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment