I'm working on a small project, which is a pill reminder app.
in my Setting Preference i have code like this
SettingsPreference.kt
.....
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.setting_preference, rootKey)
.....
notificationPreference?.setOnPreferenceChangeListener { preference, newValue ->
alarmReceiver = AlarmReceiver()
newValue?.let {
when (it as Boolean) {
true -> {
alarmReceiver.setMedicineScheduleAlarm(requireContext(), "21:42", "Test1")
alarmReceiver.setMedicineScheduleAlarm(requireContext(), "21:43", "Test2")
}
false -> {
//Cancel Alarm
}
}
}
true
}
}
and in my
AlarmReceiver.kt
class AlarmReceiver : BroadcastReceiver() {
.....
override fun onReceive(context: Context, intent: Intent) {
val medicineName = intent.getStringExtra(EXTRA_MEDICINE_NAME)
val notifId = NOTIF_ID
if (medicineName != null) {
showAlarmNotification(context, notifId)
}
}
fun setMedicineScheduleAlarm(
context: Context,
time: String,
medicineName: String
) {
val timeArray = time.split(":".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
val alarmID =
Integer.parseInt(timeArray[0]) + Integer.parseInt(timeArray[0]) + System.currentTimeMillis()
.toInt()
val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
val intent = Intent(context, AlarmReceiver::class.java)
intent.putExtra(EXTRA_MEDICINE_NAME, medicineName)
val calendar = Calendar.getInstance()
calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(timeArray[0]))
calendar.set(Calendar.MINUTE, Integer.parseInt(timeArray[1]))
calendar.set(Calendar.SECOND, 0)
val pendingIntent =
PendingIntent.getBroadcast(
context,
alarmID,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
)
alarmManager.setRepeating(
AlarmManager.RTC_WAKEUP,
calendar.timeInMillis,
AlarmManager.INTERVAL_DAY,
pendingIntent
)
Log.d("STATUS", "ALARM SET")
}
private fun showAlarmNotification(
context: Context,
notifId: Int
) {
val channelId = "Channel_1"
val channelName = "AlarmManagerChannel"
val notificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val builder = NotificationCompat.Builder(context, channelId)
.setSmallIcon(R.drawable.ic_notification_secondary_solid)
.setContentTitle("time to take medicine")
.setContentText("Don't forget to take your medicine")
.setColor(ContextCompat.getColor(context, android.R.color.transparent))
.setVibrate(longArrayOf(1000, 1000, 1000, 1000, 1000))
.setSound(alarmSound)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
channelId,
channelName,
NotificationManager.IMPORTANCE_DEFAULT
)
channel.enableVibration(true)
channel.vibrationPattern = longArrayOf(1000, 1000, 1000, 1000, 1000)
builder.setChannelId(channelId)
notificationManager.createNotificationChannel(channel)
}
val notification = builder.build()
notificationManager.notify(notifId, notification)
}
}
the above code runs normally but, only the last alarm works and runs the notification. what is wrong with my code? even the requestCode for pending intent is different for each alarm
thanks in advance