How to Set an Nscalendarunitminute Repeatinterval on iOS 10 Usernotifications

Repeat local notification for specific date iOS 10

You need to replace this in your code

UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:triggerDate
repeats:YES];

iOS 10 - Repeating notifications every x minutes

After searching around quite a bit, I've come to the conclusion that Swift 3, at this point, doesn't support this feature. For everyone looking for this functionality, I'd suggest using UILocalNotification for now (although deprecated in iOS 10), but later migrate to UNUserNotification once it supports this feature. Here are some additional questions and resources that have helped me to reach this conclusion. Also, please follow all the answers and comments in this thread to get more insight into which particular scenario it talks about.

  • It is usually a bad idea to use deprecated APIs. As a general practice, migrate to new APIs as soon as possible. The above solution is NOT recommended as a permanent solution.

Local Notification every 2 week

http://useyourloaf.com/blog/local-notifications-with-ios-10/

https://github.com/lionheart/openradar-mirror/issues/14941

UserNotification in 3 days then repeat every day/hour - iOS 10

It seems like this is not supported, but to make a workaround you could use:

let alertDays = 3.0
let daySeconds = 86400
let alertSeconds = alertDays * daySeconds

let content: UNMutableNotificationContent = UNMutableNotificationContent()

content.title = "Reminder Title"
content.subtitle = "Reminder Subtitle"
content.body = "Reminder Message"

let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: (alertSeconds), repeats: false)

let request = UNNotificationRequest(identifier: workoutAlarmIdentifier,
content: content,
trigger: trigger)

UNUserNotificationCenter.current().add(request)
{
(error) in // ...
}

in combination with didReceive(_:withContentHandler:) you can use:

let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: (daySeconds), repeats: false)

I know this isn't optimal but it should work without using deprecated classes/methods. You use repeats: false since you are intercepting the notification just before the user receives it and creating a new notification. Additionally you can use it in combination with UNNotificationAction and UNNotificationCategory if you handle multiple notifications.

How to Set repeat interval as well as fire date in UNNotificationRequest

You can still use the fire date and a repeat interval but it is not as obvious as it used to be. When you initialize the notification trigger you set its repeat parameter to YES. The repeat interval is defined by the date components that you pass to the trigger via the dateMatching parameter:

Repeat daily:
Just pass the date components hour, minute, second
⟶ The trigger will fire every day at that time

Repeat weekly:
Also pass the desired weekday component: weekday, hour, minute, second
⟶ The trigger will fire every week on that weekday at that time

Here is an example that fires a notification tomorrow at the same time and repeats it every week:

Objective-C:

UNMutableNotificationContent *notification = [[UNMutableNotificationContent alloc] init];

// find out what weekday is tomorrow
NSDate *sameTimeTomorrow = [NSDate dateWithTimeIntervalSinceNow:60*60*24];
NSInteger weekdayTomorrow = [[NSCalendar currentCalendar] component:NSCalendarUnitWeekday fromDate: sameTimeTomorrow];

// set the current time (without weekday)
unsigned unitFlags = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
NSDateComponents *firstFireDateComponents = [[NSCalendar currentCalendar] components:unitFlags fromDate:sameTimeTomorrow];

// add tomorrow's weekday
firstFireDateComponents.weekday = weekdayTomorrow;

// set a repeating trigger for the current time and tomorrow's weekday
// (trigger repeats every week on that weekday at the current time)
UNCalendarNotificationTrigger *notificationTrigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:firstFireDateComponents repeats:YES];

UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"notification" content:notification trigger:notificationTrigger];
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler: nil];

Swift:

let notification = UNMutableNotificationContent()

// find out what weekday is tomorrow
let weekDayTomorrow = Calendar.current.component(.weekday, from: Date(timeIntervalSinceNow: 60*60*24))

// set the current time (without weekday)
var firstFireDateComponents = Calendar.current.dateComponents([.hour, .minute, .second], from: Date())

// add tomorrow's weekday
firstFireDateComponents.weekday = weekDayTomorrow

// set a repeating trigger for the current time and tomorrow's weekday
// (trigger repeats every week on that weekday at the current time)
let notificationTrigger = UNCalendarNotificationTrigger(dateMatching: firstFireDateComponents, repeats: true)

let request = UNNotificationRequest(identifier: "notification", content: notification, trigger: notificationTrigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)

Having issue with repeatinterval every 30 minutes

firedate sets the time that the notification fires the first time, and repeatInterval is the interval between between repetitions of the notification. So the code in the question schedules a notification to fire 30 minutes (60 * 30 seconds) from now, and then repeat every hour.

Unfortunately, you can only schedule notifications to repeat at exact intervals defined by NSCalendar constants: e.g., every minute, every hour, every day, every month, but not at multiples of those intervals.

Luckily, to get a notification every 30 minutes, you can just schedule two notifications: one right now, one 30 minutes from now, and have both repeat every hour. Like so:

UILocalNotification *reminderNote = [[UILocalNotification alloc]init];
reminderNote.fireDate = [NSDate dateWithTimeIntervalSinceNow:60 * 30];
reminderNote.repeatInterval = NSHourCalendarUnit;
reminderNote.alertBody = @"some text";
reminderNote.alertAction = @"View";
reminderNote.soundName = @"sound.aif";
[[UIApplication sharedApplication] scheduleLocalNotification:reminderNote];

reminderNote.fireDate = [NSDate dateWithTimeIntervalSinceNow:60 * 60];
[[UIApplication sharedApplication] scheduleLocalNotification:reminderNote];

Swift 3 Local notifications not firing

So after weeks of frustration, this is the solution :

Replace dateComponents.day = day with dateComponents.weekday = day. .day is for day of the month whereas .weekday is day of the week!

Also do not use dateComponents.year = 2017 as that caused the notifications not to fire.

The notifications work perfectly fine now!!



Related Topics



Leave a reply



Submit