Ios 如何设置UNNotificationRequest';什么是自定义重复间隔?
我正在实现一个自定义的定期提醒,比如默认提醒应用程序。在这里,我不知道如何设置重复间隔,例如,从现在开始,每隔n天/周/月/年触发一次,即每4天/周/月/年触发一次通知 这是我对间隔=1的实现Ios 如何设置UNNotificationRequest';什么是自定义重复间隔?,ios,swift,local,nsnotificationcenter,unusernotificationcenter,Ios,Swift,Local,Nsnotificationcenter,Unusernotificationcenter,我正在实现一个自定义的定期提醒,比如默认提醒应用程序。在这里,我不知道如何设置重复间隔,例如,从现在开始,每隔n天/周/月/年触发一次,即每4天/周/月/年触发一次通知 这是我对间隔=1的实现 if type == .daily{ dateComponents = Calendar.current.dateComponents([.hour, .minute], from: Date()) }
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())
}
不幸的是,UNNotificationRequest没有为本地通知设置自定义重复间隔的接口
您的问题似乎没有稳定的工作解决方案,但作为一种解决方法,您可以尝试通过请求多个本地通知(使用
直到IntervalNotificationTrigger
)来实现自定义重新调度逻辑根据您的重复间隔,然后在每次应用程序运行时重新安排新通知。不幸的是,UNNotificationRequest没有为本地通知设置自定义重复间隔的接口
您的问题似乎没有稳定的工作解决方案,但作为一种解决方法,您可以尝试通过请求多个本地通知(使用
直到IntervalNotificationTrigger
)来实现自定义重新调度逻辑根据您的重复间隔,然后在每次应用程序运行时重新安排新通知。在我前面,我不能使用直到IntervalNotificationTrigger
。例如,如果我将间隔设置为17个月。第17个月可能包含29/28/30/31天。因此,UNTimeIntervalNotificationTrigger
可能不起作用。您可以使用当前日历获取适当的时间间隔calendar.current.date(通过添加:.month,value:17,to:date())?.TimeIntervalenceNow
。此外,您还可以使用自动更新CurrentCalendar
跟踪日历更改,并在需要时重新安排通知如果用户在通知中心删除通知,该怎么办。如果是这样的话,说明我们不能重新安排通知,对吗?是的,这就是为什么我在前面说这不是稳定的工作解决方案,在IntervalNotificationTrigger之前我不能使用。例如,如果我将间隔设置为17个月。第17个月可能包含29/28/30/31天。因此,UNTimeIntervalNotificationTrigger
可能不起作用。您可以使用当前日历获取适当的时间间隔calendar.current.date(通过添加:.month,value:17,to:date())?.TimeIntervalenceNow
。此外,您还可以使用自动更新CurrentCalendar
跟踪日历更改,并在需要时重新安排通知如果用户在通知中心删除通知,该怎么办。如果是这样的话,这意味着我们不能重新安排通知时间,对吗?是的,这就是为什么我说这不是一个稳定的工作解决方案