繁体   English   中英

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

[英]How do I set an NSCalendarUnitMinute repeatInterval on iOS 10 UserNotifications?

在UILocalNotification我们使用NSCalendarUnitMinute喜欢重复.....但我无法找到iOS的10 UserNotification文档 ...我如何使用NSCalendarUnitMinute在iOS的10样重复UserNotification

这是代码,它将在晚上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()

因为这对我有用,它应该适合你们其他人。 如果有问题,请尝试告诉我。

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

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

  1. 在晚上8:30安排通知
  2. 触发通知时,对于上面显示的NSCalendarUnitMinute等效项,将其删除并安排另一个具有不同标识符的通知。
  3. 瞧,它的作品!! (或者不是吗?)

使用带有UNCalendarNotificationTriggerDateComponents而不是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参数repeats

UNCalendarNotificationTrigger文档

只是提醒Apple Docs中有一个拼写错误(截至2013年6月13日)。 如果使用NSDateComponents而不是DateComponents ,则需要在dateMatching参数中显式地将date as DateComponents

在回复您的评论时,我不相信UNCalendarNotificationTrigger支持您想要的行为(更改重复通知的频率)。

忍受我这是我的第一篇文章。

我们需要在ios 10中为我们的应用程序重复1分钟,并将其用作新旧框架组合的变通方法。

  1. 使用旧计划重复本地通知:

    UILocalNotification * ln = [[UILocalNotification alloc] init];
    ...
    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);
    }
}];
  1. 响应操作和触发通知,操纵通知中心:

使用新框架UNUserNotificationCenter方法:willPresentNotification:didReceiveNotificationResponse:

  1. 请求授权和类别注册我以通常的方式为两个框架做了。

希望这可以帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM