// // SceneDelegate+APNS.swift // Veloria // // Created by 湖南秦九 on 2025/6/11. // import UIKit import FirebaseMessaging import FirebaseCore extension AppDelegate { ///是否展示过通知提示 static var haveBeenShownAPNS = false func requestAPNS() { FirebaseApp.configure() Messaging.messaging().delegate = self let center = UNUserNotificationCenter.current() center.delegate = self center.requestAuthorization(options: [.badge, .sound, .alert]) { grant, error in if !grant { if let date = UserDefaults.standard.object(forKey: kVPApnsAlertDefaultsKey) as? Date { DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { #if DEBUG self.showApnsAlert() #else if !date.vp_isToday { self.showApnsAlert() } #endif } } else { Self.haveBeenShownAPNS = true VPAppTool.sceneDelegate?.vp_retryHandleOpenAppMessage() } UserDefaults.standard.set(Date(), forKey: kVPApnsAlertDefaultsKey) } else { Self.haveBeenShownAPNS = true VPAppTool.sceneDelegate?.vp_retryHandleOpenAppMessage() } self.uploadNoticeStatus() } UIApplication.shared.registerForRemoteNotifications() } private func showApnsAlert() { let alert = VPAnpsAlertView(title: "veloria_open_notice_at_watch_video".localized, subtitle: "veloria_open_notice_alert_text".localized, icon: UIImage(named: "alert_icon_03"), normalButtonText: nil, highlightButtonText: "veloria_allow".localized) alert.show() alert.clickHighlightButton = { [weak self] in guard let _ = self else { return } VPAppTool.openApnsSetting() Self.haveBeenShownAPNS = true } alert.clickCloseButton = { [weak self] in guard let _ = self else { return } Self.haveBeenShownAPNS = true VPAppTool.sceneDelegate?.vp_retryHandleOpenAppMessage() } } ///更新通知状态 func uploadNoticeStatus() { UNUserNotificationCenter.current().getNotificationSettings { settings in if settings.authorizationStatus == .authorized { VPRewardsAPI.requestUploadNoticeStatus(status: true) } else if settings.authorizationStatus == .denied { VPRewardsAPI.requestUploadNoticeStatus(status: false) } } } } //MARK: -------------- UNUserNotificationCenterDelegate -------------- extension AppDelegate: UNUserNotificationCenterDelegate { func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { Messaging.messaging().apnsToken = deviceToken } func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: any Error) { vpLog(message: error) } ///APP处于前台是接收通知消息 func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { completionHandler([.badge, .banner]) } ///点击通知消息进入app func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { if #available(iOS 16.0, *) { UNUserNotificationCenter.current().setBadgeCount(0) } else { UIApplication.shared.applicationIconBadgeNumber = 0 } guard let userInfo: [String : Any] = response.notification.request.content.userInfo as? [String : Any] else { completionHandler() return } guard let model = VPOpenAppModel.deserialize(from: userInfo) else { completionHandler() return } VPStatAPI.requestStatApns(messageId: model.message_id ?? "", title: response.notification.request.content.title) if model.path == .videoDetail, let shortPlayId = model.short_play_id { let vc = VPDetailPlayerViewController() vc.shortPlayId = shortPlayId VPAppTool.topViewController?.navigationController?.pushViewController(vc, animated: true) } else if model.path == .promotion { let vc = VPRewardsViewController() VPAppTool.topViewController?.navigationController?.pushViewController(vc, animated: true) } else if model.path == .feedback { let vc = VPCampaignWebViewController() vc.urlStr = kVPFeedBackListWebUrl VPAppTool.topViewController?.navigationController?.pushViewController(vc, animated: true) } completionHandler() } } extension AppDelegate: MessagingDelegate { func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) { if let token = fcmToken { VPSettingAPI.requestUploadApnsDeviceToken(token: token) } // let dataDict: [String: String] = ["token": fcmToken ?? ""] // NotificationCenter.default.post( // name: Notification.Name("FCMToken"), // object: nil, // userInfo: dataDict // ) } }