作者:庾事镁 | 来源:互联网 | 2023-05-19 02:02
IniOS10,howcanIsetlocalnotificationstorepeatinminutesstartingfromaparticulardateti
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 个解决方案
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.
确保删除旧的和不需要的通知。