在UILocalNotification中,我们像重复一样使用NSCalendarUnitMinute .....但我在iOS 10 UserNotification doc中找不到...如何在iOS 10 UserNotification中像重复一样使用NSCalendarUnitMinute
以下代码将在晚上8:30安排本地通知,并在每隔一分钟重复一次。
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = pickerDate;
localNotification.alertBody = self.textField.text;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.repeatInterval = NSCalendarUnitMinute;
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];发布于 2016-09-30 14:00:57
尽管旧的通知框架似乎对此有更好的支持,但直到我们意识到新的通知框架更好!
我也遇到过类似的问题,下面是旧的通知如何与新的通知齐头并进的示例。
为了安全起见,这里是Swift2.3。
// Retrieve interval to be used for repeating the notification
private func retrieveRepeatInterval(repeatTemp: RepeatInterval) {
switch repeatTemp {
case .Never:
if #available(iOS 10.0, *) {
toDateComponents = NSCalendar.currentCalendar().components([.Hour, .Minute, .Day, .Month, .Year], fromDate: timeTemp!)
toDateComponents.second = 0
repeatNotification = false
} else {
repeatIntervalTemp = nil
}
case .EveryMinute:
if #available(iOS 10.0, *) {
toDateComponents.second = 0
repeatNotification = true
} else {
repeatIntervalTemp = .Minute
}
case .EveryHour:
if #available(iOS 10.0, *) {
toDateComponents = NSCalendar.currentCalendar().components([.Minute], fromDate: timeTemp!)
toDateComponents.second = 0
repeatNotification = true
} else {
repeatIntervalTemp = .Hour
}
case .EveryDay:
if #available(iOS 10.0, *) {
toDateComponents = NSCalendar.currentCalendar().components([.Hour, .Minute], fromDate: timeTemp!)
toDateComponents.second = 0
repeatNotification = true
} else {
repeatIntervalTemp = .Day
}
case .EveryWeek:
if #available(iOS 10.0, *) {
toDateComponents = NSCalendar.currentCalendar().components([.Hour, .Minute, .Weekday], fromDate: timeTemp!)
toDateComponents.second = 0
repeatNotification = true
} else {
repeatIntervalTemp = .WeekOfYear
}
case .EveryMonth:
if #available(iOS 10.0, *) {
toDateComponents = NSCalendar.currentCalendar().components([.Hour, .Minute, .Day], fromDate: timeTemp!)
toDateComponents.second = 0
repeatNotification = true
} else {
repeatIntervalTemp = .Month
}
case .EveryYear:
if #available(iOS 10.0, *) {
toDateComponents = NSCalendar.currentCalendar().components([.Hour, .Minute, .Day, .Month], fromDate: timeTemp!)
toDateComponents.second = 0
repeatNotification = true
} else {
repeatIntervalTemp = .Year
}
}
}虽然本文不是详尽的,但我几乎涵盖了从一分钟到一年的所有内容。
更详细地说,
// RepeatInterval
enum RepeatInterval : String, CustomStringConvertible {
case Never = "Never"
case EveryMinute = "Every Minute"
case EveryHour = "Every Hour"
case EveryDay = "Every Day"
case EveryWeek = "Every Week"
case EveryMonth = "Every Month"
case EveryYear = "Every Year"
var description : String { return rawValue }
static let allValues = [Never, EveryMinute, EveryHour, EveryDay, EveryWeek, EveryMonth, EveryYear]
}
var repeatTemp: RepeatInterval?
var repeatIntervalTemp: NSCalendarUnit?
var timeTemp: NSDate?
var toDateComponents = NSDateComponents()由于这对我有效,可能对你们其余的人也是如此。如果有问题,请试着告诉我。
对于这个问题的确切解决方案,我建议使用下面的方法。
(也许,苹果没有考虑到这种情况,但它是可以处理的)
NSCalendarUnitMinute等效项安排另一个具有不同标识符的通知。发布于 2016-06-14 14:21:46
将UNCalendarNotificationTrigger与DateComponents一起使用,而不是NSCalendar
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)您可以使用DateComponents实例设置日期,并使用UNCalendarNotificationTrigger初始值设定项的repeats参数指定是否应重复通知。
UNCalendarNotificationTrigger Documentation
提醒一下,Apple Docs中有一个拼写错误(截止到2013年6月)。如果使用NSDateComponents而不是DateComponents,则需要在dateMatching参数中显式转换date as DateComponents。
作为对您的评论的回应,我不相信UNCalendarNotificationTrigger支持您想要的行为(更改重复通知的频率)。
发布于 2016-09-27 21:04:14
请耐心等待,这是我的第一篇文章。
我们需要在iOS10中为我们的应用程序提供1分钟的重复间隔,并使用新旧框架的组合作为变通方法。
UILocalNotification *ln = [UILocalNotification分配初始化];
..。
ln.repeatInterval = kCFCalendarUnitMinute;
ln.userInfo = @{@"alarmID":...}
..。
[UIApplication sharedApplication scheduleLocalNotification:ln]; ]
这将创建并计划通常的重复LN,这些LN将显示为具有UNLegacyNotificationTrigger的UNNotification
我使用alarmID来连接新旧框架。
尝试:
UNUserNotificationCenter* center = [UNUserNotificationCenter
currentNotificationCenter];
[center getPendingNotificationRequestsWithCompletionHandler:^(NSArray<UNNotificationRequest *> * _Nonnull requests) {
NSLog(@"----------------- PendingNotificationRequests %i -------------- ", (int)requests.count);
for (UNNotificationRequest *req in requests) {
UNNotificationTrigger *trigger = req.trigger;
NSLog(@"trigger %@", trigger);
}
}];使用新框架UNUserNotificationCenter方法: willPresentNotification: didReceiveNotificationResponse:
我用通常的方式为这两个框架做了
希望这能有所帮助
https://stackoverflow.com/questions/37804287
复制相似问题