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

人气:329 发布:2022-09-21 标签: ios objective-c swift ios10 unnotificationrequest

问题描述

在UILocalNotification中,我们使用 NSCalendarUnitMinute 重复.....但我在iOS 10中找不到 UserNotification doc ...如何使用 NSCalendarUnitMinute 像iOS 10中的重复 UserNotification

In UILocalNotification we use NSCalendarUnitMinute like repetition ..... but I can't find in iOS 10 UserNotification doc ... How can I use NSCalendarUnitMinute like repetition in iOS 10 UserNotification?

这里的代码将在晚上8:30安排本地通知,并会在每一分钟后重复。

here is the code which will schedule local notification at 8:30 pm and will repeat after every one minute.

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

推荐答案

虽然看来旧的通知框架有更好的支持关于这一点,直到我们才意识到新的更好!!

Although it appears that the old notifications framework had better support regarding this, it's only until, we realise that the new one is better!!

我遇到了类似的问题,下面是旧通知如何发挥作用的示例与新的一起。

I had similar problems, and below is a sample of how the old notifications can go hand in hand with the new ones.

为了更安全,这是 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
            }
        }
    }

虽然这并非详尽无遗,但我几乎从一分钟到一年不等。

Although this isn't exhaustive, I've pretty much covered everything from a minute to a 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()

因为这对我有用,所以应该为你们其他人。如果有问题,请尝试让我知道。

As this works for me, it should probably for the rest of you. Please try and let me know if there is an issue.

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

And, for the exact solution to this question, I'd advise the below.

(可能,Apple没有考虑这种情况,但可以处理)

(Probably, Apple didn't think about such a scenario, but it can be handled)

安排晚上8:30的通知 触发通知时,将其删除并安排另一个具有不同标识符的通知,用于 NSCalendarUnitMinute 上面显示的等价物。 Voila,它有效!! (或者不是吗?) Schedule a notification for 8:30 PM When the notification is triggered, remove it and schedule another notification with a different identifier, for the NSCalendarUnitMinute equivalent shown above. Voila, it works!! (Or did it not?)

900