Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.

Commit 100e8f4

Browse files
author
Boris Tacyniak
authored
Merge pull request #2030 from elsaapp/repeat-time-for-all-repeat-types
Feat: Allow for repeat to specify amount of the given repeat type (Android)
2 parents a15978c + c5490e8 commit 100e8f4

File tree

2 files changed

+44
-44
lines changed

2 files changed

+44
-44
lines changed

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,9 @@ PushNotification.localNotificationSchedule({
361361
message: "My Notification Message", // (required)
362362
date: new Date(Date.now() + 60 * 1000), // in 60 secs
363363
allowWhileIdle: false, // (optional) set notification to work while on doze, default: false
364+
365+
/* Android Only Properties */
366+
repeatTime: 1, // (optional) Increment of configured repeateType. Check 'Repeating Notifications' section for more info.
364367
});
365368
```
366369

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

643646
### Android
644-
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.
647+
Property `repeatType` could be one of `month`, `week`, `day`, `hour`, `minute`, `time`.
648+
649+
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.
650+
For example, to configure a notification every other day
651+
```javascript
652+
PushNotification.localNotificationSchedule({
653+
...
654+
repeatType: 'day',
655+
repeatTime: 2,
656+
...
657+
});
658+
```
645659

646660
## Notification Actions
647661

android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java

Lines changed: 29 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@
4040
import java.util.ArrayList;
4141
import java.util.Arrays;
4242
import java.util.Calendar;
43-
import java.util.Date;
44-
import java.util.GregorianCalendar;
4543
import java.util.List;
4644
import java.util.Map;
4745

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

6358
public RNPushNotificationHelper(Application context) {
6459
this.context = context;
@@ -623,44 +618,19 @@ private void scheduleNextNotificationIfRepeating(Bundle bundle) {
623618
return;
624619
}
625620

626-
long newFireDate = 0;
627-
628-
switch (repeatType) {
629-
case "time":
630-
newFireDate = fireDate + repeatTime;
631-
break;
632-
case "month":
633-
final Calendar fireDateCalendar = new GregorianCalendar();
634-
fireDateCalendar.setTime(new Date(fireDate));
635-
final int fireDay = fireDateCalendar.get(Calendar.DAY_OF_MONTH);
636-
final int fireMinute = fireDateCalendar.get(Calendar.MINUTE);
637-
final int fireHour = fireDateCalendar.get(Calendar.HOUR_OF_DAY);
638-
639-
final Calendar nextEvent = new GregorianCalendar();
640-
nextEvent.setTime(new Date());
641-
final int currentMonth = nextEvent.get(Calendar.MONTH);
642-
int nextMonth = currentMonth < 11 ? (currentMonth + 1) : 0;
643-
nextEvent.set(Calendar.YEAR, nextEvent.get(Calendar.YEAR) + (nextMonth == 0 ? 1 : 0));
644-
nextEvent.set(Calendar.MONTH, nextMonth);
645-
final int maxDay = nextEvent.getActualMaximum(Calendar.DAY_OF_MONTH);
646-
nextEvent.set(Calendar.DAY_OF_MONTH, Math.min(fireDay, maxDay));
647-
nextEvent.set(Calendar.HOUR_OF_DAY, fireHour);
648-
nextEvent.set(Calendar.MINUTE, fireMinute);
649-
nextEvent.set(Calendar.SECOND, 0);
650-
newFireDate = nextEvent.getTimeInMillis();
651-
break;
652-
case "week":
653-
newFireDate = fireDate + 7 * ONE_DAY;
654-
break;
655-
case "day":
656-
newFireDate = fireDate + ONE_DAY;
657-
break;
658-
case "hour":
659-
newFireDate = fireDate + ONE_HOUR;
660-
break;
661-
case "minute":
662-
newFireDate = fireDate + ONE_MINUTE;
663-
break;
621+
long newFireDate;
622+
if ("time".equals(repeatType)) {
623+
newFireDate = fireDate + repeatTime;
624+
} else {
625+
int repeatField = getRepeatField(repeatType);
626+
627+
final Calendar nextEvent = Calendar.getInstance();
628+
nextEvent.setTimeInMillis(fireDate);
629+
// Limits repeat time increment to int instead of long
630+
int increment = repeatTime > 0 ? (int) repeatTime : 1;
631+
nextEvent.add(repeatField, increment);
632+
633+
newFireDate = nextEvent.getTimeInMillis();
664634
}
665635

666636
// Sanity check, should never happen
@@ -673,6 +643,22 @@ private void scheduleNextNotificationIfRepeating(Bundle bundle) {
673643
}
674644
}
675645

646+
private int getRepeatField(String repeatType) {
647+
switch (repeatType) {
648+
case "month":
649+
return Calendar.MONTH;
650+
case "week":
651+
return Calendar.WEEK_OF_YEAR;
652+
case "hour":
653+
return Calendar.HOUR;
654+
case "minute":
655+
return Calendar.MINUTE;
656+
case "day":
657+
default:
658+
return Calendar.DATE;
659+
}
660+
}
661+
676662
private Uri getSoundUri(String soundName) {
677663
if (soundName == null || "default".equalsIgnoreCase(soundName)) {
678664
return RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

0 commit comments

Comments
 (0)