我在App委托中的didReceiveRemoteNotification是
private func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
print("UserInfo: \(userInfo)")
switch application.applicationState {
case .active:
let content = UNMutableNotificationContent()
if let title = userInfo["title"]
{
content.title = title as! String
}
content.userInfo = userInfo
content.sound = UNNotificationSound.default
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 0.5, repeats: false)
let request = UNNotificationRequest(identifier:"rig", content: content, trigger: trigger)
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().add(request) { (error) in
if let getError = error {
print(getError.localizedDescription)
}
}
case .inactive:
break
case .background:
break
}
}
推送通知后的响应是
[AnyHashable("google.c.a.e"): 1, AnyHashable("content"): https://staging.travelsheriff.de, AnyHashable("aps"): {
alert = {
body = "Friend Request";
title = "Dave has sent you friend request";
};
category = "https://staging.travelsheriff.de";
sound = default;
}, AnyHashable("gcm.notification.url"): https://staging.travelsheriff.de]
我必须将“内容”存储为字符串,然后将其传递到我的视图控制器(作为url)进行显示。我该如何执行呢?...
答案 0 :(得分:0)
在您的didReceiveRemoteNotification
方法中,
if let url = userInfo["category"] as? String{
NotificationCenter.default.post(name: Notification.Name("url"), object: url)
}
在您的ViewController中,将此行添加到viewDidLoad
NotificationCenter.default.addObserver(self, selector: #selector(self.gotUrl(string:)), name: Notification.Name(rawValue: "url"), object: nil)
然后实现此方法:
@objc func gotUrl(string : String){
print(string)
}
如果要保存,请使用didReceiveRemoteNotification
方法
if let url = userInfo["category"] as? String{
UserDefaults.standard.set(url, forKey: "url")
}
然后撤退:
if let url = UserDefaults.standard.value(forKey: "url") as? String {
print(url)
}
答案 1 :(得分:0)
为了从推送通知中获取网址,请在您的应用程序委托中尝试 (这将保存来自推送通知响应的内容(URL)。)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let content: UNNotificationContent = response.notification.request.content
let userInfo = content.userInfo
if let url = userInfo["content"] as? String{
NotificationCenter.default.post(name: Notification.Name("url"), object: url)
UserDefaults.standard.set(url, forKey: "urlNotification")
let storyBoard : UIStoryboard = UIStoryboard(name: "yourStoryboardName", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "yourPushViewController") as! yourPushViewController
window?.rootViewController = nextViewController
}
completionHandler()
}
然后在推送视图控制器中像这样在viewdidload中调用
override func viewDidLoad() {
super.viewDidLoad()
pushUrl()// this is function to call the notification url in web view
}
func pushUrl() {
let getPushURL = UserDefaults.standard.value(forKey: "urlNotification") as? String
if let url = URL(string: getPushURL) {
let request = URLRequest(url: url)
webView.load(request)
}
}