2

I am trying to make an alert at a certain time to display a field of a Parcelable Java Object I made. I know there have been questions pertaining to this before, however, I have not found a solution to this problem. The Receiver will successfully make the alert if no data is passed to it, however, any data I send to it via an Intent from the Fragment that creates the Receiver is not maintained.

My code for the Receiver is below:

public class NotificationReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    IUser eu = intent.getExtras().getParcelable("USER");
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Intent repeating_intent = new Intent(new Intent(context, MainActivity.class));
    repeating_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(context, 100, repeating_intent, PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setContentIntent(pendingIntent)
            .setSmallIcon(android.R.drawable.arrow_up_float)
            .setContentTitle("Warning: Approaching Threshold")
            .setContentText(eu.get_forecast_info().get_estimate_cur_cycle_total_cost()+" is the price")
            .setAutoCancel(true);

    notificationManager.notify(100, builder.build());
    }
}

The code for supplying Intent for Receiver is below:

trigger_b = (Button) view.findViewById(R.id.trigger_button);
    trigger_b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.HOUR_OF_DAY, 10);
            calendar.set(Calendar.MINUTE, 14);
            calendar.set(Calendar.SECOND, 0);
            Intent intent = new Intent(getActivity().getApplicationContext(), NotificationReceiver.class);
            Bundle c = new Bundle();
            c.putParcelable("USER", eu);
            intent.putExtras(c);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity().getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
        }
    });

1 Answers1

2

With the latest versions of Android, you cannot reliably put a custom Parcelable or Serializable object in an Intent that is passed to AlarmManager. You will need to serialize the custom object into a byte[] and put the byte[] into the Intent. In onReceive() you will need to construct the object from the byte[] again.

See How to marshall and unmarshall a Parcelable to a byte array with help of Parcel? or https://gist.github.com/jacklt/6711967 for code examples.

Community
  • 1
  • 1
David Wasser
  • 93,459
  • 16
  • 209
  • 274