Ios 我如何每天每小时重复本地通知?
我在谷歌做了研究,为我的应用程序(洗手提醒)创建了本地通知,但我认为我犯了一个错误,因为我把它设置在每天上午8:00到晚上10:00之间,但它在上午9:00发送了第一个通知,但在10:00时没有重复 用户界面截图将帮助您了解我想做什么 功能:Ios 我如何每天每小时重复本地通知?,ios,swift,Ios,Swift,我在谷歌做了研究,为我的应用程序(洗手提醒)创建了本地通知,但我认为我犯了一个错误,因为我把它设置在每天上午8:00到晚上10:00之间,但它在上午9:00发送了第一个通知,但在10:00时没有重复 用户界面截图将帮助您了解我想做什么 功能: func createNotify(){ let now = Date().shortTime if now < endTime.shortTime{ let content = UNMutableNotific
func createNotify(){
let now = Date().shortTime
if now < endTime.shortTime{
let content = UNMutableNotificationContent()
content.title = titles.randomElement()!
content.body = contents.randomElement()!
content.sound = UNNotificationSound.default
let interval:TimeInterval = 3600.0 // 1 minute = 60 seconds
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: interval, repeats: true)
let request = UNNotificationRequest(identifier:"washHands", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request)
}else{
print("do nothing")
}
}
使用UNTimeIntervalNotificationTrigger
而不是UNCalendarNotificationTrigger
,因为UNCalendarNotificationTrigger
对象用于在指定日期和时间安排本地通知的传递,而UNTimeIntervalNotificationTrigger
对象用于在需要时在指定的秒数之后安排本地通知的传递
你不应该改用它吗?它每天会触发一次,你必须创建24个通知并将它们全部设置为重复。谢谢@Catherine,现在它似乎可以工作,我将测试它直到一天结束:)@HalilİbrahimYÜCE你需要手动检查结束时间,并在超过结束时间后取消通知。不知道为什么这一点没有在报告中提到answer@JoakimDanielson是的,我添加了一个if-else条件来检查它,现在看起来很有效:)但是结束时间还有一个小时,我们会看看发生了什么:)@Joakim Danielson你说得对,需要手动检查结束时间,一旦结束时间过了就取消。我将更新答案。@JoakimDanielson我设置了结束时间16:00,但几分钟前(16:50),我仍然收到一个新的通知。如果其他情况似乎不起作用,您建议什么?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
center.getNotificationSettings { (settings) in
if settings.authorizationStatus != .authorized {
self.center.requestAuthorization(options: self.options) { (accepted, error) in
if !accepted {
print("Error")
}
}
}
}
center.delegate = self
BGTaskScheduler.shared.register(
forTaskWithIdentifier: "com.labters.korona-evdekal.washHands",
using: DispatchQueue.global()
) { task in
self.scheduleLocalNotification()
}
FirebaseApp.configure()
ApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions)
return true
}
func scheduleLocalNotification() {
let request = BGProcessingTaskRequest(identifier: "com.labters.korona-evdekal.washHands")
request.requiresNetworkConnectivity = false
request.requiresExternalPower = false
request.earliestBeginDate = Date(timeIntervalSinceNow: 3600.0)
do {
try BGTaskScheduler.shared.submit(request)
HealthView().createNotify()
} catch {
print("Could not schedule notification: (error)")
}
}
func userNotificationCenter(
_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions)
-> Void) {
completionHandler([.alert, .badge, .sound])
}
let interval:TimeInterval = 60.0 // 1 minute = 60 seconds
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: interval, repeats: true)