2019-06-04 23:14:19 +00:00
|
|
|
package com.adlerosn.brasilfurfest.helper
|
|
|
|
|
|
|
|
import android.app.NotificationChannel
|
|
|
|
import android.app.NotificationManager
|
|
|
|
import android.content.Context
|
|
|
|
import android.os.Build
|
2019-06-22 04:04:57 +00:00
|
|
|
import androidx.annotation.StringRes
|
|
|
|
import androidx.core.app.NotificationCompat
|
2019-06-04 23:14:19 +00:00
|
|
|
|
|
|
|
inline fun <reified KClass: Context> KClass.notificationBuilderCompat(@StringRes notificationChannelString: Int, important: Boolean): NotificationCompat.Builder =
|
|
|
|
getNotificationChannelBuilder(this, KClass::class.java.simpleName, notificationChannelString, important)
|
|
|
|
|
|
|
|
fun getNotificationChannelBuilder(context: Context, SIMPLE_ID: String, @StringRes notificationChannelString: Int, important: Boolean) =
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
|
val notificationManager = context.getSystemService(NotificationManager::class.java) as NotificationManager
|
|
|
|
if (notificationManager.getNotificationChannel(SIMPLE_ID) == null) {
|
|
|
|
val channelName = context.getString(notificationChannelString)
|
|
|
|
val channel = NotificationChannel(SIMPLE_ID, channelName, if (important) NotificationManager.IMPORTANCE_HIGH else NotificationManager.IMPORTANCE_LOW)
|
|
|
|
notificationManager.createNotificationChannel(channel)
|
|
|
|
}
|
|
|
|
NotificationCompat.Builder(context, SIMPLE_ID)
|
|
|
|
} else {
|
|
|
|
@Suppress("DEPRECATION") //older API will have this
|
|
|
|
NotificationCompat.Builder(context)
|
|
|
|
}
|