Skip to content

Commit

Permalink
Persist base date within trigger.
Browse files Browse the repository at this point in the history
- Store base date for repeating notifications that are rescheduled.
- On reboot, do not fire high priority notifications that already fired.
  • Loading branch information
timkellypa committed Aug 5, 2020
1 parent 989f924 commit 965d356
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/android/notification/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,9 @@ private List<Integer> getSpecialMatchingComponents() {
* Gets the base date from where to calculate the next trigger date.
*/
private Date getBaseDate() {
if (spec.has("requestBaseDate")) {
return new Date(spec.optLong("requestBaseDate"));
} else
if (spec.has("at")) {
return new Date(spec.optLong("at", 0));
} else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import android.content.BroadcastReceiver;
import android.content.Context;
import android.os.PowerManager;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Calendar;

Expand All @@ -22,6 +26,8 @@
* The base class for any receiver that is trying to display a notification.
*/
abstract public class AbstractNotificationReceiver extends BroadcastReceiver {
private final String TAG = "AbstractNotification";

/**
* Perform a notification. All notification logic is here.
* Determines whether to dispatch events, autoLaunch the app, use fullScreenIntents, etc.
Expand Down Expand Up @@ -84,11 +90,34 @@ public void performNotification(Notification notification) {

Calendar cal = Calendar.getInstance();
cal.add(MINUTE, 1);
Request req = new Request(options, cal.getTime());

Request req = new Request(
getOptionsWithBaseDate(options, cal.getTimeInMillis()),
cal.getTime()
);

manager.schedule(req, this.getClass());
}

/**
* Clone options with base date attached to trigger.
* Used so that persisted objects know the last execution time.
* @param baseDateMillis base date represented in milliseconds
* @return new Options object with base time set in requestBaseDate.
*/
private Options getOptionsWithBaseDate(Options options, long baseDateMillis) {
JSONObject optionsDict = options.getDict();
try {
JSONObject triggerDict = optionsDict.getJSONObject("trigger");
triggerDict.put("requestBaseDate", baseDateMillis);
optionsDict.remove("trigger");
optionsDict.put("trigger", triggerDict);
} catch (JSONException e) {
Log.e(TAG, "Unexpected error adding requestBaseDate to JSON structure: " + e.toString());
}
return new Options(optionsDict);
}

/**
* Send the application an event using our notification
* @param key key for our event in the app
Expand Down

0 comments on commit 965d356

Please sign in to comment.