重复触发本地通知会立即触发-如何推迟? [英] Repeating local notification is triggered immediately -- how to defer?
问题描述
我的目标是设置一条通知,该通知将在以后的N秒内第一次出现,然后每N秒重复一次.
My goal is to set a notification that will occur N seconds in the future for the first time, and then repeat every N seconds.
但是,创建重复通知似乎立即触发了 UNUserNotificationCenterDelegate
.
However, creating a repeating notification seems to trigger the UNUserNotificationCenterDelegate
immediately.
应用程序代表:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let center = UNUserNotificationCenter.current()
center.delegate = self
return true
}
func startRequest() {
let content = UNMutableNotificationContent()
content.body = bodyText
content.categoryIdentifier = categoryIdentifier
content.sound = UNNotificationSound.default()
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: true)
trigger.nextTriggerDate()
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
let center = UNUserNotificationCenter.current()
center.add(request)
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
// This callback received right after the request is made
completionHandler([.alert, .sound])
}
我可以通过以下方法来解决此问题:创建一个非重复的通知,然后在到期时开始重复通知.但是,我希望有某种方法可以指定第一个触发日期-如果内存可用,旧的通知API便具有此功能,也许我误会了这个新的API
I can work around this by creating a non-repeating notification and then starting repeating notifications when it expires. However, I was hoping there was some way to specify a first trigger date -- if memory serves right, the old notification API had this ability, and perhaps I am misunderstanding this new API
谢谢!
推荐答案
问题可能是您没有在 UNMutableNotificationContent()
上设置标题.
The issue may be that you aren't setting the title on the UNMutableNotificationContent()
.
content.title = "Title goes Here"
此外,要查看添加请求是否出错,您可以添加以下代码.
Also, to see if there is an error adding the request, you can add the following code.
center.add(request) { (error : Error?) in
if let theError = error {
// Handle any errors
}
}
我是从Apple的文档中获得的- https://developer.apple.com/reference/usernotifications/unmutablenotificationcontent
I got this from Apple's docs - https://developer.apple.com/reference/usernotifications/unmutablenotificationcontent
这篇关于重复触发本地通知会立即触发-如何推迟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!