【发布时间】:2020-06-27 15:29:39
【问题描述】:
我在应用中使用本地通知。我每隔 1 小时、2 小时等触发一次本地通知。我想在 8 小时后停止此通知。我用来触发本地通知的代码如下:-
func localNotificationFire(timeInterval: Int) {
let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
// Enable or disable features based on authorization.
if error != nil {
print("Request authorization failed!")
} else {
print("Request authorization succeeded!")
let content = UNMutableNotificationContent() // Содержимое уведомления
content.title = ""
content.body = kNotificationReminder
content.sound = UNNotificationSound.default
content.badge = 1
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: TimeInterval(timeInterval), repeats: true)
let request = UNNotificationRequest(identifier: "notification.id.01", content: content, trigger: trigger)
// 4
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
}
}
我知道,我可以像 UNUserNotificationCenter.current().removeAllPendingNotificationRequests() 这样停止此通知
但我想自动停止此通知。
【问题讨论】:
-
您可以使用 UNCalendarNotificationTrigger 一次创建 8 个通知,只需设置准确的日期时间。请记住,您可以在 UNUserNotificationCenter 中一次拥有大约 20 个本地通知。显然这不是最好的解决方案,但我找不到更好的解决方案。
-
我不能这样做,因为我想在每个特定间隔之后触发通知,直到特定时间
-
通知将无限期继续(或直到您取消它们,但您需要在前台运行才能执行此操作),或者您可以按照@TiranUt 的建议安排所需数量的单个通知
-
感谢您的澄清,这意味着当本地通知设置为重复时,没有直接选项可以停止本地通知
标签: ios swift uilocalnotification