7

In iOS 10, how can I set local notifications to repeat in minutes starting from a particular date/time.

For example, trigger local notification every 15 minutes starting from 11 AM on 8th September? Assume that, below, dateTimeReminder.date has 09/08 11 AM.

let dateStart = self.dateTimeNotif.date
let notifTrigger = UNCalendarNotificationTrigger.init(dateMatching: NSCalendar.current.dateComponents([.day, .month, .year, .hour, .minute], from: dateStart), repeats: true)
let notificationRequest = UNNotificationRequest(identifier: "MYNOTIF", content: notifContent, trigger: notifTrigger)

UNUserNotificationCenter.current().add(notificationRequest, withCompletionHandler: nil)

With the above code, I have a possibility to schedule at a particular minute of every hour, at a particular hour of each day and so on. But how do I turn it into "every "x" minutes"? Any help is appreciated.

Similar question - How do I set an NSCalendarUnitMinute repeatInterval on iOS 10 UserNotifications?

Community
  • 1
  • 1
DS.
  • 2,846
  • 4
  • 30
  • 35
  • Hey same type question : http://stackoverflow.com/questions/41845576/ios-10-how-to-show-incoming-voip-call-notification-when-app-is-in-background @DS. : Please help – Abhishek Thapliyal Jan 31 '17 at 10:24

7 Answers7

5

Swift 3/4 and iOS 10/11:

According with this bug seems there is no way to use DateComponents() to repeat correctly a local notification.

Instead of this method you can change your trigger with TimeInterval (this method works if you interval is major than 60 seconds):

let thisTime:TimeInterval = 60.0 // 1 minute = 60 seconds

// Some examples:
// 5 minutes = 300.0
// 1 hour = 3600.0
// 12 hours = 43200.0
// 1 day = 86400.0
// 1 week = 604800.0

let trigger = UNTimeIntervalNotificationTrigger(
            timeInterval: thisTime,
            repeats: true)
Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133
3

As you are already aware, you can schedule maximum of 64 notifications per app. If you add more than that, the system will keep the soonest firing 64 notifications and will discard the other.

One way to make sure all notifications get scheduled is to schedule the first 64 notifications first, and then on regular time intervals (may be on every launch of the app or each time a notification fires) check for the number of notifications scheduled and if there are less than 64 notifications, lets say n notifications, then schedule the next (64 - n) notifications.

int n = [[[UIApplication sharedApplication] scheduledLocalNotifications] count];
int x = 64 - n;
// Schedule the next 'x' notifications

for rest add a NSTimer for X minutes to come and set the notification.

override func viewDidLoad() {
    super.viewDidLoad()
    //Swift 2.2 selector syntax
    var timer = NSTimer.scheduledTimerWithTimeInterval(60.0, target: self, selector: #selector(MyClass.update), userInfo: nil, repeats: true)
    //Swift <2.2 selector syntax
    var timer = NSTimer.scheduledTimerWithTimeInterval(60.0, target: self, selector: "update", userInfo: nil, repeats: true)
}

// must be internal or public. 
func setNotification() {
    // Something cool
}

Make sure you remove old and not required notifications.

gurmandeep
  • 1,227
  • 1
  • 14
  • 30
  • This doesn't provide the start date. This schedules immediately and repeats every x minutes. Edit - to be clear - it starts at 60 seconds from now, and then repeats every minute afterward. – DS. Sep 08 '16 at 13:04
  • Please see my above comment. Also, please see discussion here - http://stackoverflow.com/questions/37804287/how-do-i-set-an-nscalendarunitminute-repeatinterval-on-ios-10-usernotifications/37804344?noredirect=1#comment66109521_37804344 – DS. Sep 08 '16 at 13:07
  • Hey same type question :http://stackoverflow.com/questions/41845576/ios-10-how-to-show-incoming-voip-call-notification-when-app-is-in-background : Please help : @gurmandeep – Abhishek Thapliyal Jan 31 '17 at 10:25
3

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

Community
  • 1
  • 1
DS.
  • 2,846
  • 4
  • 30
  • 35
2

This is how I set local notification based on time interval repeatedly and executed method customized snooze and delete

let content = UNMutableNotificationContent()
content.title = "Time Based Local Notification"
content.subtitle = "Its is a demo"
content.sound = .default
content.categoryIdentifier = "UYLReminderCategory"
//repition of time base local notification in foreground, background, killed
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: true)
let request = UNNotificationRequest(identifier: "IOS Demo", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
let snoozeAction = UNNotificationAction(identifier: "Snooze", title: "Snooze", options: [])
let deleteAction = UNNotificationAction(identifier: "UYLDeleteAction", title: "Delete", options: [.destructive])
let cat = UNNotificationCategory(identifier: "UYLReminderCategory", actions: [snoozeAction, deleteAction], intentIdentifiers: [], options: [])
UNUserNotificationCenter.current().setNotificationCategories([cat])
  • It works for repeating notification, Thanks! I have voted up for you. – Rajput Nancy Katal Jan 19 '23 at 23:59
  • I need to get notification when app is terminated and repeat notification every 72 hours t's working fine however my app is in background and I rebooted device. After, rebooted device, notification is not working while terminate app until I manually open app. Is there anything, I am misisng? – Rajput Nancy Katal Jan 20 '23 at 00:04
0

I think this thread has what you're looking for.

Do something every x minutes in Swift

The '60.0' represents seconds between the code running. Simply change that to x minutes * 60

Community
  • 1
  • 1
key9060
  • 75
  • 2
  • 10
  • This doesn't provide the start date. This schedules immediately and repeats every x minutes. – DS. Sep 08 '16 at 13:03
  • You could start the timer when the notification runs. Not completely sure what you mean. – key9060 Sep 08 '16 at 13:07
  • 1
    I tried that but - "you can start when the notification runs" is not feasible. There are only two methods in the new API - willAppear and didReceive. One triggers only if your app is in the foreground. The other triggers only if the user interacts with your notification. It is likely that neither might happen. That is notification appears and stays in the notification center. How do you start the timer then? – DS. Sep 08 '16 at 13:10
0

The link you shared and the new details you added you need to save the last hour and minute of time for which notification got scheduled. Then when the timer ticks to next minute, there change the hour and minute

var date = DateComponents()
date.hour = 8
date.minute = 30

let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: true)
let content = UNNotificationContent()
// edit your content

let notification = UNNotificationRequest(identifier: "myNotification", content: content, trigger: trigger)
gurmandeep
  • 1,227
  • 1
  • 14
  • 30
  • This is straight from Apple documentation. Note what it says - Listing 1 shows an example that builds a trigger to fire every morning at 8:30 by specifying only the hour and minute date components and setting the trigger to repeat. https://developer.apple.com/reference/usernotifications/uncalendarnotificationtrigger – DS. Sep 08 '16 at 13:12
  • My scenario is - "trigger local notification every 15 minutes starting from 11 AM on 8th September" – DS. Sep 08 '16 at 13:13
  • You have to set your custom algorithm's and make the notifications pop every 15 mins i.e it will start 8:00 and then 8:15 and so on. The timer will help and you very well know how to use the notifications – gurmandeep Sep 09 '16 at 03:08
0

You can first use the UNCalendarNotificationTrigger to trigger the notification at a particular date and set its repeat property to false so that it notifies only once. When you receive that notification, use the UNTimeIntervalNotificationTrigger API to set the time interval at which you want to trigger the local notification

  • 6
    This assumes that the user responds to the first notification. If the user ignores it, the App will not receive the first notification! – ragnarius Jan 09 '17 at 23:54