我的目标是设置一个在未来N秒后第一次发生的通知,然后每N秒重复一次。
但是,创建重复通知似乎会触发UNUserNotificationCenterDelegate
立即地。
应用程序委托:
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
Thanks!
问题可能是您没有设置标题UNMutableNotificationContent()
.
content.title = "Title goes Here"
另外,要查看添加请求是否出错,可以添加以下代码。
center.add(request) { (error : Error?) in
if let theError = error {
// Handle any errors
}
}
我从苹果的文档中得到了这个 -https://developer.apple.com/reference/usernotifications/unmutablenotificationcontent
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)