Browse Source

【通知推送】添加推送通知(已测试本地推送成功)

lizhe 11 months ago
parent
commit
487dc73f8b
1 changed files with 51 additions and 1 deletions
  1. 51 1
      PDF Office/PDF Master/AppDelegate.swift

+ 51 - 1
PDF Office/PDF Master/AppDelegate.swift

@@ -6,6 +6,7 @@
 //
 
 import Cocoa
+import UserNotifications
 
 @main
 class AppDelegate: NSObject, NSApplicationDelegate, iRateDelegate{
@@ -34,6 +35,47 @@ class AppDelegate: NSObject, NSApplicationDelegate, iRateDelegate{
     func applicationDidFinishLaunching(_ aNotification: Notification) {
         // Insert code here to initialize your application
         
+        //通知
+        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
+            if granted {
+                print("User authorized notifications")
+            } else {
+                print("User denied notifications")
+            }
+        }
+
+        /**本地推送测试
+         UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
+             if granted {
+                 // 创建本地推送通知
+                 let content = UNMutableNotificationContent()
+                 content.title = "Test Notification"
+                 content.body = "This is a test notification!"
+                 
+                 // 触发条件,例如延迟 5 秒触发
+                 let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
+                 
+                 // 创建通知请求
+                 let request = UNNotificationRequest(identifier: "LocalNotification", content: content, trigger: trigger)
+                 
+                 // 添加通知请求到用户通知中心
+                 UNUserNotificationCenter.current().add(request) { error in
+                     if let error = error {
+                         print("Error adding notification request: \(error)")
+                     } else {
+                         print("Local notification scheduled")
+                     }
+                 }
+             } else {
+                 print("User denied notifications")
+             }
+         }
+         **/
+        
+        DispatchQueue.main.async {
+            NSApplication.shared.registerForRemoteNotifications()
+        }
+        
 #if VERSION_DMG
         let updater = SUUpdater.shared()
         updater?.delegate = self
@@ -256,7 +298,8 @@ class AppDelegate: NSObject, NSApplicationDelegate, iRateDelegate{
 
     
     func application(_ application: NSApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
-        
+        let token = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
+        print("Device token:", token)
     }
     
     func application(_ application: NSApplication, didReceiveRemoteNotification userInfo: [String : Any]) {
@@ -861,3 +904,10 @@ extension AppDelegate {
         }
     }
 }
+
+extension AppDelegate: UNUserNotificationCenterDelegate {
+    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
+        // 处理用户点击推送通知的操作
+        completionHandler()
+    }
+}