gpt4 book ai didi

ios - 如何在 iOS 10 UserNotifications 上设置 NSCalendarUnitMinute repeatInterval?

转载 作者:IT王子 更新时间:2023-10-29 05:44:21 25 4
gpt4 key购买 nike

在 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];

最佳答案

虽然看起来旧的通知框架对此有更好的支持,但直到我们意识到新的更好!

我遇到了类似的问题,下面是旧通知如何与新通知齐头并进的示例。

为了安全起见,这是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()

因为这对我有用,所以它可能对你们其他人也适用。如果有问题,请尝试让我知道。

而且,对于这个问题的确切解决方案,我建议如下。

(大概苹果没有想过这样的场景,但是可以处理)

  1. 安排晚上 8:30 的通知
  2. 当通知被触发时,将其移除并安排另一个具有不同标识符的通知,用于上面显示的 NSCalendarUnitMinute 等价物。
  3. 瞧,成功了!! (或者没有?)

关于ios - 如何在 iOS 10 UserNotifications 上设置 NSCalendarUnitMinute repeatInterval?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37804287/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com