我在用户点击ios中的firebase通知时发出了问题。当用户点击通知时,我将用户重定向到特定功能。它成功访问该功能但无法显示我定义的警报。 这是我的代码:
AppDelegate.swift
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
if(String(describing: userInfo) != "[:]"){
print("userInfo : \(userInfo)")
print("firebase 3")
var userData = userInfo as NSDictionary? as? [AnyHashable: AnyObject] ?? [:]
let responseAlert = (userData["aps"]! as! NSDictionary)["alert"]! as! NSDictionary
let title = responseAlert["title"]! as! String
if(title == "x"){
viewController.x()
}
}
completionHandler()
}
ViewController.swift
func x(){
let a = (UserDefaults.standard.string(forKey: "a"))!
let b = (UserDefaults.standard.string(forKey: "b"))!
if Reachability.isConnectedToNetwork() == true {
if let url = URL(string: UrlHelper.url_x){
let request = NSMutableURLRequest(url:url)
request.httpMethod = "POST";
let paramString = "&a="+a+"&b="+b
request.httpBody = paramString.data(using: String.Encoding.utf8)
var responseString: NSString!
let task = URLSession.shared.dataTask(with:request as URLRequest){
data, response, error in
if error != nil{
let responsealert = UIAlertController(title: "Error", message: "Service Not Available" , preferredStyle: UIAlertControllerStyle.alert)
responsealert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
self.present(responsealert, animated: true, completion: nil)
}else{
do {
if let convertedJsonIntoDict = try JSONSerialization.jsonObject(with: data!, options: []) as? NSArray {
print(convertedJsonIntoDict)
if (((convertedJsonIntoDict[0] as! NSDictionary)["status"] as! String)) == "0" {
DispatchQueue.main.async(execute: {
let message = ((convertedJsonIntoDict[0] as! NSDictionary)["result"]! as! NSDictionary)["message"]! as! String
let responsealert = UIAlertController(title: "Error", message: message , preferredStyle: UIAlertControllerStyle.alert)
responsealert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
self.present(responsealert, animated: true, completion: nil)
})
}else if (((convertedJsonIntoDict[0] as! NSDictionary)["status"] as! String)) == "1" {
let newX = ((convertedJsonIntoDict[0] as! NSDictionary)["result"]! as! NSDictionary)["new_x"]! as! String
DispatchQueue.main.async(execute: {
let responsealert = UIAlertController(title: "Success", message: "You got new data" , preferredStyle: UIAlertControllerStyle.alert)
responsealert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
self.present(responsealert, animated: true, completion: nil)
})
}
}
} catch let error as NSError {
print(error.localizedDescription)
}
}
}
task.resume()
}
}else{
let connectionAllert = UIAlertView(title: "No Network Connection", message: "Please enable your network connection.", delegate: self, cancelButtonTitle: "OK")
connectionAllert.show()
}
}
我认为,问题出在这段代码中:
let responsealert = UIAlertController(title: "Success", message: "You got new data" , preferredStyle: UIAlertControllerStyle.alert)
responsealert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
self.present(responsealert, animated: true, completion: nil)
当我在调度中登录时,会显示日志。 当我通过单击按钮触发该功能时,警报显示。
答案 0 :(得分:1)
将函数x()导出到AppDelegate。 比写而不是
self.present(responsealert, animated: true, completion: nil)
这样:
self.window?.rootViewController?.present(responsealert, animated: true, completion: nil)
由于AppDelegate没有视图,您必须使用
window?.rootViewController?