63 lines
2.0 KiB
Swift
63 lines
2.0 KiB
Swift
//
|
|
// AppDelegate+APNS.swift
|
|
// MoviaBox
|
|
//
|
|
// Created by 佳尔 on 2025/4/30.
|
|
//
|
|
|
|
import UIKit
|
|
import FirebaseMessaging
|
|
import FirebaseCore
|
|
|
|
extension AppDelegate {
|
|
|
|
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 {
|
|
// SPRewardsAPI.requestUploadOpenNotify(completer: nil)
|
|
}
|
|
}
|
|
UIApplication.shared.registerForRemoteNotifications()
|
|
}
|
|
}
|
|
|
|
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, .alert])
|
|
}
|
|
|
|
///点击通知消息进入app
|
|
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
|
|
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
|
|
)
|
|
}
|
|
}
|