点击推送时,可以对推送进行操作

This commit is contained in:
Fin 2019-05-27 18:07:15 +08:00
parent d830ad90b0
commit 176fdc0669

View File

@ -50,21 +50,71 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
Client.shared.bindDeviceToken() Client.shared.bindDeviceToken()
} }
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
if let urlStr = userInfo["url"] as? String{
if let url = URL(string: urlStr) { let navigationController = ((self.window?.rootViewController as? BarkSnackbarController)?
if ["http","https"].contains(url.scheme?.lowercased() ?? ""){ .rootViewController as? BarkNavigationController)
((self.window?.rootViewController as? BarkSnackbarController)? let alert = (userInfo["aps"] as? [String:Any])?["alert"] as? [String:Any]
.rootViewController as? BarkNavigationController)? let title = alert?["title"] as? String
.present(BarkSFSafariViewController(url: url), animated: true, completion: nil) let body = alert?["body"] as? String
} let url:URL? = {
else{ if let url = userInfo["url"] as? String {
UIApplication.shared.open(url, options: [:], completionHandler: nil) return URL(string: url)
} }
return nil
}()
//URL
if let url = url {
if ["http","https"].contains(url.scheme?.lowercased() ?? ""){
navigationController?.present(BarkSFSafariViewController(url: url), animated: true, completion: nil)
} }
else{ else{
(self.window?.rootViewController as? BarkSnackbarController)?.rootViewController.showSnackbar(text: "URL好像不对劲") UIApplication.shared.open(url, options: [:], completionHandler: nil)
} }
return
} }
let alertController = UIAlertController(title: title, message: body, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "复制内容", style: .default, handler: { (_) in
if userInfo["automaticallycopy"] as? String == "1"{
if let copy = userInfo["copy"] as? String {
UIPasteboard.general.string = copy
}
else{
UIPasteboard.general.string = body
}
}
}))
alertController.addAction(UIAlertAction(title: "更多操作", style: .default, handler: { (_) in
var shareContent = ""
if let title = title {
shareContent += "\(title)\n"
}
if let body = body {
shareContent += "\(body)\n"
}
for (key,value) in userInfo {
if ["aps","title","body","url"].contains((key as? String) ?? "") {
continue
}
shareContent += "\(key): \(value) \n"
}
var items:[Any] = []
items.append(shareContent)
if let url = url{
items.append(url)
}
let controller = UIApplication.shared.keyWindow?.rootViewController
let activityController = UIActivityViewController(activityItems: items,
applicationActivities: nil)
controller?.present(activityController, animated: true, completion: nil)
}))
alertController.addAction(UIAlertAction(title: "取消", style: .cancel, handler: nil))
navigationController?.present(alertController, animated: true, completion: nil)
} }
func applicationWillResignActive(_ application: UIApplication) { func applicationWillResignActive(_ application: UIApplication) {