I am trying to schedule SMS messages in my app, using alarm manager with the RTC_WAKEUP type so that it will fire even when the device is in doze mode (as it needs to be exact). I have had reports of it not working and in my own testing it seems that if I for example schedule a message for an hour ahead it will fire, but then scheduling for the next day does not fire. (I have verified that the times being scheduled are correct).
Here is the code for the scheduling:
fun scheduleTimeMessage(activity: Context, time: Long) {
val manager = activity.getSystemService(Context.ALARM_SERVICE) as AlarmManager
val intent = Intent(activity, TimeSendReceiver::class.java)
intent.putExtra("TIME", time)
Log.e("ERROR?", "Time is " + time + SimpleDateFormat(" dd/MM/yy hh:mm", Locale.US).format(Date(time)))
if (Build.VERSION.SDK_INT >= 23)
manager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, time, PendingIntent.getBroadcast(activity, time.toInt(), intent, PendingIntent.FLAG_UPDATE_CURRENT))
else
manager.setExact(AlarmManager.RTC_WAKEUP, time, PendingIntent.getBroadcast(activity, time.toInt(), intent, PendingIntent.FLAG_UPDATE_CURRENT))
}
Here is the BroadcastReceiver:
class TimeSendReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val epoch = intent.getLongExtra("TIME", 0)
val database = SchedulerDatabase(context)
val cursor = database.getTimeCursor(epoch)
if (cursor.moveToFirst()) {
do {
if (cursor.getString(cursor.getColumnIndex("Address")).contains(","))
sendGroupMessage(context, cursor)
else {
if (cursor.getString(cursor.getColumnIndex("Image")) == "")
sendSMSMessage(context, cursor)
else
sendMMSMessage(context, cursor)
}
} while (cursor.moveToNext())
database.deleteMessageData(epoch)
cursor.close()
}
}
}