Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ONXPushManager: NSObject {
- weak var delegate: ONXPushManagerDelegate?
- internal var latestToken: String?
- fileprivate var pendingPush : PushInfo?
- func start(_ app: UIApplication, launchOptions: [AnyHashable: Any]?, registerNow: Bool) {
- if (registerNow) {
- if #available(iOS 10.0, *) {
- self.registerPushes(app, completion: nil)
- } else {
- self.registerPushes(app)
- }
- }
- if let remoteOptions = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? [String : AnyObject] {
- self.handleDidRecieveNotification(remoteOptions, app: app, handler: nil)
- }
- }
- @available(iOS 10.0, *)
- func registerPushes(_ app: UIApplication, completion: ((_ granted: Bool) -> ())?) {
- let types: UIUserNotificationType = [.badge, .sound, .alert]
- let mySettings = UIUserNotificationSettings(types: types, categories: nil)
- let center = UNUserNotificationCenter.current()
- center.delegate = self
- center.requestAuthorization(options: [.badge, .sound, .alert], completionHandler: { (granted, error) in
- self.denied = !granted
- if let uError = error {
- self.delegate?.pushManager(manager: self, didGetNotificationsRegisterError: uError)
- } else {
- app.registerUserNotificationSettings(mySettings)
- }
- if let c = completion {
- c(granted)
- }
- })
- self.pushesPrompted = true
- }
- func handleDidRecieveNotification(_ userInfo: [AnyHashable: Any], app: UIApplication, handler: ((UIBackgroundFetchResult) -> Void)?) {
- print("PUSH \(userInfo)")
- switch app.applicationState {
- case .active:
- print("state - active")
- self.pendingPush = nil
- let pushInfo = PushInfo(userInfo: userInfo, applicationState: app.applicationState)
- self.actFromPush(pushInfo)
- case .inactive:
- print("state - inactive")
- self.pendingPush = PushInfo(userInfo: userInfo, applicationState: app.applicationState)
- case .background:
- print("state - background")
- }
- handler?(UIBackgroundFetchResult.noData)
- }
- func handleApplicationDidBecomeActive(_ app: UIApplication) {
- if let push = self.pendingPush { //Means that push has been received before the app became active, and once it's active we need to do some action
- self.actFromPush(push)
- }
- self.pendingPush = nil
- self.delegate?.pushManagerDidHandleApplicationActivation?(self)
- }
- func handleApplicationDidBecomeActive(_ app: UIApplication) {
- if let push = self.pendingPush { //Means that push has been received before the app became active, and once it's active we need to do some action
- self.actFromPush(push)
- }
- self.pendingPush = nil
- self.delegate?.pushManagerDidHandleApplicationActivation?(self)
- }
- }
- @available(iOS 10.0, *)
- extension ONXPushManager : UNUserNotificationCenterDelegate {
- func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
- print("userNotificationCenter willPresent \(notification)")
- completionHandler([.alert, .sound])
- }
- func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
- self.handleDidRecieveNotification(response.notification.request.content.userInfo, app: UIApplication.shared, handler: nil)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement