Swift - 在应用程序后台收到本地通知时执行函数

Swift - Execute function when receive Local Notification in app in background

我正在创建一个应用程序,用户在特定时间间隔内安排一些任务,应用程序发送本地通知来提醒用户。

我正在使用 UNUserNotificationCenter 来安排通知,一切正常,但每次收到通知时我都需要执行一个函数,将通知数据保存在数据库中。

如果我只在用户点击通知时使用 userNotificationCenter (_: didReceive: withCompletionHandler :) 功能。

如果我使用 userNotificationCenter (_: willPresent: withCompletionHandler :) 函数,我只能在应用程序 运行ning 时 运行。

当应用程序在后台收到通知并且用户没有点击时,使用上面的这两个选项我无法保存到数据库。有谁知道我可以调用这种情况的任何其他功能?或者在收到通知时执行功能的方式。

你不能 :( UserNotification 是为了向用户显示一些信息,而不是在应用程序中安排任务。希望这会在 iOS 13 中改变 - 正如 Apple 所说: "iOS 13 adds a new background tasks API to the platform which allows apps to schedule activities to run in the background without forcing users to open the app and let it run in the foreground."