【问题标题】:Open custom url when user clicks push notifications in swift当用户在 swift 中单击推送通知时打开自定义 url
【发布时间】:2020-04-15 13:52:12
【问题描述】:

我正在尝试使用 WKWebView 和 Firebase Push 开发 IOS / Swift 应用程序。我想使用 PHP 发送通知,当用户单击通知以在 webview 中打开自定义 URL 时。默认网址是

let url = URL(string: "https://mywebsite.com/index.php?token=\(token)")!

我想在这个 url 中传递一个像这样的 id

let url = URL(string: "https://client.gazduire.net/app3/index.php?token=\(token)&ntid=(id that is send with push notification, ex.:1)")!

我在 appdelegate.swift 中的代码

 func userNotificationCenter(_ center: UNUserNotificationCenter,didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
  print("Message ID: \(messageID)")
}
// Print full message.
print(userInfo)

let notificationName = Notification.Name("test")
NotificationCenter.default.post(name: notificationName, object: nil,userInfo: userInfo)

let ntid = userInfo["ntid"] as! String
print("\(ntid)")


completionHandler()

}

ViewController.swift

@objc func test() {
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    let ntid = appDelegate.ntid
    let token = Messaging.messaging().fcmToken


guard let url = URL(string: "https://mywebsite.com/index.php?token=\(token ?? "")&ntid=\(ntid)")      else {
   print("Invalid URL")
   return
   }

    let request = URLRequest(url: url)
    webView.load(request)
}

当用户点击推送通知时,我可以将 ntid(ntid 已接收并且打印正常)从 appdelegate 发送到 viewcontroller 吗?谢谢!

【问题讨论】:

    标签: php ios swift firebase push-notification


    【解决方案1】:

    简单来说,只需在视图控制器的层次结构中找到您的视图控制器,将“ntid”值传递给存储的属性并调用“test”方法。

    【讨论】:

    • 你能举个简单的例子吗?谢谢!我是 Swift 语言的新手!
    【解决方案2】:

    首先定义枚举为

    enum Identifiers {
      static let viewAction = "cat1"
      static let newsCategory = "cat2"
    }
    

    然后在你的 UNUserNotificationCenterDelegate 中

    extension AppDelegate: UNUserNotificationCenterDelegate {
      func userNotificationCenter(
        _ center: UNUserNotificationCenter,
        didReceive response: UNNotificationResponse,
        withCompletionHandler completionHandler: @escaping () -> Void) {
    
        // 1
        let userInfo = response.notification.request.content.userInfo
    
        // 2
        if let aps = userInfo["aps"] as? [String: AnyObject],
          let newsItem = NewsItem.makeNewsItem(aps) {
    
          (window?.rootViewController as? UITabBarController)?.selectedIndex = 1
    
          // 3
          if response.actionIdentifier == Identifiers.viewAction,
            let url = URL(string: newsItem.link) {
            let safari = SFSafariViewController(url: url)
            window?.rootViewController?.present(safari, animated: true,
                                                completion: nil)
          }
        }
    
        // 4
        completionHandler()
      }
    }
    

    像这样在推送服务中添加类别

    {
      "aps": {
        "alert": "messaga",
        "sound": "default",
        "link_url": "www.google.com",
        "category": "cat1",
    
      }
    }
    

    它将检查 aps 类型,然后如果类别类型为 1 或 2,则推送视图控制器。 你可以通过这种方式添加很多你想要的

    【讨论】:

    • 我有以下错误:使用未解析的标识符'NewsItem'
    • NewsItem 仅用于示例。使用您自己的结构。在示例代码中,当用户单击通知并显示新闻时,标签栏会发生变化
    猜你喜欢
    • 2017-10-14
    • 2016-02-15
    • 1970-01-01
    • 2017-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多