我正在尝试使用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)
}
当用户点击推送通知时,我可以从appdelegate向视图控制器发送ntid(ntid已被接收并正确打印吗?谢谢!
以一种简单的方式,只需在视图控制器的层次结构中找到您的视图控制器,将“ ntid”值传递给存储的属性并调用“测试”方法。
首先将枚举定义为
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推送视图控制器。您可以通过这种方式添加任意数量的]