iOS 11-用户本地通知,每x分钟重复一次 [英] iOS 11- User local notification which repeat every x minutes
问题描述
在iOS 11中,如何实现每隔x分钟重复一次的本地通知?
In iOS 11, how can I implement a local notification which repeats every x minutes?
将从用户中选择重复间隔.举例来说,假设用户选择设置一条通知,该通知将在明天的9:00 AM触发,并从那里每2天(或两周或6个月或10分钟)触发一次
The repeating interval will be selected from the user. So for example let's say that a user choose to set a notification which will trigger tomorrow at 9:00 AM and from there it triggers every 2 days (or two weeks or 6 months or 10 minutes)
var repeatInterval = Bool()
trigger = UNCalendarNotificationTrigger(dateMatching: triggerDate, repeats: repeatInterval)
//Schedule the Notification
let identifier = titleNospace
let request = UNNotificationRequest(identifier: identifier!, content: content, trigger: trigger)
self.center.add(request, withCompletionHandler: { (error) in
if let error = error {
print(error.localizedDescription)
}
})
UNUserNotificationCenter.current().add(notificationRequest, withCompletionHandler: nil)
使用此代码,我可以在设定的日期安排通知.有人告诉我,从这里开始,如果我想安排重复通知,则应在传递通知时使用triggerInterval.但是我该怎么办呢?传递通知时,如何获取重复时间(由用户定义)的值?我可以用这个吗?:
With this code I can schedule a notification at a set date. I've been told that from here if I would like to schedule a repeat notification I should use a triggerInterval when the notification is delivered. But how can I do so? How can I get the value of the repeating time (defined by the user) when the notification is delivered? Shall I use this?:
func didReceive(_ notification: UNNotification)
我已经尝试过,但似乎从未有人打电话.
I've tried but it seems that it's never called.
我是新手,我已经尝试了很多次,但是似乎找不到解决方法.我已经能够管理每小时,每月,每天和每年的重复.如果我真的想添加自定义重复项,虽然我真的不知道该怎么做.
I'm new to swift and I've tried and tried but it seems I cannot find a solution. I've been able to manage the repeating hourly, monthly, daily and yearly. If I would like to add a custom repeat though I really wouldn't know how to do.
推荐答案
对于您的信息,您不能像每2分钟,10分钟或其他时间那样自定义重复时间间隔,必须使用
For your information you can't do customise repeat time interval like every 2 min, 10 min or Etc. you must use value of NSCalendarUnit
like
这篇关于iOS 11-用户本地通知,每x分钟重复一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!