Skip to content

Commit

Permalink
Merge pull request #2030 from elsaapp/repeat-time-for-all-repeat-types
Browse files Browse the repository at this point in the history
Feat: Allow for repeat to specify amount of the given repeat type (Android)
  • Loading branch information
Boris Tacyniak authored Jun 24, 2021
2 parents a15978c + c5490e8 commit 100e8f4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 44 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,9 @@ PushNotification.localNotificationSchedule({
message: "My Notification Message", // (required)
date: new Date(Date.now() + 60 * 1000), // in 60 secs
allowWhileIdle: false, // (optional) set notification to work while on doze, default: false

/* Android Only Properties */
repeatTime: 1, // (optional) Increment of configured repeateType. Check 'Repeating Notifications' section for more info.
});
```

Expand Down Expand Up @@ -641,7 +644,18 @@ https://developer.android.com/training/monitoring-device-state/doze-standby
Property `repeatType` can only be `day`.

### Android
Property `repeatType` could be one of `month`, `week`, `day`, `hour`, `minute`, `time`. If specified as time, it should be accompanied by one more parameter `repeatTime` which should the number of milliseconds between each interval.
Property `repeatType` could be one of `month`, `week`, `day`, `hour`, `minute`, `time`.

The interval used can be configured to a different interval using `repeatTime`. If `repeatType` is `time`, `repeatTime` must be specified as the number of milliseconds between each interval.
For example, to configure a notification every other day
```javascript
PushNotification.localNotificationSchedule({
...
repeatType: 'day',
repeatTime: 2,
...
});
```

## Notification Actions

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;

Expand All @@ -56,9 +54,6 @@ public class RNPushNotificationHelper {
private Context context;
private RNPushNotificationConfig config;
private final SharedPreferences scheduledNotificationsPersistence;
private static final int ONE_MINUTE = 60 * 1000;
private static final long ONE_HOUR = 60 * ONE_MINUTE;
private static final long ONE_DAY = 24 * ONE_HOUR;

public RNPushNotificationHelper(Application context) {
this.context = context;
Expand Down Expand Up @@ -623,44 +618,19 @@ private void scheduleNextNotificationIfRepeating(Bundle bundle) {
return;
}

long newFireDate = 0;

switch (repeatType) {
case "time":
newFireDate = fireDate + repeatTime;
break;
case "month":
final Calendar fireDateCalendar = new GregorianCalendar();
fireDateCalendar.setTime(new Date(fireDate));
final int fireDay = fireDateCalendar.get(Calendar.DAY_OF_MONTH);
final int fireMinute = fireDateCalendar.get(Calendar.MINUTE);
final int fireHour = fireDateCalendar.get(Calendar.HOUR_OF_DAY);

final Calendar nextEvent = new GregorianCalendar();
nextEvent.setTime(new Date());
final int currentMonth = nextEvent.get(Calendar.MONTH);
int nextMonth = currentMonth < 11 ? (currentMonth + 1) : 0;
nextEvent.set(Calendar.YEAR, nextEvent.get(Calendar.YEAR) + (nextMonth == 0 ? 1 : 0));
nextEvent.set(Calendar.MONTH, nextMonth);
final int maxDay = nextEvent.getActualMaximum(Calendar.DAY_OF_MONTH);
nextEvent.set(Calendar.DAY_OF_MONTH, Math.min(fireDay, maxDay));
nextEvent.set(Calendar.HOUR_OF_DAY, fireHour);
nextEvent.set(Calendar.MINUTE, fireMinute);
nextEvent.set(Calendar.SECOND, 0);
newFireDate = nextEvent.getTimeInMillis();
break;
case "week":
newFireDate = fireDate + 7 * ONE_DAY;
break;
case "day":
newFireDate = fireDate + ONE_DAY;
break;
case "hour":
newFireDate = fireDate + ONE_HOUR;
break;
case "minute":
newFireDate = fireDate + ONE_MINUTE;
break;
long newFireDate;
if ("time".equals(repeatType)) {
newFireDate = fireDate + repeatTime;
} else {
int repeatField = getRepeatField(repeatType);

final Calendar nextEvent = Calendar.getInstance();
nextEvent.setTimeInMillis(fireDate);
// Limits repeat time increment to int instead of long
int increment = repeatTime > 0 ? (int) repeatTime : 1;
nextEvent.add(repeatField, increment);

newFireDate = nextEvent.getTimeInMillis();
}

// Sanity check, should never happen
Expand All @@ -673,6 +643,22 @@ private void scheduleNextNotificationIfRepeating(Bundle bundle) {
}
}

private int getRepeatField(String repeatType) {
switch (repeatType) {
case "month":
return Calendar.MONTH;
case "week":
return Calendar.WEEK_OF_YEAR;
case "hour":
return Calendar.HOUR;
case "minute":
return Calendar.MINUTE;
case "day":
default:
return Calendar.DATE;
}
}

private Uri getSoundUri(String soundName) {
if (soundName == null || "default".equalsIgnoreCase(soundName)) {
return RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Expand Down

0 comments on commit 100e8f4

Please sign in to comment.