Android 7 BroadcastReceiver onReceive intent.getExtras missing data

Android O version doesn’t get extras properly in BroadcastReceivers. But the one great solution is to use an intent’s setAction(String action) method to send the serializable Alarm object. Then get it back to object in onReceive.
Here is the sample:

    Intent intent = new Intent(context, AlarmReceiver.class);
    intent.setAction(new Gson().toJson(alarm));

Then in your Alarm’s receiver

public void onReceive(Context context, Intent intent) {

    String alarmSerializable = intent.getAction();
    if (alarmSerializable != null)
        alarm = new Gson().fromJson(alarmSerializable, Alarm.class);

    //TODO next, send notification
}

This is the common way for Android O and others.

Leave a Comment