react-native - 如何使苹果设备上的通知点击重定向到链接?
问题描述
我成功地将 APNs 发送到苹果设备。我已经在 react native 中编写了我的应用程序。当有人点击通知时,我想将他们重定向到我已配置我的应用程序以识别的深层链接 -ne://page/id
通过深层链接,我不需要帮助。如何将通知点击重定向到链接?
我已经尝试了一切。我在这里查看了官方文档 - 它没有说明网址和重定向 - https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/generating_a_remote_notification
解决方案
如需进一步参考,请在我回复后参考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"
}
}
推荐阅读
- visual-studio-code - Visual Studio Code Love2D 支持扩展检测 love.app 作为目录
- azure-pipelines - 在解决方案中构建两个项目,但只有 1 个进入放置文件夹
- javascript - 获取 div 项的内容
- wordpress - 以编程方式在多个页面中调用多个简码
- java - Firestore 实时查询 (SnapshotListener) 停止获取更新,所有其他 Firestore 查询都不会获取数据
- amazon-s3 - 如何使用 Ckeditor5 将图像上传到 AWS S3
- javascript - await 不等待异步函数返回值
- javascript - Message.fetch() 在 discord.js 中实际上做了什么
- python-3.x - 以 sudo 运行 selenium python3 脚本时 Chrome 崩溃
- python - 当我添加用于在 DF 中创建新列的分组功能时,它没有按预期工作