通知展示到本地通知中心
This commit is contained in:
parent
ea0e93be12
commit
379cff4c73
@ -7,30 +7,36 @@
|
||||
|
||||
import UIKit
|
||||
import UserNotifications
|
||||
import SwiftUI
|
||||
|
||||
class AppDelegate: UIResponder, UIApplicationDelegate {
|
||||
|
||||
//当应用程序启动后,可能需要进行其他逻辑处理时调用的方法
|
||||
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
|
||||
|
||||
var window: UIWindow?
|
||||
|
||||
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
|
||||
// 设置 UNUserNotificationCenter 的代理
|
||||
UNUserNotificationCenter.current().delegate = self
|
||||
|
||||
// 请求推送通知权限
|
||||
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { granted, error in
|
||||
if granted {
|
||||
DispatchQueue.main.async {
|
||||
application.registerForRemoteNotifications()
|
||||
}
|
||||
} else {
|
||||
// 在消息列表的界面可以使用这里的不变量展示让用户去手机设置界面开启通知
|
||||
print("用户拒绝通知权限: \(String(describing: error))")
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
/// 当设备成功注册推送通知时调用。用来获取设备令牌并发送到服务器
|
||||
// 当设备成功注册推送通知时调用。用来获取设备令牌并发送到服务器
|
||||
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
|
||||
// 获取 deviceToken
|
||||
let tokenParts = deviceToken.map { data in String(format: "%02.2hhx", data) }
|
||||
let token = tokenParts.joined()
|
||||
print("Device Token: \(token)")
|
||||
|
||||
// 将 deviceToken 发送到服务器
|
||||
sendDeviceTokenToServer(token: token)
|
||||
}
|
||||
|
||||
@ -40,21 +46,15 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
|
||||
}
|
||||
|
||||
private func sendDeviceTokenToServer(token: String) {
|
||||
// 服务器的 API URL
|
||||
let url = URL(string: "https://yourserver.com/api/deviceToken")!
|
||||
|
||||
// 设备信息
|
||||
let parameters: [String: Any] = [
|
||||
"deviceToken": token,
|
||||
"deviceType": "iOS"
|
||||
]
|
||||
|
||||
// 使用 URLSession 发送请求
|
||||
var request = URLRequest(url: url)
|
||||
request.httpMethod = "POST"
|
||||
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
||||
request.httpBody = try? JSONSerialization.data(withJSONObject: parameters)
|
||||
|
||||
let task = URLSession.shared.dataTask(with: request) { data, response, error in
|
||||
if let error = error {
|
||||
print("Error sending device token: \(error)")
|
||||
@ -64,4 +64,60 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
|
||||
}
|
||||
task.resume()
|
||||
}
|
||||
|
||||
// 处理接收到的通知
|
||||
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
|
||||
if #available(iOS 14.0, *) {
|
||||
completionHandler([.banner, .sound, .badge])
|
||||
} else {
|
||||
completionHandler([.alert, .sound, .badge])
|
||||
}
|
||||
}
|
||||
|
||||
// 当用户点击通知或通知到达时触发
|
||||
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
|
||||
// 从锁屏弹出消息到主屏幕
|
||||
if response.actionIdentifier == UNNotificationDefaultActionIdentifier {
|
||||
// 处理通知,跳转到特定界面
|
||||
handleNotification(response.notification)
|
||||
|
||||
// 发送本地通知
|
||||
let message = Message(text: response.notification.request.content.body, sender: response.notification.request.content.title, timestamp: "" ,isSelf: true, avatar: "avatar")
|
||||
sendNotification(for: message)
|
||||
}
|
||||
completionHandler()
|
||||
}
|
||||
|
||||
|
||||
private func handleNotification(_ notification: UNNotification) {
|
||||
// 在这里添加自定义处理逻辑,比如跳转到特定界面
|
||||
if let window = window {
|
||||
// 假设 ContentView 是你希望跳转到的视图
|
||||
let contentView = ContentView()
|
||||
|
||||
// 将 SwiftUI 视图包装为 UIViewController
|
||||
let hostingController = UIHostingController(rootView: contentView)
|
||||
|
||||
// 获取 rootViewController 并呈现新的视图
|
||||
if let rootViewController = window.rootViewController {
|
||||
rootViewController.present(hostingController, animated: true, completion: nil)
|
||||
} else {
|
||||
window.rootViewController = hostingController
|
||||
window.makeKeyAndVisible()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 发送本地通知
|
||||
private func sendNotification(for message: Message) {
|
||||
let content = UNMutableNotificationContent()
|
||||
content.title = message.sender
|
||||
content.body = message.text
|
||||
content.sound = UNNotificationSound.default
|
||||
|
||||
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
|
||||
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
|
||||
|
||||
UNUserNotificationCenter.current().add(request)
|
||||
}
|
||||
}
|
||||
|
||||
@ -17,6 +17,8 @@
|
||||
</dict>
|
||||
<key>NSLocalNotificationUsageDescription
|
||||
NSLocalNotificationUsageDescription</key>
|
||||
<string>需要通知权限以便发送新消息通知</string>
|
||||
<key>NSUserNotificationUsageDescription</key>
|
||||
<string>需要通知权限以便发送新消息通知</string>
|
||||
<key>UIBackgroundModes</key>
|
||||
<array>
|
||||
|
||||
@ -19,7 +19,7 @@ let pp = "http://43.136.169.205:9999"
|
||||
|
||||
let release = "https://api.flyaha.top"
|
||||
|
||||
let apiprefixV1 = local+"/api/v1"
|
||||
let apiprefixV1 = pp+"/api/v1"
|
||||
|
||||
|
||||
var loginApi = "/user/login"
|
||||
|
||||
Loading…
Reference in New Issue
Block a user