0

背景

我有一个支持多个窗口(iPadOS 13+)的应用程序,我想知道响应用户点击通知的正确方法。我想根据用户点击的通知设置 UI。

UNUserNotificationCenterDelegate在这样的共享实例上设置属性UNUserNotificationCenter

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    UNUserNotificationCenter.current().delegate = self
    return true
}

我符合UNUserNotificationCenterDelegate并实现以下方法。当用户与通知交互时,总是会触发此方法:

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    // This gets called whenever a notification is tapped, dismissed or a custom action is performed by the user from the system Notification Center UI.
    // Should my UI setup code live here?
    completionHandler()
}

在名为Targeting Content with Multiple Windows的会话中,Apple 解释说,当您的应用程序支持多个窗口时,系统将使用以下属性来帮助确定当用户点击通知时将向用户显示哪个窗口:

// An identifier for the content of the notification used by the system to customize the scene to be activated when tapping on a notification.
response.notification.request.content.targetContentIdentifier

最终,UIKit 不会就响应用户点击通知而打开哪个场景(窗口)做出任何约定,但它会尽最大努力打开我targetContentIdentifier为通知指定的最匹配的场景(窗口)。

如果用户在我的应用程序终止时点击通知,那么当我调用以下函数时,SceneDelegate我将能够访问有关通知点击内部的信息SceneDelegate

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    connectionOptions.notificationResponse
    // Should my UI setup code live here? 
    // I don't think so because `connectionOptions.notificationResponse` is sometimes nil and this method is only called once when the scene is being connected to a `UISceneSession`

}

如果我的应用程序未终止,则不会调用此函数。

问题

  • 我应该如何确定 UIKit 将打开哪个场景,以便我可以在正确的窗口中设置我的 UI?
4

1 回答 1

0

似乎UNNotificationResponse有一个名为的属性targetScene,并且应根据此属性确定要更新的场景: https ://developer.apple.com/documentation/usernotifications/unnotificationresponse/3255096-targetscene

于 2021-01-06T23:08:00.817 回答