1

Trying to have a recurring notification.

Create a repeating pending Intent using AlarmManager as below and add the serializable extra (newHabit is an instance of my own class). newHabit.getNotificationID() returns an int based on the timestamp of when the instance was created so unlikely that this is an intent used elsewhere

Intent notifyIntent = new Intent(this, ReminderReceiver.class);
                    // tried this
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("habit", newHabit);

                    notifyIntent.putExtras(bundle);
                    //then this
                    notifyIntent.putExtra("habit",newHabit);

                    PendingIntent pendingIntent = PendingIntent.getBroadcast( this,
                            newHabit.getNotificationID(), notifyIntent,
                                    PendingIntent.FLAG_UPDATE_CURRENT);

                    Calendar calendar = Calendar.getInstance();
                    calendar.set(Calendar.HOUR_OF_DAY, reminderTime.getHour());
                    calendar.set(Calendar.MINUTE, reminderTime.getMinute());
                    calendar.set(Calendar.SECOND, 0);

                    AlarmManager alarmManager =
                            (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
                    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,  calendar.getTimeInMillis(),
                            AlarmManager.INTERVAL_DAY, pendingIntent);

Then in recieving BroadcastReciever use the following code to get extras

        Habit habit = null;

        Bundle extras = intent.getExtras();
        if (extras != null) {
            habit = (Habit) extras.get("habit");

        }

This works correctly up to API 24 and after that habit in the reciever is always null.

peewee6765
  • 49
  • 5

1 Answers1

1

Based on this answer: Pass Serializable Object to Pending Intent , there are problems using AlarmManager and passing Serializable Object

As with the second answer in above question, converting the Serializable object to a byte[] works

peewee6765
  • 49
  • 5