ThimraTV/MoviaBox/AppDelegate/AppDelegate+APNS.swift
2025-06-19 10:49:17 +08:00

134 lines
4.6 KiB
Swift

//
// AppDelegate+APNS.swift
// MoviaBox
//
// Created by on 2025/4/30.
//
import UIKit
import FirebaseMessaging
import FirebaseCore
extension AppDelegate {
///
static var haveBeenShownAPNS = false
func registerAPNS() {
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: kSPApnsAlertDefaultsKey) as? Date {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
#if DEBUG
self.showApnsAlert()
#else
if !date.sp_isToday {
self.showApnsAlert()
}
#endif
}
} else {
Self.haveBeenShownAPNS = true
SPAPPTool.sceneDelegate?.retryHandleOpenAppMessage()
}
UserDefaults.standard.set(Date(), forKey: kSPApnsAlertDefaultsKey)
} else {
Self.haveBeenShownAPNS = true
SPAPPTool.sceneDelegate?.retryHandleOpenAppMessage()
}
self.uploadNoticeStatus()
}
UIApplication.shared.registerForRemoteNotifications()
}
private func showApnsAlert() {
let view = SPApnsAlertView()
view.show()
}
///
func uploadNoticeStatus() {
UNUserNotificationCenter.current().getNotificationSettings { settings in
if settings.authorizationStatus == .authorized {
SPRewardsAPI.requestUploadNoticeStatus(status: true)
} else if settings.authorizationStatus == .denied {
SPRewardsAPI.requestUploadNoticeStatus(status: false)
}
}
}
}
extension AppDelegate: UNUserNotificationCenterDelegate {
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken
}
///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 = SPOpenAppModel.deserialize(from: userInfo) else {
completionHandler()
return
}
SPStatAPI.requestStatApns(messageId: model.message_id ?? "", title: response.notification.request.content.title)
if model.path == .videoDetail, let shortPlayId = model.short_play_id {
let vc = SPPlayerDetailViewController()
vc.shortPlayId = shortPlayId
SPAPPTool.topViewController()?.navigationController?.pushViewController(vc, animated: true)
} else if model.path == .promotion {
SPAPPTool.mainTabBarController?.selectedReward()
} else if model.path == .feedback {
let vc = SPCampaignWebViewController()
vc.urlStr = SPFeedBackListWebUrl
SPAPPTool.topViewController()?.navigationController?.pushViewController(vc, animated: true)
}
completionHandler()
}
}
//MARK: -------------- MessagingDelegate --------------
extension AppDelegate: MessagingDelegate {
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
if let token = fcmToken {
SPApnsAPI.requestUploadDeviceToken(token: token)
}
let dataDict: [String: String] = ["token": fcmToken ?? ""]
NotificationCenter.default.post(
name: Notification.Name("FCMToken"),
object: nil,
userInfo: dataDict
)
}
}