立即触发重复本地通知 – 如何推迟?

我的目标是设置一个将在第一时间发生N秒的通知,然后每N秒重复一次。

但是,创build重复通知似乎会立即触发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]) } 

我可以通过创build一个非重复的通知,然后开始重复通知,当它到期时解决这个问题。 但是,我希望有一些方法来指定第一个触发date – 如果内存服务正确,旧的通知API有这个能力,也许我误解了这个新的API

谢谢!

问题可能是您没有在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