Dismiss notification automatically when user adds a checkmark

pull/114/merge
Alinson S. Xavier 10 years ago
parent e933cbbc43
commit eae0d66f51

@ -167,9 +167,7 @@ public class HabitBroadcastReceiver extends BroadcastReceiver
{ {
if (todayValue != Checkmark.UNCHECKED) return; if (todayValue != Checkmark.UNCHECKED) return;
if (!checkWeekday(intent, habit)) return; if (!checkWeekday(intent, habit)) return;
if (!habit.hasReminder()) return;
// Check if reminder has been turned off after alarm was scheduled
if (habit.reminderHour == null) return;
Intent contentIntent = new Intent(context, MainActivity.class); Intent contentIntent = new Intent(context, MainActivity.class);
contentIntent.setData(data); contentIntent.setData(data);
@ -184,10 +182,12 @@ public class HabitBroadcastReceiver extends BroadcastReceiver
NotificationCompat.WearableExtender wearableExtender = NotificationCompat.WearableExtender wearableExtender =
new NotificationCompat.WearableExtender().setBackground( new NotificationCompat.WearableExtender().setBackground(
BitmapFactory.decodeResource(context.getResources(), R.drawable.stripe)); BitmapFactory.decodeResource(context.getResources(),
R.drawable.stripe));
Notification notification = Notification notification =
new NotificationCompat.Builder(context).setSmallIcon(R.drawable.ic_notification) new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(habit.name) .setContentTitle(habit.name)
.setContentText(habit.description) .setContentText(habit.description)
.setContentIntent(contentPendingIntent) .setContentIntent(contentPendingIntent)
@ -205,7 +205,8 @@ public class HabitBroadcastReceiver extends BroadcastReceiver
notification.flags |= Notification.FLAG_AUTO_CANCEL; notification.flags |= Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Activity.NOTIFICATION_SERVICE); (NotificationManager) context.getSystemService(
Activity.NOTIFICATION_SERVICE);
int notificationId = (int) (habit.getId() % Integer.MAX_VALUE); int notificationId = (int) (habit.getId() % Integer.MAX_VALUE);
notificationManager.notify(notificationId, notification); notificationManager.notify(notificationId, notification);
@ -261,4 +262,13 @@ public class HabitBroadcastReceiver extends BroadcastReceiver
return reminderDays[weekday]; return reminderDays[weekday];
} }
public static void dismissNotification(Context context, Habit habit)
{
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(
Activity.NOTIFICATION_SERVICE);
int notificationId = (int) (habit.getId() % Integer.MAX_VALUE);
notificationManager.cancel(notificationId);
}
} }

@ -44,6 +44,7 @@ import org.isoron.uhabits.fragments.ListHabitsFragment;
import org.isoron.uhabits.helpers.DateHelper; import org.isoron.uhabits.helpers.DateHelper;
import org.isoron.uhabits.helpers.ReminderHelper; import org.isoron.uhabits.helpers.ReminderHelper;
import org.isoron.uhabits.helpers.UIHelper; import org.isoron.uhabits.helpers.UIHelper;
import org.isoron.uhabits.models.Checkmark;
import org.isoron.uhabits.models.Habit; import org.isoron.uhabits.models.Habit;
import org.isoron.uhabits.tasks.BaseTask; import org.isoron.uhabits.tasks.BaseTask;
import org.isoron.uhabits.widgets.CheckmarkWidgetProvider; import org.isoron.uhabits.widgets.CheckmarkWidgetProvider;
@ -271,11 +272,21 @@ public class MainActivity extends BaseActivity
@Override @Override
protected void doInBackground() protected void doInBackground()
{ {
dismissNotifications(MainActivity.this);
updateWidgets(MainActivity.this); updateWidgets(MainActivity.this);
} }
}.execute(); }.execute();
} }
private void dismissNotifications(Context context)
{
for(Habit h : Habit.getHabitsWithReminder())
{
if(h.checkmarks.getTodayValue() != Checkmark.UNCHECKED)
HabitBroadcastReceiver.dismissNotification(context, h);
}
}
public static void updateWidgets(Context context) public static void updateWidgets(Context context)
{ {
updateWidgets(context, CheckmarkWidgetProvider.class); updateWidgets(context, CheckmarkWidgetProvider.class);

Loading…
Cancel
Save