Advertisement
undeadhip

Untitled

Oct 18th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.87 KB | None | 0 0
  1. class ONXPushManager: NSObject {
  2.     weak var delegate: ONXPushManagerDelegate?
  3.     internal var latestToken: String?
  4.     fileprivate var pendingPush : PushInfo?
  5.  
  6.     func start(_ app: UIApplication, launchOptions: [AnyHashable: Any]?, registerNow: Bool) {
  7.         if (registerNow) {
  8.             if #available(iOS 10.0, *) {
  9.                 self.registerPushes(app, completion: nil)
  10.             } else {
  11.                 self.registerPushes(app)
  12.             }
  13.         }
  14.        
  15.         if let remoteOptions = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? [String : AnyObject] {
  16.             self.handleDidRecieveNotification(remoteOptions, app: app, handler: nil)
  17.         }
  18.     }
  19.  
  20.     @available(iOS 10.0, *)
  21.     func registerPushes(_ app: UIApplication, completion: ((_ granted: Bool) -> ())?) {
  22.         let types: UIUserNotificationType = [.badge, .sound, .alert]
  23.         let mySettings = UIUserNotificationSettings(types: types, categories: nil)
  24.        
  25.         let center = UNUserNotificationCenter.current()
  26.         center.delegate = self
  27.         center.requestAuthorization(options: [.badge, .sound, .alert], completionHandler: { (granted, error) in
  28.             self.denied = !granted
  29.            
  30.             if let uError = error {
  31.                 self.delegate?.pushManager(manager: self, didGetNotificationsRegisterError: uError)
  32.             } else {
  33.                 app.registerUserNotificationSettings(mySettings)
  34.             }
  35.            
  36.             if let c = completion {
  37.                 c(granted)
  38.             }
  39.         })
  40.        
  41.         self.pushesPrompted = true
  42.     }
  43.  
  44.     func handleDidRecieveNotification(_ userInfo: [AnyHashable: Any], app: UIApplication, handler: ((UIBackgroundFetchResult) -> Void)?) {
  45.         print("PUSH \(userInfo)")
  46.        
  47.         switch app.applicationState {
  48.         case .active:
  49.             print("state - active")
  50.             self.pendingPush = nil
  51.            
  52.             let pushInfo = PushInfo(userInfo: userInfo, applicationState: app.applicationState)
  53.             self.actFromPush(pushInfo)
  54.         case .inactive:
  55.             print("state - inactive")
  56.             self.pendingPush = PushInfo(userInfo: userInfo, applicationState: app.applicationState)
  57.         case .background:
  58.             print("state - background")
  59.         }
  60.        
  61.         handler?(UIBackgroundFetchResult.noData)
  62.     }
  63.  
  64.     func handleApplicationDidBecomeActive(_ app: UIApplication) {
  65.         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
  66.             self.actFromPush(push)
  67.         }
  68.        
  69.         self.pendingPush = nil
  70.         self.delegate?.pushManagerDidHandleApplicationActivation?(self)
  71.     }
  72.  
  73.     func handleApplicationDidBecomeActive(_ app: UIApplication) {
  74.         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
  75.             self.actFromPush(push)
  76.         }
  77.        
  78.         self.pendingPush = nil
  79.         self.delegate?.pushManagerDidHandleApplicationActivation?(self)
  80.     }
  81. }
  82.  
  83. @available(iOS 10.0, *)
  84. extension ONXPushManager : UNUserNotificationCenterDelegate {
  85.     func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
  86.         print("userNotificationCenter willPresent \(notification)")
  87.         completionHandler([.alert, .sound])
  88.     }
  89.    
  90.     func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
  91.        
  92.         self.handleDidRecieveNotification(response.notification.request.content.userInfo, app: UIApplication.shared, handler: nil)
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement