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

Support repeatIntervals of minute, hour, day, week, month, and year in addNotificationRequest() #308

Closed
Closed
Changes from 1 commit
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
Prev Previous commit
Additional modifications to local notification repeating logic to ens…
…ure function of all use cases. Supports repeating by year, month, week, day, hour, minute, non-repeating, and immediate delivery.

The "repeats" parameter in addNotificationRequest() is effectively redundant and replaced by "repeatInterval" however the documented behaviour of "repeats" has been retained to ensure backwards compatibility, even though it didn't previously work.
  • Loading branch information
jamesxabregas committed Jul 20, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit b159cb60acf127ff1cd536d554eed58d200284a7
38 changes: 24 additions & 14 deletions ios/RCTConvert+Notification.m
Original file line number Diff line number Diff line change
@@ -116,12 +116,19 @@ + (UNNotificationRequest *)UNNotificationRequest:(id)json
}

NSDate* fireDate = [RCTConvert NSDate:details[@"fireDate"]];
BOOL repeats = [RCTConvert BOOL:details[@"repeats"]];
NSString* repeatInterval = [RCTConvert NSString:details[@"repeatInterval"]];

//For backward compatability with existing request interface.
//If repeats param is set to true and no repeatInterval set then use "day" as the default
BOOL repeatsParam = [RCTConvert BOOL:details[@"repeats"]];
if (repeatsParam && repeatInterval.length == 0) {
repeatInterval = @"day";
}

BOOL repeats = TRUE;
NSDateComponents *triggerDate = nil;
if (repeats && fireDate) {

if (fireDate) {
if ([repeatInterval isEqualToString:@"year"]) {
triggerDate = [[NSCalendar currentCalendar]
components:
@@ -135,32 +142,35 @@ + (UNNotificationRequest *)UNNotificationRequest:(id)json
NSCalendarUnitHour + NSCalendarUnitMinute +
NSCalendarUnitSecond + NSCalendarUnitTimeZone
fromDate:fireDate] : nil;

} else if ([repeatInterval isEqualToString:@"week"]) {
triggerDate = fireDate ? [[NSCalendar currentCalendar]
components: NSCalendarUnitWeekday +
NSCalendarUnitHour + NSCalendarUnitMinute +
NSCalendarUnitSecond + NSCalendarUnitTimeZone
fromDate:fireDate] : nil;

} else if ([repeatInterval isEqualToString:@"day"]) {
triggerDate = fireDate ? [[NSCalendar currentCalendar]
components:
NSCalendarUnitHour + NSCalendarUnitMinute +
NSCalendarUnitSecond + NSCalendarUnitTimeZone
fromDate:fireDate] : nil;
} else if ([repeatInterval isEqualToString:@"hour"]) {

triggerDate = fireDate ? [[NSCalendar currentCalendar]
components: NSCalendarUnitMinute +
NSCalendarUnitSecond + NSCalendarUnitTimeZone
fromDate:fireDate] : nil;

} else if ([repeatInterval isEqualToString:@"minute"]) {

triggerDate = fireDate ? [[NSCalendar currentCalendar]
components: NSCalendarUnitSecond + NSCalendarUnitTimeZone
fromDate:fireDate] : nil;
} else { //Default to "day" if repeatInterval not specifed or invalid
triggerDate = fireDate ? [[NSCalendar currentCalendar]
components:
NSCalendarUnitHour + NSCalendarUnitMinute +
NSCalendarUnitSecond + NSCalendarUnitTimeZone
fromDate:fireDate] : nil;
} else { //If no valid repeat interval, set repeats to false and create non-repeating trigger date
repeats = FALSE;
triggerDate = [[NSCalendar currentCalendar]
components: NSCalendarUnitYear +
NSCalendarUnitMonth + NSCalendarUnitDay +
NSCalendarUnitHour + NSCalendarUnitMinute +
NSCalendarUnitSecond + NSCalendarUnitTimeZone
fromDate:fireDate];
}
}