我有一个支持多个窗口的应用程序(iPadOS 13+),我想知道响应用户点击通知的正确方法。我想基于用户点击的通知来设置用户界面。
我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()
}
在名为“使用多个窗口定位内容”的会话中,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`
}
如果我的应用程序未终止,则不会调用此函数。
似乎UNNotificationResponse
有一个名为的属性targetScene
,应该根据该属性确定要更新的场景:https : //developer.apple.com/documentation/usernotifications/unnotificationresponse/3255096-targetscene
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句