Skip to content

Instantly share code, notes, and snippets.

@januprasad
Created February 17, 2020 15:17
Show Gist options
  • Save januprasad/6c2b33eff28b46513971ff0f733fff3b to your computer and use it in GitHub Desktop.
Save januprasad/6c2b33eff28b46513971ff0f733fff3b to your computer and use it in GitHub Desktop.
class NotificationUtils(base: Context) : ContextWrapper(base) {
val MYCHANNEL_ID = "App Alert Notification ID"
val MYCHANNEL_NAME = "App Alert Notification"
private var manager: NotificationManager? = null
init {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createChannels()
}
}
// Create channel for Android version 26+
@TargetApi(Build.VERSION_CODES.O)
private fun createChannels() {
val channel = NotificationChannel(MYCHANNEL_ID, MYCHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH)
channel.enableVibration(true)
getManager().createNotificationChannel(channel)
}
// Get Manager
fun getManager() : NotificationManager {
if (manager == null) manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
return manager as NotificationManager
}
fun getNotificationBuilder(): NotificationCompat.Builder {
val intent = Intent(this, MainActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_CLEAR_TASK
}
val pendingIntent = PendingIntent.getActivity(this, 0, intent, 0)
return NotificationCompat.Builder(applicationContext, MYCHANNEL_ID)
.setContentTitle("Alarm!")
.setContentText("Your AlarmManager is working.")
.setSmallIcon(R.drawable.ic_alarm_blue)
.setColor(Color.YELLOW)
.setContentIntent(pendingIntent)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setAutoCancel(true)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment