【问题标题】:iOS 10 - Repeating notifications every "x" minutesiOS 10 - 每“x”分钟重复一次通知
【发布时间】:2016-09-08 12:52:37
【问题描述】:

在 iOS 10 中,如何将本地通知设置为从特定日期/时间开始以分钟为单位重复。

例如,从 9 月 8 日上午 11 点开始,每 15 分钟触发一次本地通知?假设下面的 dateTimeReminder.date 是 09/08 11 AM。

let dateStart = self.dateTimeNotif.date
let notifTrigger = UNCalendarNotificationTrigger.init(dateMatching: NSCalendar.current.dateComponents([.day, .month, .year, .hour, .minute], from: dateStart), repeats: true)
let notificationRequest = UNNotificationRequest(identifier: "MYNOTIF", content: notifContent, trigger: notifTrigger)

UNUserNotificationCenter.current().add(notificationRequest, withCompletionHandler: nil)

使用上面的代码,我可以安排在每小时的特定分钟,每天的特定时间等等。但是我如何把它变成“每“x”分钟”?任何帮助表示赞赏。

类似问题 - How do I set an NSCalendarUnitMinute repeatInterval on iOS 10 UserNotifications?

【问题讨论】:

标签: ios swift ios10


【解决方案1】:

Swift 3/4 和 iOS 10/11

根据这个bug 似乎没有办法使用DateComponents() 来正确重复本地通知。

您可以使用TimeInterval 更改触发器而不是此方法(如果您的时间间隔大于 60 秒,此方法有效):

let thisTime:TimeInterval = 60.0 // 1 minute = 60 seconds

// Some examples:
// 5 minutes = 300.0
// 1 hour = 3600.0
// 12 hours = 43200.0
// 1 day = 86400.0
// 1 week = 604800.0

let trigger = UNTimeIntervalNotificationTrigger(
            timeInterval: thisTime,
            repeats: true)

【讨论】:

    【解决方案2】:

    如您所知,您可以为每个应用安排最多 64 个通知。如果您添加的更多,系统将保持最快触发的 64 个通知,并丢弃另一个。

    确保安排所有通知的一种方法是首先安排前 64 个通知,然后定期(可能在每次启动应用程序或每次触发通知时)检查安排的通知数量如果少于 64 个通知,假设 n 个通知,然后安排下一个 (64 - n) 个通知。

    int n = [[[UIApplication sharedApplication] scheduledLocalNotifications] count];
    int x = 64 - n;
    // Schedule the next 'x' notifications
    

    为休息添加一个 NSTimer X 分钟来设置通知。

    override func viewDidLoad() {
        super.viewDidLoad()
        //Swift 2.2 selector syntax
        var timer = NSTimer.scheduledTimerWithTimeInterval(60.0, target: self, selector: #selector(MyClass.update), userInfo: nil, repeats: true)
        //Swift <2.2 selector syntax
        var timer = NSTimer.scheduledTimerWithTimeInterval(60.0, target: self, selector: "update", userInfo: nil, repeats: true)
    }
    
    // must be internal or public. 
    func setNotification() {
        // Something cool
    }
    

    确保删除旧的和不需要的通知。

    【讨论】:

    【解决方案3】:

    经过一番搜索,我得出的结论是 Swift 3 在这一点上不支持此功能。对于寻找此功能的每个人,我建议现在使用 UILocalNotification(尽管在 iOS 10 中已弃用),但稍后在支持此功能后迁移到 UNUserNotification。以下是一些帮助我得出这一结论的其他问题和资源。另外,请关注此线程中的所有答案和 cmets,以更深入地了解它所讨论的特定场景。

    • 使用已弃用的 API 通常是个坏主意。作为一般做法,请尽快迁移到新的 API。不建议将上述解决方案作为永久解决方案。

    Local Notification every 2 week

    http://useyourloaf.com/blog/local-notifications-with-ios-10/

    https://github.com/lionheart/openradar-mirror/issues/14941

    【讨论】:

    • 你现在有没有办法做到这一点
    • @sivakrishna 不,还没有。
    【解决方案4】:

    这就是我如何根据时间间隔重复设置本地通知并执行方法自定义贪睡和删除

    let content = UNMutableNotificationContent()
    content.title = "Time Based Local Notification"
    content.subtitle = "Its is a demo"
    content.sound = .default
    content.categoryIdentifier = "UYLReminderCategory"
    //repition of time base local notification in foreground, background, killed
    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: true)
    let request = UNNotificationRequest(identifier: "IOS Demo", content: content, trigger: trigger)
    UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
    let snoozeAction = UNNotificationAction(identifier: "Snooze", title: "Snooze", options: [])
    let deleteAction = UNNotificationAction(identifier: "UYLDeleteAction", title: "Delete", options: [.destructive])
    let cat = UNNotificationCategory(identifier: "UYLReminderCategory", actions: [snoozeAction, deleteAction], intentIdentifiers: [], options: [])
    UNUserNotificationCenter.current().setNotificationCategories([cat])
    

    【讨论】:

      【解决方案5】:

      我认为这个帖子有你要找的东西。

      Do something every x minutes in Swift

      “60.0”表示代码运行之间的秒数。只需将其更改为 x 分钟 * 60

      【讨论】:

      • 这不提供开始日期。这会立即安排并每 x 分钟重复一次。
      • 您可以在通知运行时启动计时器。不完全确定您的意思。
      • 我试过了,但是“你可以在通知运行时开始”是不可行的。新 API 中只有两个方法 - willAppear 和 didReceive。仅当您的应用程序在前台时才会触发。另一个仅在用户与您的通知交互时触发。很可能两者都不会发生。那就是通知出现并停留在通知中心。那么如何启动计时器呢?
      【解决方案6】:

      您共享的链接和添加的新详细信息需要保存安排通知的最后一小时和一分钟。然后当计时器到下一分钟时,就会改变小时和分钟

      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)
      

      【讨论】:

      • 这直接来自 Apple 文档。请注意它所说的内容——清单 1 显示了一个示例,该示例通过仅指定小时和分钟日期组件并将触发器设置为重复来构建在每天早上 8:30 触发的触发器。 developer.apple.com/reference/usernotifications/…
      • 我的方案是——“从 9 月 8 日上午 11 点开始,每 15 分钟触发一次本地通知”
      • 您必须设置您的自定义算法并让通知每 15 分钟弹出一次,即它将从 8:00 开始,然后是 8:15,依此类推。计时器会有所帮助,您非常了解如何使用通知
      【解决方案7】:

      您可以先使用UNCalendarNotificationTrigger 在特定日期触发通知,并将其repeat 属性设置为false,以便只通知一次。当您收到该通知时,使用UNTimeIntervalNotificationTrigger API 设置您要触发本地通知的时间间隔

      【讨论】:

      • 这假定用户响应了第一个通知。如果用户忽略它,App将不会收到第一条通知!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-31
      • 2013-10-19
      • 1970-01-01
      • 2012-02-08
      • 2018-03-15
      • 1970-01-01
      • 2014-12-01
      相关资源
      最近更新 更多