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

问题描述 投票:5回答:7

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

例如,从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)

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

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

ios swift ios10
7个回答
3
投票

您已经知道,每个应用最多可以安排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
}

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


2
投票

经过大量搜索后,我得出的结论是,此时Swift 3不支持此功能。对于每个寻找此功能的人,我建议现在使用UILocalNotification(虽然在iOS 10中已弃用),但是一旦支持此功能,稍后会迁移到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


2
投票

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)

1
投票

您共享的链接以及您添加的新详细信息需要保存计划通知的最后一小时和一小时。然后当计时器滴答到下一分钟时,会改变小时和分钟

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)

1
投票

这是我如何重复设置基于时间间隔的本地通知和执行方法自定义的贪睡和删除

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])

0
投票

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

Do something every x minutes in Swift

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


0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.