Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(android): LocalNotification on not working properly #3307

Merged
merged 6 commits into from
Jul 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ public void setMinute(Integer minute) {
private Calendar buildCalendar(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.MILLISECOND, 0);
cal.set(Calendar.SECOND, 0);
return cal;
}

Expand All @@ -92,46 +94,46 @@ public long nextTrigger(Date date) {
* Postpone trigger if first schedule matches the past
*/
private long postponeTriggerIfNeeded(Calendar current, Calendar next) {
int currentYear = current.get(Calendar.YEAR);
if (matchesUnit(Calendar.YEAR, current, next)) {
next.set(Calendar.YEAR, currentYear + 1);
this.unit = Calendar.YEAR;
} else if (matchesUnit(Calendar.MONTH, current, next)) {
next.set(Calendar.YEAR, currentYear + 1);
this.unit = Calendar.MONTH;
} else if (matchesUnit(Calendar.DAY_OF_MONTH, current, next)) {
next.set(Calendar.MONTH, current.get(Calendar.MONTH) + 1);
this.unit = Calendar.DAY_OF_MONTH;
} else if (matchesUnit(Calendar.HOUR_OF_DAY, current, next)) {
next.set(Calendar.DAY_OF_MONTH, current.get(Calendar.DAY_OF_MONTH) + 1);
this.unit = Calendar.DAY_OF_MONTH;
} else if (matchesUnit(Calendar.MINUTE, current, next)) {
next.set(Calendar.HOUR_OF_DAY, current.get(Calendar.HOUR_OF_DAY) + 1);
this.unit = Calendar.MINUTE;
if (next.getTimeInMillis() <= current.getTimeInMillis() && unit != -1) {
Integer incrementUnit = -1;
if (unit == Calendar.YEAR || unit == Calendar.MONTH) {
incrementUnit = Calendar.YEAR;
} else if (unit == Calendar.DAY_OF_MONTH) {
incrementUnit = Calendar.MONTH;
} else if (unit == Calendar.HOUR_OF_DAY) {
incrementUnit = Calendar.DAY_OF_MONTH;
} else if (unit == Calendar.MINUTE) {
incrementUnit = Calendar.HOUR_OF_DAY;
}

if (incrementUnit != -1) {
next.set(incrementUnit, next.get(incrementUnit) + 1);
}
}
return next.getTimeInMillis();
}

private boolean matchesUnit(Integer unit, Calendar current, Calendar next) {
return next.get(unit) < current.get(unit);
}

private Calendar buildNextTriggerTime(Date date) {
Calendar next = buildCalendar(date);
if (year != null) {
next.set(Calendar.YEAR, year);
if (unit == -1) unit = Calendar.YEAR;
}
if (month != null) {
next.set(Calendar.MONTH, month);
if (unit == -1) unit = Calendar.MONTH;
}
if (day != null) {
next.set(Calendar.DAY_OF_MONTH, day);
if (unit == -1) unit = Calendar.DAY_OF_MONTH;
}
if (hour != null) {
next.set(Calendar.HOUR_OF_DAY, hour);
if (unit == -1) unit = Calendar.HOUR_OF_DAY;
}
if (minute != null) {
next.set(Calendar.MINUTE, minute);
if (unit == -1) unit = Calendar.MINUTE;
}
return next;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

Expand Down Expand Up @@ -338,9 +339,12 @@ private void triggerScheduledNotification(Notification notification, LocalNotifi
// Cron like scheduler
DateMatch on = schedule.getOn();
if (on != null) {
long trigger = on.nextTrigger(new Date());
notificationIntent.putExtra(TimedNotificationPublisher.CRON_KEY, on.toMatchString());
pendingIntent = PendingIntent.getBroadcast(context, request.getId(), notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
alarmManager.setExact(AlarmManager.RTC, on.nextTrigger(new Date()), pendingIntent);
alarmManager.setExact(AlarmManager.RTC, trigger, pendingIntent);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Logger.debug(Logger.tags("LN"), "notification " + request.getId() + " will next fire at " + sdf.format(new Date(trigger)));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.content.Intent;
import com.getcapacitor.Logger;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
Expand Down Expand Up @@ -42,8 +43,10 @@ private void rescheduleNotificationIfNeeded(Context context, Intent intent, int
long trigger = date.nextTrigger(new Date());
Intent clone = (Intent) intent.clone();
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, id, clone, PendingIntent.FLAG_CANCEL_CURRENT);
alarmManager.set(AlarmManager.RTC, trigger, pendingIntent);
alarmManager.setExact(AlarmManager.RTC, trigger, pendingIntent);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Logger.debug(Logger.tags("LN"), "notification " + id + " will next fire at " + sdf.format(new Date(trigger)));
}
}

}