【问题标题】:How to make notification clicks on apple devices redirect to links?如何使苹果设备上的通知点击重定向到链接?
【发布时间】:2019-09-02 20:34:07
【问题描述】:

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

我已经尝试了一切。我在这里查看了官方文档 - 它没有说明 url 和重定向 - https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/generating_a_remote_notification

此外,我使用apn-node 库通过我的服务器发送通知。他们的 notification docs 没有 url 选项,只有一个叫做 urlArgs 的东西。

【问题讨论】:

  • 它可以通过处理您的深层链接以接受 URL 作为参数来完成,但它会立即打开您的应用程序来处理 URL,然后将其重定向到网站。那怎么样?
  • @tsaebeht: 是尝试从通知中打开网站 url,还是您想在点击通知时使用 url 打开应用程序中的特定页面?

标签: react-native apple-push-notifications


【解决方案1】:
  • node-apn 的负载对象中设置 URL,如下所示:
notification.payload = {
  url: "https://www.google.com"
}
  • 从 AppDelegate 的 didReceiveRemoteNotification 委托方法中的 userInfo 对象解析 URL

  • 在应用内 WebView 或 Safari 中打开 URL

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2015-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 2022-08-18
      • 1970-01-01
      相关资源
      最近更新 更多