首页 > 解决方案 > 如何使苹果设备上的通知点击重定向到链接?

问题描述

我成功地将 APNs 发送到苹果设备。我已经在 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.

标签: react-nativeapple-push-notifications

解决方案


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

推荐阅读