I have an app that notifies you if you have any bills to pay. I want one notification for each bill due. The alarm is set to go off at an exact time in the day.
I have everything working except for one aspect:
The alarm goes away after I swipe it out of notification panel. Then when I enter the app, I always check to reset any alarms (in case user changes dates), BUT the same alarm comes back over and over again. Even though it is past the time.
Here is code where I set the alarm. I loop thru each bill and see if alarm (the day of the month) is set:
public static void setRepeatingAlarm(Context c) {
List<Debt> debts = datasource.getAllDebt();
int count = 0;
for (Debt d : debts) {
int i;
if (String.valueOf(d.getDay()) == null) {
i = 300;
} else {
i = Integer.valueOf(d.getDay());
}
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, i);
cal.set(Calendar.HOUR_OF_DAY, 12);
cal.set(Calendar.MINUTE, 22);
cal.set(Calendar.SECOND, 10);
Intent intent = new Intent(c, TimeAlarm.class);
Bundle b = new Bundle();
b.putString("keyvalue", d.getName());
b.putInt("id", (int) d.getId());
intent.putExtras(b);
pendingIntent = PendingIntent.getBroadcast(c, (int) d.getId(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
}
}
Here is where I call notification:
public class TimeAlarm extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String debtName = intent.getStringExtra("keyvalue");
int id = intent.getIntExtra("id", 0);
CharSequence message = "Click to Update Balance for " + debtName;
Intent notificationIntent = new Intent(context, MainFragmentActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, id, notificationIntent, 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_notify)
.setContentTitle("Payment Due")
.setContentText(message)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(message));
mBuilder.setContentIntent(contentIntent);
mBuilder.setAutoCancel(true);
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(0, mBuilder.build());
}
}
Can someone help me with these problems?
You can explicitly cancel an alarm by passing the intent to alarmManager.cancel( intent );