Issue
Upon initialization, my viewmodel creates an alarm. It is set to ring 10 seconds after the application is launched. The alarm manager receives a pendingIntent. The pendingIntent receives an intent. The intent is a broadcast receiver. The intent contains extras which bring me to my problem. Below is the relevant code for my viewmodel:
MainActivityViewModel.kt
init {
val intent =
Intent(app, TimetableAlarmReceiver::class.java).also {
val alarmMessage =
"message"
it.putExtra("message", alarmMessage)
Timber.i("The extra message is $alarmMessage")
it.putExtra("dayOfWeek", getTodayEnumDay())
Timber.i("The extra dayOfWeek is ${getTodayEnumDay()}")
}
Timber.i("The intent is $intent")
val pendingIntent = PendingIntent.getBroadcast(
app,
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
)
val alarmManager = app.getSystemService(Context.ALARM_SERVICE) as AlarmManager
alarmManager.setExact(
AlarmManager.RTC_WAKEUP,
System.currentTimeMillis().plus(10000L),
pendingIntent
)
}
The broadcast receiver does not detect any extras:
TimetableAlarmReceiver.kt
override fun onReceive(context: Context, intent: Intent?) {
Timber.i("The receiver has been called")
val notificationManager = ContextCompat.getSystemService(
context,
NotificationManager::class.java
) as NotificationManager
if(intent!=null){
Timber.i("The extras are:${intent.extras}")
val message = intent.getStringExtra("message")
Timber.i("The extra message is $message")
val dayOfWeek = intent.getSerializableExtra("dayOfWeek") as DayOfWeek?
Timber.i("The extra dayOfWeek is $dayOfWeek")
}
The irony of the logs:
MainActivityViewModel.kt Logs
2021-04-14 17:42:38.684 5492-5492/com.example.android.mycampusapp I/MainActivityViewModel: The extra message is message
2021-04-14 17:42:38.687 5492-5492/com.example.android.mycampusapp I/MainActivityViewModel: The extra dayOfWeek is WEDNESDAY
2021-04-14 17:42:38.687 5492-5492/com.example.android.mycampusapp I/MainActivityViewModel: The intent is Intent { cmp=com.example.android.mycampusapp/.timetable.receiver.TimetableAlarmReceiver (has extras) }
TimetableAlarmReceiver.kt logs:
2021-04-14 17:53:38.884 5814-5814/com.example.android.mycampusapp I/TimetableAlarmReceiver: The receiver has been called
2021-04-14 17:53:38.885 5814-5814/com.example.android.mycampusapp I/TimetableAlarmReceiver: The extras are:Bundle[EMPTY_PARCEL]
2021-04-14 17:53:38.886 5814-5814/com.example.android.mycampusapp I/TimetableAlarmReceiver: The extra message is null
2021-04-14 17:53:38.886 5814-5814/com.example.android.mycampusapp I/TimetableAlarmReceiver: The extra dayOfWeek is null
I have tried a variety of solutions here on stack overflow that involve a receiver that has been called successfully. A bunch of them involve changing the pending intent flag, but that is not helpful here. I am using the correct flag PendingIntent.FLAG_UPDATE_CURRENT. In any case, changing the flag to Intent.FILL_IN_DATA or PendingIntent.FLAG_CANCEL_CURRENT doesn't work.
Solution
Ah. You can't put custom serializable objects as "extras" in an Intent
that you send to AlarmManager
. You can only put know types in there. If you need a custom type then you will need to serialize the custom object into a byte array and add that to your Intent
as an "extra".
Answered By - David Wasser
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.