Yes you can push a notification at a particular time and day with a repeat option.
Follow the code below:
//1. catch the notif center
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
//2. I prefer removing any and all previously pending notif
[center removeAllPendingNotificationRequests];
//then check whether user has granted notif permission
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
if (settings.authorizationStatus != UNAuthorizationStatusAuthorized) {
// Notifications not allowed, ask permission again
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!error) {
//request authorization succeeded!
}
}];
}
}];
//3. prepare notif content
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.title = [NSString localizedUserNotificationStringForKey:NSLocalizedString(@"Hello! Today's Sunday!!",nil) arguments:nil];
content.body = [NSString localizedUserNotificationStringForKey:NSLocalizedString(@"Sleep tight!",nil) arguments:nil];
content.sound = [UNNotificationSound defaultSound];
content.badge = @([[UIApplication sharedApplication] applicationIconBadgeNumber] + 1);
//4. next, create a weekly trigger for notif on every Sunday
NSDate* sundayDate = startDate;
NSDateComponents *components = [[NSDateComponents alloc] init];
while(true){
NSInteger weekday = [[NSCalendar currentCalendar] component:NSCalendarUnitWeekday fromDate:sundayDate];
if(weekday == 1){//sunday, stay asleep reminder . LOL
components.weekday = 1;
components.hour = 9;
components.minute = 0;
break;
}else{//keep adding a day
[components setDay:1];
sundayDate = [[NSCalendar currentCalendar] dateByAddingComponents:components toDate:sundayDate options:0];
}
}
//5. Create time for notif on Sunday
unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comps = [calendar components:unitFlags fromDate:sundayDate];
comps.hour = 9;
comps.minute = 0;
sundayDate = [calendar dateFromComponents:comps];
NSDateComponents *triggerWeekly = [[NSCalendar currentCalendar] components:NSCalendarUnitWeekday + NSCalendarUnitHour + NSCalendarUnitMinute fromDate:sundayDate];
UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:triggerWeekly repeats:YES];
//6. finally, add it to the request
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"LocalIdentifier" content:content trigger:trigger];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (!error) {
NSLog(@"Local Notification succeeded");
} else {
NSLog(@"Local Notification failed");
}
}];
If you are looking for actionable items on the notification. You should add it somewhere in the center.
UNNotificationAction *snoozeAct = [UNNotificationAction actionWithIdentifier:@"Snooze"
title:NSLocalizedString(@"Snooze",nil) options:UNNotificationActionOptionNone];
UNNotificationAction *deleteAct = [UNNotificationAction actionWithIdentifier:@"Delete"
title:NSLocalizedString(@"Delete",nil) options:UNNotificationActionOptionDestructive];
UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:identifier
actions:@[snoozeAct,deleteAct] intentIdentifiers:@[]
options:UNNotificationCategoryOptionNone];
NSSet *categories = [NSSet setWithObject:category];
[center setNotificationCategories:categories];
content.categoryIdentifier = @"ActionIdentifier";
Make sure you set a different identifier for your notification request!