应用程序终止时处理通知(iOS)

我正在开发接收通知的应用程序(使用苹果推送通知)。 我正在存储这些通知并在一个控制器中显示为列表。 据我所知,只要收到通知,就会调用didReceiveRemoteNotification。 当应用程序处于前台和后台时,我可以从didReceiveRemoteNotification方法在Db中存储通​​知。 但是,当应用程序终止时,我如何存储通知?

如果用户在应用程序终止时点击通知,我可以使用lauch选项存储通知。 但是,如果用户在应用程序终止时未点击通知,我该如何存储通知?

您是否尝试过使用getDeliveredNotifications(completionHandler:) ? 您可以在控制器的viewDidLoad方法中调用以下代码,在该方法中显示收到的通知。

请注意,如果用户清除通知中心收到的通知,则无效。 您必须按照您在评论中链接的问题的接受答案中提到的那样做。

 UNUserNotificationCenter.current().getDeliveredNotifications { notifications in // notifications: An array of UNNotification objects representing the local // and remote notifications of your app that have been delivered and are still // visible in Notification Center. If none of your app's notifications are // visible in Notification Center, the array is empty. // As said in the documentation, this closure may be executed in a background // thread, so if you want to update your UI you'll need to do the following: DispatchQueue.main.sync { /* or .async {} */ // update UI } }