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

7
在iOS 10中,我如何设置本地通知以从特定日期/时间开始每隔几分钟重复一次?
例如,假设下面的dateTimeReminder.date为09/08 11 AM,请从这个时间开始每隔15分钟触发本地通知。
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分钟一次”呢?非常感谢您提供任何帮助。
类似问题 - 如何在iOS 10 UserNotifications上设置NSCalendarUnitMinute repeatInterval?

嘿,同类型的问题:https://dev59.com/kZ7ha4cB1Zd3GeqPkoU6@DS.:请帮忙。 - Abhishek Thapliyal
7个回答

5

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)

3
经过搜索,我得出结论,目前Swift 3不支持此功能。对于所有寻求此功能的人,我建议暂时使用UILocalNotification(尽管在iOS 10中已弃用),但以后可以迁移到UNUserNotification一旦支持此功能。以下是一些额外的问题和资源,这些帮助我得出这个结论。请注意,要获取更多关于它所涉及的特定方案的见解,请遵循本主题中的所有答案和评论。
  • 通常使用弃用的API是一个不好的想法。作为一般实践,应尽快迁移到新的API。上述解决方案不建议作为永久解决方案。

每两周本地通知

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

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


你现在找到任何方法来做这件事了吗? - siva krishna
@sivakrishna 不,还没有。 - DS.

3

如您所知,每个应用程序最多可以安排64个通知。如果添加的通知数超过了该限制,系统将保留最先触发的64个通知,并且将删除其他通知。

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

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

为了进行休息,添加一个 NSTimer 来设置 X 分钟并发送通知。

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
}

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


这并没有提供开始日期。它会立即安排并每 x 分钟重复一次。编辑 - 明确说明 - 它将在现在的 60 秒后开始,然后每分钟重复一次。 - DS.
请查看我的上面评论。另外,请参阅此处的讨论- https://dev59.com/Kpfga4cB1Zd3GeqPD_5T#37804344?noredirect=1#comment66109521_37804344 - DS.
嘿,同类型问题:https://dev59.com/kZ7ha4cB1Zd3GeqPkoU6。请帮忙:@gurmandeep - Abhishek Thapliyal

2
这是我如何基于时间间隔设置本地通知并重复执行自定义的“贪睡”和删除方法。原始答案翻译为“最初的回答”。
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])

它适用于重复通知,谢谢!我已经为你投了赞成票。 - Rajput Nancy Katal
它适用于重复通知,谢谢!我已经为你点赞了。 - undefined
我需要在应用程序终止时收到通知,并每72小时重复通知。它正常工作,但是我的应用程序在后台运行并重新启动设备后,通知不起作用,直到我手动打开应用程序。我是否遗漏了什么? - Rajput Nancy Katal
我需要在应用程序终止时收到通知,并每隔72小时重复通知。目前它的工作正常,但是当我的应用程序在后台运行并重新启动设备后,通知就不起作用了,直到我手动打开应用程序。我是否遗漏了什么? - undefined

0

6
假如用户对第一条通知做出了回应,此情况成立。如果用户忽略了这条通知,该应用将不会收到第一条通知! - ragnarius

0

这并没有提供开始日期。它会立即安排并每 x 分钟重复一次。 - DS.
当通知运行时,您可以启动计时器。不完全确定您的意思。 - key9060
1
我尝试过了,但“当通知运行时可以开始”是不可行的。新API中只有两种方法-willAppear和didReceive。其中一个仅在您的应用程序在前台时触发。另一个仅在用户与您的通知交互时触发。很可能两者都不会发生。这意味着通知出现并停留在通知中心。那么你怎么启动计时器呢? - DS.

0
您分享的链接和添加的新细节需要保存通知计划的上一个小时和分钟。然后,在计时器跳转到下一分钟时,更改小时和分钟。
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显示了一个示例,通过仅指定小时和分钟日期组件并设置触发器重复来构建每天早上8:30触发的触发器。https://developer.apple.com/reference/usernotifications/uncalendarnotificationtrigger - DS.
我的场景是 - “从9月8日上午11点开始,每15分钟触发本地通知”。 - DS.
你需要设置自定义算法,并使通知每15分钟弹出一次,即它将从8:00开始,然后是8:15等等。计时器会有所帮助,而你非常清楚如何使用通知。 - gurmandeep

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接