Объявить AppDelegate делегата push-уведомлений из другого класса в Swift? - PullRequest
0 голосов
/ 25 июня 2019

Я использую синглтон класса Utilities для представления всплывающего окна и запроса Push Notifications доступа, но я сделал класс AppDelegate delegate для FirebaseMessaging и UserNotifications, поэтому didReceiveRegistrationToken функция никогда не вызывается в тот момент, когда пользователь предоставил доступ в процессе регистрации, и, следовательно, не сохраняет токен в это время.

Мне нужно сделать AppDelegate delegate для уведомленийЕсли новые регистрационные токены получены и, следовательно, сохранены.

Как я могу сделать AppDelegate UNUserNotificationCenter.current().delegate и Messaging.messaging().delegate из моего класса Utilities?

Объявление синглтона AppDelegate недопустимо.

Класс утилит:

Class Utilities {

static let run = Utilities()


    func showNotificationServicesIsDisabledPopup(viewController: UIViewController, handler: @escaping (Bool) -> ()){

        //present popup to allow for push notifications...

    }


    func registerForRemoteNotification() {

        if #available(iOS 10.0, *) {

            UNUserNotificationCenter.current().requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in

                if error == nil{
                    DispatchQueue.main.async { UIApplication.shared.registerForRemoteNotifications() } //
                    //UIApplication.shared.registerForRemoteNotifications()

                }//end if

            }//end requestAuthorization

        } else {

            UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.sound, .alert, .badge], categories: nil))
            UIApplication.shared.registerForRemoteNotifications()

            //Declaring AppDelegate as the delegate
            let appDelegate = UIApplication.shared.delegate as! AppDelegate

            UNUserNotificationCenter.current().delegate = appDelegate
            Messaging.messaging().delegate = appDelegate

        }//end else


    }

}

Класс AppDelegate:

import UserNotifications
import FirebaseMessaging





class AppDelegate: UIResponder, MessagingDelegate, UIApplicationDelegate {

        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        UNUserNotificationCenter.current().delegate = self
                Messaging.messaging().delegate = self
        }

        func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {

                let dataDict:[String: Bool] = [fcmToken: true]

                NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)

                DataService.run.saveFCMToken(tokenDict: dataDict, fcmtoken: fcmToken) { (success) in

                    if success {
                        print("FCM Token Saved: \(fcmToken)")
                        DEVICEID_FCMTOKEN = fcmToken
                        self.connectToFCM()
                    }//end if

                }//end saveFCMToken

            }//end func

        }

    }
...