25

I'm having a problem with alarmManager and the pending intent with extras that will go along with it.

If I set multiple alarms, they will go off, however the extras stay the same.

I have already read into these questions:

and I have tried:

  • assigning a unique ID to each pending intent and
  • using all the pending intent flags,

all to no avail. I have no clue why it will not work.

Here is a code snippet:

Intent intent = new Intent(con,
                    AppointmentNotificationReciever.class);
            intent.putExtra("foo", bar.toString());


            int id = randomNum;

            PendingIntent sender = PendingIntent.getBroadcast(con, id,
                    intent, PendingIntent.FLAG_UPDATE_CURRENT);


            AlarmManager am = (AlarmManager) con.getSystemService(Context.ALARM_SERVICE);
            am.set(AlarmManager.RTC_WAKEUP, scheduleExecution, sender);
Community
  • 1
  • 1
ninjasense
  • 13,382
  • 17
  • 71
  • 91

3 Answers3

69

Possibly two different issues here:

1) If you've already created your PendingIntent before and it "matches" an existing PendingIntent, then you must specify the PendingIntent.FLAG_UPDATE_CURRENT flag or it will not pass the extras. A "match" is based on the criteria that Intent.filterEquals() uses, so definitely read the docs there and make sure you understand the data, action, type, etc.

2) I've read that if you do NOT set an action on your intent, then it will not propagate the extras, so perhaps try intent.setAction("com.blah.Action").

DustinB
  • 10,606
  • 5
  • 42
  • 54
  • In my pending intent I try to set the PendingIntent.FLAG_UPDATE_CURRENT, but now when the intent is called, I get an error like "java.lang.IllegalArgumentException: Can't use FLAG_RECEIVER_BOOT_UPGRADE here" - no idea though where this BOOT flag is coming from, all I added wa the FLAG_UPDATE_CURRENT. Did you have similar issue before? (I'm on Huawei Ideos, 2.2) – Mathias Conradt May 08 '11 at 03:55
  • This one post explained several issues I was seeing, great job! – Nemanja Kovacevic May 22 '14 at 14:10
  • Really helpful for this annoying and misleading error. Usage of both flag and setting action worked for me – Umar Qureshi Oct 20 '14 at 16:52
  • I used different requestCode values to make filterEquals treat them as different intent, as suggested here: http://stackoverflow.com/questions/7496603/how-to-create-different-pendingintent-so-filterequals-return-false – Daniel Lubarov Sep 29 '16 at 17:29
12

I've run into a similar problem. Using PendingIntent.FLAG_ONE_SHOT may solve the problem, because it means the PendingActivity won't be reused.

Robbie Matthews
  • 1,140
  • 10
  • 18
2

This could be due to Activity::getIntent returning the Activity's original intent given certain intent flags/filters.

If that is the case for you, you'll need to look at Activity::onNewIntent. Override that method, and the intent passed to that function should be the new intent with proper extras, etc.

Credit goes to this SO question that helped me to solve my problem: Why is my searchable activity's Intent.getAction() null?

Community
  • 1
  • 1
brack
  • 659
  • 7
  • 14