Muhammad Ashraf
Muhammad Ashraf

Reputation: 1282

Pushing a Notification at specific time

I have an app that works as a Task Manager.. The user adds tasks and the app notifies him/her at the time he entered.. That works fine when I add just one task.. But when I add two tasks, The first one doesn't notify and the second one notifies with the data of the first one.. Example.. I add the first task with title of Task1 and the description of Desc1 On 11:05, And I add the second task with the title of Task2 and the description of Desc2 On 11:07.. On 11:05 Nothing happens.. But On 11:07 I get notification with Title of Task1 and description Desc1.. I have a custom class for Task..

public class Task {

String name,desc;
Date date;
Context context;

public Task(String name, String desc, Date date, Context context) {
    this.name = name;
    this.desc = desc;
    this.date = date;
    this.context = context;

    startAlarm(date.getTime(), name, desc);
}

public String toString() {
    return name + " : " + desc + " at " + date;
}

public void startAlarm(long when, String title, String descreption) {
    AlarmManager alarmManager = (AlarmManager) context
            .getSystemService(MainActivity.ALARM_SERVICE);

    Intent intent = new Intent(context, BGService.class);
    intent.putExtra("title", title);
    intent.putExtra("descreption", descreption);

    PendingIntent pendIntent = PendingIntent.getService(context,
            0, intent, 0);
    alarmManager.set(AlarmManager.RTC, when, pendIntent);
}
//// Getters And Setters Here .... 

}

the startAlarm method should be called whenever I create new Task.. It starts an alarm which ends at the time of the notification and the notification is shown..

But now just the last task is shown.. Why is that ? :S

Thanks :)

EDIT

I fixed It by following this answer:

How to set more than one alarms at a time in android?

Upvotes: 1

Views: 488

Answers (1)

akohout
akohout

Reputation: 1811

From Android docs for AlarmManager.set:

...If there is already an alarm scheduled for the same IntentSender, that previous alarm will first be canceled...

It seems like you can set only one alarm and need to create workarounds for multiple alarms, e.g. setting up a service that is called every X to set the next alarm.

Upvotes: 2

Related Questions