【问题标题】:How to set UNNotificationRequest's custom repeat interval?如何设置 UNNotificationRequest 自定义重复间隔?
【发布时间】:2019-11-13 02:53:57
【问题描述】:

我正在实施自定义重复提醒,例如默认提醒应用。在这里,我不介绍如何设置重复间隔,例如每隔 N 天/周/月/年触发一次,即从现在开始每隔 4 天/周/月/年触发一次通知。

这是我对区间 = 1 的实现

    if type == .daily{                        
        dateComponents = Calendar.current.dateComponents([.hour, .minute], from: Date())            
    }else if type == .weekly{
        dateComponents = Calendar.current.dateComponents([.hour, .minute,.weekday], from: Date())
    }else if type == .monthly{
        dateComponents = Calendar.current.dateComponents([.day,.hour, .minute], from: Date())

    }else if type == .yearly{
        dateComponents = Calendar.current.dateComponents([.hour, .minute,.day,.month], from: Date())
    }

【问题讨论】:

    标签: ios swift local nsnotificationcenter unusernotificationcenter


    【解决方案1】:

    很遗憾,UNNotificationRequest 没有为本地通知设置自定义重复间隔的界面。

    您的问题似乎没有稳定的工作解决方案,但作为一种解决方法,您可以尝试通过根据您的重复间隔请求多个本地通知(使用UNTimeIntervalNotificationTrigger)然后重新安排来实现您的自定义重新安排逻辑每次应用开始运行时都会收到新通知。

    【讨论】:

    • 在我的前面,我不能使用UNTimeIntervalNotificationTrigger。例如,如果我将间隔设置为 17 个月。第 17 个月可能包含 29/28/30/31 天。所以UNTimeIntervalNotificationTrigger 可能不起作用
    • 您可以使用当前日历获取适当的时间间隔Calendar.current.date(byAdding: .month, value: 17, to: Date())?.timeIntervalSinceNow。您还可以使用autoupdatingCurrentCalendar 跟踪日历更改并在需要时重新安排通知
    • 如果用户在通知中心删除了通知怎么办。如果是这种情况意味着我们不能重新安排通知,对吗?
    • 是的,这就是为什么我说这不是稳定的工作解决方案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多