How to Create a Distinct PendingIntents

49 views Asked by At

I have an app that allows a user to create a reminder to alert them in the future. The issue I am running into, is that as a user creates more than one reminder, the pending intent only triggers on the latest one created, with the earliest reminder content to display, ie:

Task 1, set for 4pm

Task 2, set for 5pm

Task 3, set for 6pm

No reminder will appear until 6pm, which will display Task 1.

I believe it has to due with the lack of uniqueness of the PendingIntent:

public class ReminderManager {

    private Context mContext; 
    private AlarmManager mAlarmManager;

    public ReminderManager(Context context) {
        mContext = context; 
        mAlarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    }

    public void setReminder(Long taskId, Calendar when) {

        Intent i = new Intent(mContext, OnAlarmReceiver.class);
        i.putExtra(RemindersDbAdapter.KEY_ROWID, (long)taskId); 

        PendingIntent pi = PendingIntent.getBroadcast(mContext, 0, i, PendingIntent.FLAG_ONE_SHOT); 

        mAlarmManager.set(AlarmManager.RTC_WAKEUP, when.getTimeInMillis(), pi);
    }
}

How can I ensure that each reminder is unique and gets its correct time on time?

1

There are 1 answers

0
Ali On BEST ANSWER

You need to provide something other than 0 for the requestCode param. Try making it a unique ID for each request.