调整推送

This commit is contained in:
Fin 2020-05-26 10:17:46 +08:00
parent 2db35c6048
commit a60ab895d7

View File

@ -13,7 +13,7 @@ import RealmSwift
import IceCream
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate{
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate{
var window: UIWindow?
var syncEngine: SyncEngine?
@ -34,6 +34,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate{
UNUserNotificationCenter.current().getNotificationSettings { (settings) in
dispatch_sync_safely_main_queue {
if settings.authorizationStatus == .authorized {
UNUserNotificationCenter.current().delegate = self
Client.shared.registerForRemoteNotifications()
}
}
@ -77,82 +78,87 @@ class AppDelegate: UIResponder, UIApplicationDelegate{
//
Client.shared.bindDeviceToken()
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
let navigationController = ((self.window?.rootViewController as? BarkSnackbarController)?
.rootViewController as? BarkNavigationController)
func presentController(){
let alert = (userInfo["aps"] as? [String:Any])?["alert"] as? [String:Any]
let title = alert?["title"] as? String
let body = alert?["body"] as? String
let url:URL? = {
if let url = userInfo["url"] as? String {
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{
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 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)
}
if let presentedController = navigationController?.presentedViewController {
presentedController.dismiss(animated: false) {
presentController()
}
}
else{
presentController()
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
notificatonHandler(userInfo: notification.request.content.userInfo)
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
notificatonHandler(userInfo: response.notification.request.content.userInfo)
}
private func notificatonHandler(userInfo:[AnyHashable:Any]){
let navigationController = ((self.window?.rootViewController as? BarkSnackbarController)?
.rootViewController as? BarkNavigationController)
func presentController(){
let alert = (userInfo["aps"] as? [String:Any])?["alert"] as? [String:Any]
let title = alert?["title"] as? String
let body = alert?["body"] as? String
let url:URL? = {
if let url = userInfo["url"] as? String {
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{
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 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)
}
if let presentedController = navigationController?.presentedViewController {
presentedController.dismiss(animated: false) {
presentController()
}
}
else{
presentController()
}
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.