iOS 10 - 每隔“x”分钟重复一次通知

时间:2022-10-19 01:06:31

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

在iOS 10中,如何设置从特定日期/时间开始在几分钟内重复的本地通知。

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.

例如,从9月8日上午11点开始,每15分钟触发一次本地通知?假设在下面,dateTimeReminder.date有09/08上午11点。

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.

通过上面的代码,我有可能在每小时的特定时刻,每天的特定时刻安排,等等。但是如何将其变成“每”x“分钟”?任何帮助表示赞赏。

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

类似的问题 - 如何在iOS 10 UserNotifications上设置NSCalendarUnitMinute repeatInterval?

6 个解决方案

#1


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.

您已经知道,每个应用最多可以安排64个通知。如果你添加更多,系统将保持最快的64个通知,并将丢弃另一个。

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.

确保所有通知都已安排的一种方法是先安排前64个通知,然后按照常规时间间隔(可能是每次启动应用程序或每次通知触发时)检查计划的通知数量以及是否有少于64个通知,让我们说n个通知,然后安排下一个(64-n)通知。

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.

休息时间为X分钟添加NSTimer并设置通知。

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.

确保删除旧的和不需要的通知。

#2


2  

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.

经过大量搜索后,我得出的结论是,此时Swift 3不支持此功能。对于每个寻找此功能的人,我建议现在使用UILocalNotification(虽然在iOS 10中已弃用),但是一旦支持此功能,稍后会迁移到UNUserNotification。以下是一些帮助我得出这个结论的其他问题和资源。另外,请遵循此主题中的所有答案和评论,以便更深入地了解它所涉及的特定方案。

  • 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.
  • 使用已弃用的API通常是个坏主意。作为一般做法,请尽快迁移到新的API。不建议将上述解决方案作为永久解决方案。

Local Notification every 2 week

每两周一次本地通知

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

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

#3


1  

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)

#4


1  

Swift 3/4 and iOS 10/11:

Swift 3/4和iOS 10/11:

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

根据这个bug似乎没有办法使用DateComponents()来正确重复本地通知。

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

您可以使用TimeInterval更改触发器而不是此方法(如果间隔大于60秒,则此方法有效):

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)

#5


0  

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

我认为这个主题有你想要的东西。

Do something every x minutes in Swift

在Swift中每隔x分钟做一些事情

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

'60 .0'表示代码运行之间的秒数。只需将其更改为x分钟* 60

#6


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

您可以先使用UNCalendarNotificationTrigger在特定日期触发通知,并将其repeat属性设置为false,以便它只通知一次。收到该通知后,使用UNTimeIntervalNotificationTrigger API设置要触发本地通知的时间间隔

#1


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.

您已经知道,每个应用最多可以安排64个通知。如果你添加更多,系统将保持最快的64个通知,并将丢弃另一个。

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.

确保所有通知都已安排的一种方法是先安排前64个通知,然后按照常规时间间隔(可能是每次启动应用程序或每次通知触发时)检查计划的通知数量以及是否有少于64个通知,让我们说n个通知,然后安排下一个(64-n)通知。

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.

休息时间为X分钟添加NSTimer并设置通知。

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.

确保删除旧的和不需要的通知。

#2


2  

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.

经过大量搜索后,我得出的结论是,此时Swift 3不支持此功能。对于每个寻找此功能的人,我建议现在使用UILocalNotification(虽然在iOS 10中已弃用),但是一旦支持此功能,稍后会迁移到UNUserNotification。以下是一些帮助我得出这个结论的其他问题和资源。另外,请遵循此主题中的所有答案和评论,以便更深入地了解它所涉及的特定方案。

  • 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.
  • 使用已弃用的API通常是个坏主意。作为一般做法,请尽快迁移到新的API。不建议将上述解决方案作为永久解决方案。

Local Notification every 2 week

每两周一次本地通知

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

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

#3


1  

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)

#4


1  

Swift 3/4 and iOS 10/11:

Swift 3/4和iOS 10/11:

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

根据这个bug似乎没有办法使用DateComponents()来正确重复本地通知。

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

您可以使用TimeInterval更改触发器而不是此方法(如果间隔大于60秒,则此方法有效):

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)

#5


0  

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

我认为这个主题有你想要的东西。

Do something every x minutes in Swift

在Swift中每隔x分钟做一些事情

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

'60 .0'表示代码运行之间的秒数。只需将其更改为x分钟* 60

#6


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

您可以先使用UNCalendarNotificationTrigger在特定日期触发通知,并将其repeat属性设置为false,以便它只通知一次。收到该通知后,使用UNTimeIntervalNotificationTrigger API设置要触发本地通知的时间间隔