iOS 10 - 每 "x" 分钟重复一次通知

iOS 10 - Repeating notifications every "x" minutes

在 iOS 10 中,如何将本地通知设置为从特定 date/time.

开始在几分钟内重复

比如9月8日上午11点开始每15分钟触发一次本地通知?假设在下面,dateTimeReminder.date 是 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)

使用上面的代码,我可以安排在每小时的特定时间、每天的特定时间等。但是如何将它变成 "every "x" 分钟"?感谢任何帮助。

类似问题 - How do I set an NSCalendarUnitMinute repeatInterval on iOS 10 UserNotifications?

我想这个帖子有你要找的东西。

Do something every x minutes in Swift

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

如您所知,您可以为每个应用安排最多 64 条通知。如果您添加更多,系统将保留最快触发的 64 个通知并丢弃其他通知。

确保安排所有通知的一种方法是先安排前 64 条通知,然后定期(可能是在每次启动应用程序或每次触发通知时)检查通知的数量已安排通知,如果少于 64 个通知,假设有 n 个通知,则安排下一个 (64 - n) 个通知。

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

为了休息,添加 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
}

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

您分享的 link 和您添加的新详细信息您需要保存安排通知的最后一小时和一分钟的时间。然后当计时器滴答到下一分钟时,会更改小时和分钟

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)

您可以先使用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设置您想要触发本地通知的时间间隔

经过大量搜索后,我得出的结论是 Swift 3 目前不支持此功能。对于寻找此功能的每个人,我建议现在使用 UILocalNotification(尽管在 iOS10 中已弃用),但一旦它支持此功能,稍后迁移到 UNUserNotification。以下是帮助我得出此结论的一些其他问题和资源。此外,请关注此线程中的所有答案和评论,以更深入地了解它所讨论的特定场景。

  • 使用已弃用的 API 通常不是一个好主意。作为一般做法,请尽快迁移到新的 API。不建议将上述解决方案作为永久解决方案。

Local Notification every 2 week

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

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

Swift 3/4 和 iOS 10/11:

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

您可以使用 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)

这是我反复设置本地基于时间间隔的通知并执行自定义snooze和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])