Jignesh Patel Jignesh Patel - 4 months ago 18
Android Question

trigger an alarm on this date i.e 03-02-2014 04:50 PM

I have a

AlarmManager
that triggers different
notifications
on various days. It displays the
notification
of past dates but doesn't show the
notifications
for future date time.
I tried putting hard coded string then it works fine, but when i get the dates from server then the alarm manager doesn't fire. Any kind of help is greatly appreciated.
Here is My Code:

ReminderAlarmReceiver.java

public class ReminderAlarmReceiver extends BroadcastReceiver {

public static final String REMINDER = "reminder";
@Override
public void onReceive(Context context, Intent intent) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, REMINDER);
// Acquire the lock
wakeLock.acquire();

Intent service = new Intent(context, ReminderService.class);
service.putExtra("id", intent.getStringExtra("id"));
service.putExtra("subject", intent.getStringExtra("subject"));
service.putExtra("description", intent.getStringExtra("description"));
context.startService(service);

// Release the lock
wakeLock.release();
}

public void setReminder(Context context, List<PlannerData> planner) {

SimpleDateFormat sdf = new SimpleDateFormat(AttributeSet.Constants.DATE_FORMAT);
Calendar calendar = Calendar.getInstance();
for (PlannerData data : planner) {
try {
Date d = sdf.parse(data.getReminderDate().trim());
calendar.setTime(d);
long when = calendar.getTimeInMillis(); // Notification Time
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

Intent intent = new Intent(context, ReminderAlarmReceiver.class);
intent.putExtra("id", String.valueOf(planner.indexOf(data)));
intent.putExtra("subject", data.getSubject());
intent.putExtra("description", data.getDescription());
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
am.set(AlarmManager.RTC_WAKEUP, when, pendingIntent);

} catch (ParseException e) {
e.printStackTrace();
}

}
}


public void cancelAlarm(Context context) {
Intent intent = new Intent(context, ReminderAlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
}

}


ReminderService.java

public class ReminderService extends IntentService {

private static final int NOTIFY_ID = 1;

public ReminderService() {
super("ReminderService");
}

@Override
protected void onHandleIntent(Intent intent) {


NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.yellow)
.setContentTitle(intent.getStringExtra("subject") != null ? intent.getStringExtra("subject") : "")
.setContentText(intent.getStringExtra("description") != null ? intent.getStringExtra("description") : "");

// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, PlannerActivity.class);

// The stack builder object will contain an artificial back stack for the started Activity
// This ensures that navigating backward from the Activity leads out of your application to the Home Screen
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(PlannerActivity.class);

resultIntent.setAction(intent.getStringExtra("id"));

// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(resultPendingIntent);

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
long timeInMilis = System.currentTimeMillis();

Notification notification = builder.build();
notification.when = timeInMilis;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(Integer.parseInt(intent.getStringExtra("id")), notification);

}
}


And my date format is :

public static final String DATE_FORMAT = "dd-MM-yyyy hh:mm a";

Answer

I got my mistake. For setting multiple alarms I need to create my PendingIntent s with different requestCode. Below is my updated source code:

ReminderAlarmReceiver.java

public class ReminderAlarmReceiver extends BroadcastReceiver {

public static final String REMINDER = "reminder";
@Override
public void onReceive(Context context, Intent intent) {
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, REMINDER);
    // Acquire the lock
    wakeLock.acquire();

    Intent service = new Intent(context, ReminderService.class);
    service.putExtra("id", intent.getStringExtra("id"));
    service.putExtra("subject", intent.getStringExtra("subject"));
    service.putExtra("description", intent.getStringExtra("description"));
    context.startService(service);

    // Release the lock
    wakeLock.release();
}

public void setReminder(Context context, List<PlannerData> planner) {

    SimpleDateFormat sdf = new SimpleDateFormat(AttributeSet.Constants.DATE_FORMAT);
    Calendar calendar = Calendar.getInstance();
    for (PlannerData data : planner) {
        try {
            Date d = sdf.parse(data.getReminderDate().trim());
            calendar.setTime(d);
            long when = calendar.getTimeInMillis();     // Notification Time
            AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

            Intent intent = new Intent(context, ReminderAlarmReceiver.class);
            intent.putExtra("id", String.valueOf(planner.indexOf(data)));
            intent.putExtra("subject", data.getSubject());
            intent.putExtra("description", data.getDescription());
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, planner.indexOf(data), intent, PendingIntent.FLAG_UPDATE_CURRENT);
            am.set(AlarmManager.RTC_WAKEUP, when, pendingIntent);

        } catch (ParseException e) {
            e.printStackTrace();
        }

    }
}

}

Got the solution from here: Android Set Multiple Alarms!