基本上,我有一個應用程式,用戶可以在其中設定第一次通知的時間,以下會重復發生。例如,我想每 8 小時收到一次通知,但從現在開始不會第一次通知。所以我需要創建 2 個通知,一個用于第一個,一旦第一個發出,我需要每 8 小時設定一個新通知。
一個實際的例子:我會在早上 8 點吃藥,但現在是 5 點,我想每 8 小時收到一次通知。第一次鬧鈴一定要從8點到8點,也就是4點。為此,我做了一個邏輯:
但是,其他警報不能與第一個警報具有相同的時間,因為在前面的示例中,如果我留下相同的觸發器,警報將是 11 小時,而不是 8。我需要第一個是 11 小時,并且示例中的后續數字 8。
我已經有了第一個警報的代碼,但是,我想不出一種方法來創建后續警報,它將根據所需的時間不斷重復,例如 8 中的 8、12 中的 12
uj5u.com熱心網友回復:
您需要設定一個BGAppRefreshTask
,它會告訴系統定期喚醒您的應用程式以在后臺運行以在大約 30 秒內執行某些操作,然后應用程式被暫停。您可以每 6 小時設定一次重繪 任務,以便在需要時檢查并制作新的本地推送通知。
這是指南:https ://developer.apple.com/documentation/uikit/app_and_environment/scenes/preparing_your_ui_to_run_in_the_background/using_background_tasks_to_update_your_app
當您設定本地通知時:
// Create the request
let uuidString = UUID().uuidString // SAVE this one to UserDefault then you can fetch delivered/pending notification if needed.
let request = UNNotificationRequest(identifier: uuidString,
content: content, trigger: trigger)
// Schedule the request with the system.
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.add(request) { (error) in
if error != nil {
// Handle any errors.
}
}
AppDelegate.swift 中的函式
// Here you register it in didFinishLaunching()
BGTaskScheduler.shared.register(forTaskWithIdentifier: "com.example.apple-samplecode.ColorFeed.refresh", using: nil) { task in
self.handleAppRefresh(task: task as! BGAppRefreshTask)
}
// Func to register a next background refresh task
func scheduleAppRefresh() {
let request = BGAppRefreshTaskRequest(identifier: "com.example.apple-samplecode.ColorFeed.refresh")
// Fetch no earlier than 15 minutes from now.
request.earliestBeginDate = Date(timeIntervalSinceNow: 15 * 60)
do {
try BGTaskScheduler.shared.submit(request)
} catch {
print("Could not schedule app refresh: \(error)")
}
}
// Here is where you do your logic when the app runs by background refresh event.
func handleAppRefresh(task: BGAppRefreshTask) {
// Schedule a new refresh task.
scheduleAppRefresh()
// Create an operation that performs the main part of the background task.
let operation = RefreshAppContentsOperation() // you dont really need this one, just call your logic here.
// Ex:
UNUserNotificationCenter.current().getDeliveredNotifications {[weak self] listNoti in
for noti in listNoti {
if noti.identifier == [your UUID that you just save in UserDefault before] {
// It delivered, now make new Local notification...
}
}
}
// Provide the background task with an expiration handler that cancels the operation.
task.expirationHandler = {
operation.cancel()
}
// Inform the system that the background task is complete
// when the operation completes.
operation.completionBlock = {
task.setTaskCompleted(success: !operation.isCancelled)
}
// Start the operation.
operationQueue.addOperation(operation)
}
uj5u.com熱心網友回復:
對一天中的每個單獨時間使用重復UNCalendarNotificationTrigger
。在您的情況下,這意味著使用三個觸發器(因此三個請求):在 8、16 和 0 小時各一個。您無需為最快的事件添加單獨的請求。
for i in 0 ..< 3 {
let when = DateComponents(hour: (8 8 * i) % 24, minute: 0)
let trigger = UNCalendarNotificationTrigger(dateMatching: when, repeats: true)
let request = UNNotificationRequest(
identifier: "trigger-\(i)",
content: someContent(),
trigger: trigger
)
UNUserNotificationCenter.current().add(request) { error in
if let error = error {
// handle error
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/508517.html