首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在iOS 10 UserNotifications上设置NSCalendarUnitMinute repeatInterval?

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

Stack Overflow用户
提问于 2016-06-14 14:18:03
回答 3查看 4.7K关注 0票数 9

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

以下代码将在晚上8:30安排本地通知,并在每隔一分钟重复一次。

代码语言:javascript
复制
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];
EN

回答 3

Stack Overflow用户

发布于 2016-09-30 14:00:57

尽管旧的通知框架似乎对此有更好的支持,但直到我们意识到新的通知框架更好!

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

为了安全起见,这里是Swift2.3

代码语言:javascript
复制
// 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
            }
        }
    }

虽然本文不是详尽的,但我几乎涵盖了从一分钟到一年的所有内容。

更详细地说,

代码语言:javascript
复制
// 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 PM
  2. 安排一个通知,删除该通知,然后为上面显示的NSCalendarUnitMinute等效项安排另一个具有不同标识符的通知。
  3. Voila,它可以工作!!(或者不是?)
票数 3
EN

Stack Overflow用户

发布于 2016-06-14 14:21:46

UNCalendarNotificationTriggerDateComponents一起使用,而不是NSCalendar

代码语言:javascript
复制
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支持您想要的行为(更改重复通知的频率)。

票数 1
EN

Stack Overflow用户

发布于 2016-09-27 21:04:14

请耐心等待,这是我的第一篇文章。

我们需要在iOS10中为我们的应用程序提供1分钟的重复间隔,并使用新旧框架的组合作为变通方法。

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

UILocalNotification *ln = [UILocalNotification分配初始化];

..。

ln.repeatInterval = kCFCalendarUnitMinute;

ln.userInfo = @{@"alarmID":...}

..。

[UIApplication sharedApplication scheduleLocalNotification:ln]; ]

这将创建并计划通常的重复LN,这些LN将显示为具有UNLegacyNotificationTrigger的UNNotification

我使用alarmID来连接新旧框架。

尝试:

代码语言:javascript
复制
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. 请求授权和类别注册。

希望这能有所帮助

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37804287

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档