BaseNotification.kt 3.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package io.r_a_d.radio2
  2. import android.app.Notification
  3. import android.app.NotificationChannel
  4. import android.app.NotificationManager
  5. import android.app.PendingIntent
  6. import android.content.Context
  7. import android.content.Intent
  8. import android.os.Build
  9. import androidx.annotation.RequiresApi
  10. import androidx.core.app.NotificationCompat
  11. abstract class BaseNotification(private val notificationChannelId: String,
  12. private val notificationChannel : Int,
  13. private val notificationId: Int,
  14. private val notificationImportance: Int
  15. ) {
  16. // ########################################
  17. // ########## BASE NOTIFICATION ###########
  18. // ########################################
  19. // Define the notification in android's swipe-down menu
  20. lateinit var notification: Notification
  21. protected lateinit var notificationManager: NotificationManager
  22. protected lateinit var builder: NotificationCompat.Builder
  23. @RequiresApi(api = Build.VERSION_CODES.O)
  24. protected fun createNotificationChannel(c: Context): String {
  25. val chanName = notificationChannel
  26. val notificationChannelImportance =
  27. when(notificationImportance) {
  28. NotificationCompat.PRIORITY_LOW -> NotificationManager.IMPORTANCE_LOW
  29. NotificationCompat.PRIORITY_DEFAULT -> NotificationManager.IMPORTANCE_DEFAULT
  30. NotificationCompat.PRIORITY_HIGH-> NotificationManager.IMPORTANCE_HIGH
  31. NotificationCompat.PRIORITY_MAX -> NotificationManager.IMPORTANCE_MAX
  32. NotificationCompat.PRIORITY_MIN -> NotificationManager.IMPORTANCE_MIN
  33. else -> NotificationManager.IMPORTANCE_DEFAULT
  34. }
  35. val chan = NotificationChannel(this.notificationChannelId, c.getString(chanName), notificationChannelImportance)
  36. chan.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
  37. notificationManager.createNotificationChannel(chan)
  38. return this.notificationChannelId
  39. }
  40. fun show()
  41. {
  42. notification = builder.build()
  43. notificationManager.notify(notificationId, notification)
  44. }
  45. open fun create(c: Context) {
  46. notificationManager = c.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
  47. val notificationIntent = Intent(c, MainActivity::class.java)
  48. // The PendingIntent will launch the SAME activity
  49. // thanks to the launchMode specified in the Manifest : android:launchMode="singleTop"
  50. val pendingIntent = PendingIntent.getActivity(
  51. c, 0,
  52. notificationIntent, 0
  53. )
  54. var channelID = ""
  55. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  56. channelID = createNotificationChannel(c)
  57. }
  58. builder = NotificationCompat.Builder(c, channelID)
  59. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  60. builder.setSmallIcon(R.drawable.lollipop_logo)
  61. builder.color = -0x20b3c6
  62. } else {
  63. builder.setSmallIcon(R.drawable.normal_logo)
  64. }
  65. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  66. builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
  67. }
  68. builder.priority = notificationImportance
  69. // The PendingIntent will launch the SAME activity
  70. // thanks to the launchMode specified in the Manifest : android:launchMode="singleTop"
  71. builder.setContentIntent(pendingIntent)
  72. builder.setColorized(true)
  73. }
  74. fun clear()
  75. {
  76. notificationManager.cancel(notificationId)
  77. }
  78. }