如何使Apple设备上的通知点击重定向到链接?

tsa*_*eht 6 apple-push-notifications react-native

我能够成功将APN发送到Apple设备。我已经在React Native中编写了我的应用程序。当有人单击通知时,我想将他们重定向到我已将我的应用配置为可识别的ne://page/id深层链接- 通过深层链接,我不需要任何帮助。如何将通知单击重定向到链接?

我从头到尾都尝试了一切。我在这里查看了官方文档-它没有提及网址和重定向-https: //developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/generating_a_remote_notification

此外,我已经使用apn-node库通过我的服务器发送通知。他们的通知文档没有url选项,只是一个urlArgs

kis*_*ker 1

如需进一步参考,在我回复后您可以参考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) {

    }
Run Code Online (Sandbox Code Playgroud)

当应用程序在前台模式运行时收到推送通知时,也会触发此方法。因为我们只考虑您想要在特定页面上打开应用程序的场景,所以我们不会讨论在前台模式下处理通知。

为了处理通知,我们将创建一个NotificationParser:

class NotificationParser {
   static let shared = NotificationParser()
   private init() { }
   func handleNotification(_ userInfo: [AnyHashable : Any]) -> DeeplinkType? {
      return nil
   }
}
Run Code Online (Sandbox Code Playgroud)

现在我们可以将此方法连接到 Deeplink Manager:

func handleRemoteNotification(_ notification: [AnyHashable: Any]) {
   deeplinkType =                       NotificationParser.shared.handleNotification(notification)
}
Run Code Online (Sandbox Code Playgroud)

并完成appDelegate didReceiveRemoteNotification方法:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
   Deeplinker.handleRemoteNotification(userInfo)
}
Run Code Online (Sandbox Code Playgroud)

最后一步是完成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
}
Run Code Online (Sandbox Code Playgroud)

如果您将应用程序配置为支持推送通知并想要测试它,那么这是我用来传递消息的通知:

apns: {
    aps: {
        alert: {
            title: "New Message!",
            subtitle: "",
            body: "Hello!"
        },
        "mutable-content": 0,
        category: "pusher"
    },
    data: {
        "messageId": "1"
    }
}
Run Code Online (Sandbox Code Playgroud)