mirror of
https://github.com/easychen/pushdeer.git
synced 2024-11-16 23:39:24 +08:00
d3050e154f
修改配置为pushdeer官方配置(包名,团队标识,api域名); 补充新添加的文字的英文翻译 (国际化); 设备和key是空列表时,加一个提示,让用户知道可以新增; 尝试解决列表删除项后UI偶尔没刷新的bug; 首次打开提示注册设备; 首次自动生成一个 key; 消息列表放到第一个位置; 推送测试时自动生成一个key; 后台进入前台后, 刷新本地消息列表; 键盘上方添加完成按钮, 用于收键盘; 发送推送测试后 自动收键盘; 及时清角标, 拯救强迫症; 为了保持一致, 设置页也改为可滑动; 添加SelfHosted配置, 可以直接一套代码跑出两种版本; 支持服务自建; 添加Env统一管理环境变量.
66 lines
2.5 KiB
Swift
66 lines
2.5 KiB
Swift
//
|
|
// AppDelegate.swift
|
|
// PushDeer
|
|
//
|
|
// Created by HeXiaoTian on 2021/12/31.
|
|
//
|
|
|
|
import UIKit
|
|
import UserNotifications
|
|
import IQKeyboardManagerSwift
|
|
|
|
class AppDelegate: NSObject, UIApplicationDelegate, UNUserNotificationCenterDelegate {
|
|
|
|
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
|
|
|
|
// 注册通知
|
|
let center = UNUserNotificationCenter.current()
|
|
center.delegate = self
|
|
center.requestAuthorization(options: [.badge, .sound, .alert]) { granted, error in
|
|
print("注册通知结果: \(granted) - \(String(describing: error))")
|
|
}
|
|
application.registerForRemoteNotifications()
|
|
|
|
// 首次网络请求提示
|
|
Task {
|
|
let notFirstStart = UserDefaults.standard.bool(forKey: "PushDeer_notFirstStart")
|
|
if !notFirstStart {
|
|
// APP首次启动后要先调一个无用接口, 用于触发国行手机的网络授权弹框, 未授权前调的接口会直接失败. (提前触发网络授权弹窗)
|
|
_ = try await HttpRequest.fake()
|
|
UserDefaults.standard.set(true, forKey: "PushDeer_notFirstStart")
|
|
}
|
|
}
|
|
|
|
// IQ键盘管理
|
|
IQKeyboardManager.shared.enable = false // 键盘与输入框的距离管理 禁用
|
|
IQKeyboardManager.shared.enableAutoToolbar = true // 键盘上方添加的工具栏 启用
|
|
|
|
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
|
|
}
|
|
|
|
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
|
|
print("didFailToRegisterForRemoteNotificationsWithError: ", error)
|
|
}
|
|
|
|
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification) async -> UNNotificationPresentationOptions {
|
|
print("willPresent:", notification.request.content.userInfo)
|
|
Task {
|
|
// 收到推送后, 刷新本地消息列表
|
|
let messageItems = try await HttpRequest.getMessages().messages
|
|
try MessageModel.saveAndUpdate(messageItems: messageItems)
|
|
}
|
|
return [.sound, .list, .banner]
|
|
}
|
|
|
|
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse) async {
|
|
print("didReceive:", response.notification.request.content.userInfo)
|
|
}
|
|
|
|
}
|