用户在swift中点击firebase通知后如何显示警报?
Posted
技术标签:
【中文标题】用户在swift中点击firebase通知后如何显示警报?【英文标题】:How to show alert after user tap firebase notification in swift? 【发布时间】:2017-07-10 10:02:18 【问题描述】:当用户在 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)
当我在调度中登录时,日志正在显示。 当我通过单击按钮触发该功能时,会显示警报。
【问题讨论】:
【参考方案1】:将函数 x() 导出到 AppDelegate。 比写而不是
self.present(responsealert, animated: true, completion: nil)
这个:
self.window?.rootViewController?.present(responsealert, animated: true, completion: nil)
由于 AppDelegate 没有你必须使用的视图
window?.rootViewController?
【讨论】:
以上是关于用户在swift中点击firebase通知后如何显示警报?的主要内容,如果未能解决你的问题,请参考以下文章
当用户点击 Flutter App 中的 Firebase 通知时如何打开链接?