如需进一步参考,在我回复后您可以参考https://medium.com/@stasost/ios-how-to-open-deep-links-notifications-and-shortcuts-253fb38e1696。
当应用关闭或在后台运行更多时,点击通知横幅将触发didReceiveRemoteNotification appDelegate方法:
func application(_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable : Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
}
当应用程序在前台模式下运行时收到推送通知时,也会触发此方法。因为我们只考虑了你想在某个页面打开应用的场景,所以我们不会在前台模式下处理通知。
为了处理通知,我们将创建一个 NotificationParser:
class NotificationParser {
static let shared = NotificationParser()
private init() { }
func handleNotification(_ userInfo: [AnyHashable : Any]) -> DeeplinkType? {
return nil
}
}
现在我们可以将此方法连接到 Deeplink Manager:
func handleRemoteNotification(_ notification: [AnyHashable: Any]) {
deeplinkType = NotificationParser.shared.handleNotification(notification)
}
并完成appDelegate didReceiveRemoteNotification 方法:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
Deeplinker.handleRemoteNotification(userInfo)
}
最后一步是完成NotificationParser中的解析方法。这将取决于您的通知结构,但基本的解析技术将是相似的:
func handleNotification(_ userInfo: [AnyHashable : Any]) -> DeeplinkType? {
if let data = userInfo["data"] as? [String: Any] {
if let messageId = data["messageId"] as? String {
return DeeplinkType.messages(.details(id: messageId))
}
}
return nil
}
如果您将应用配置为支持推送通知并想对其进行测试,这里是我用来传递消息的通知:
apns: {
aps: {
alert: {
title: "New Message!",
subtitle: "",
body: "Hello!"
},
"mutable-content": 0,
category: "pusher"
},
data: {
"messageId": "1"
}
}