pushdeer/ios/PushDeer-iOS/PushDeer/AppDelegate.swift

73 lines
2.8 KiB
Swift
Raw Normal View History

2022-01-04 22:28:18 +08:00
//
// AppDelegate.swift
// PushDeer
//
// Created by HeXiaoTian on 2021/12/31.
//
import UIKit
import UserNotifications
import IQKeyboardManagerSwift
2022-01-04 22:28:18 +08:00
@MainActor
2022-01-04 22:28:18 +08:00
class AppDelegate: NSObject, UIApplicationDelegate, UNUserNotificationCenterDelegate {
2022-01-04 22:28:18 +08:00
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
//
2022-01-04 22:28:18 +08:00
let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options: [.badge, .sound, .alert]) { granted, error in
print("注册通知结果: \(granted) - \(String(describing: error))")
}
application.registerForRemoteNotifications()
//
2022-01-04 22:28:18 +08:00
Task {
let notFirstStart = UserDefaults.standard.bool(forKey: "PushDeer_notFirstStart")
if !notFirstStart {
// APP, , . ()
_ = try await HttpRequest.fake()
UserDefaults.standard.set(true, forKey: "PushDeer_notFirstStart")
}
2022-01-04 22:28:18 +08:00
}
// IQ
IQKeyboardManager.shared.enable = false //
IQKeyboardManager.shared.enableAutoToolbar = true //
#if !targetEnvironment(macCatalyst) && !APPCLIP && !SELFHOSTED
// Mac and AppClip and
//
WXApi.registerApp(Env.wxAppid, universalLink: Env.wxUniversalLink)
#endif
2022-01-04 22:28:18 +08:00
return true
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
print("deviceToken: ", deviceTokenString)
AppState.shared.deviceToken = deviceTokenString
2022-01-04 22:28:18 +08:00
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("didFailToRegisterForRemoteNotificationsWithError: ", error)
}
2022-01-04 22:28:18 +08:00
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification) async -> UNNotificationPresentationOptions {
NSLog("willPresent: %@", notification.request.content.userInfo)
Task {
// ,
let messageItems = try await HttpRequest.getMessages().messages
try MessageModel.saveAndUpdate(messageItems: messageItems)
}
2022-01-04 22:28:18 +08:00
return [.sound, .list, .banner]
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse) async {
NSLog("didReceive: %@", response.notification.request.content.userInfo)
2022-01-04 22:28:18 +08:00
}
}