Skip to content

Instantly share code, notes, and snippets.

@Sloy
Created January 26, 2018 10:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sloy/54f3e8f488e14459afb011b9fd23ee28 to your computer and use it in GitHub Desktop.
Save Sloy/54f3e8f488e14459afb011b9fd23ee28 to your computer and use it in GitHub Desktop.
Utility class to show a debug notification from anywhere in your app
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