Permalink
Browse files

Merge remote-tracking branch 'origin/master' into production

  • Loading branch information...
Michael Rademaker
Michael Rademaker committed Nov 8, 2017
2 parents a1beca3 + a58d37c commit ad2366ad345c3eb71c31792e663d6c07122d0988
Showing with 88 additions and 96 deletions.
  1. +2 −1 console/iOS/NotificationExtension/NotificationService.swift
  2. +86 −95 console/iOS/console/AppDelegate.swift
@@ -63,7 +63,9 @@ class NotificationService: UNNotificationServiceExtension, URLSessionDelegate {
//print(String(data: data!, encoding: .utf8) ?? "")
let json = try JSONSerialization.jsonObject(with: data!) as? [[String: Any]]
if (json?.count)! > 0 {
let categoryName = "openremoteNotification"
let detailedJson = (json?[0])! as [String: Any]
bestAttemptContent.categoryIdentifier = categoryName
bestAttemptContent.title = detailedJson["title"] as! String
bestAttemptContent.body = detailedJson["message"] as! String
bestAttemptContent.userInfo["appUrl"] = detailedJson["appUrl"]
@@ -82,7 +84,6 @@ class NotificationService: UNNotificationServiceExtension, URLSessionDelegate {
default : break
}
}
let categoryName = "openremoteNotification"
let category = UNNotificationCategory(identifier: categoryName, actions: notificationActions, intentIdentifiers: [], options: [])
let categories : Set = [category]
UNUserNotificationCenter.current().setNotificationCategories(categories)
@@ -24,26 +24,26 @@ import Firebase
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, URLSessionDelegate {
class AppDelegate: UIResponder, UIApplicationDelegate, URLSessionDelegate {
var window: UIWindow?
let gcmMessageIDKey = "gcm.message_id"
var reachabilityAlert : UIAlertController?
var reachabilityAlertShown = false
let internetReachability = Reachability.forInternetConnection()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
FirebaseApp.configure()
Messaging.messaging().delegate = self
application.registerForRemoteNotifications()
NotificationCenter.default.addObserver(self, selector: #selector(self.tokenRefreshNotification), name: NSNotification.Name.InstanceIDTokenRefresh, object: nil)
NotificationCenter.default.addObserver(self,
selector: #selector(self.reachabilityChanged(note:)),
name: NSNotification.Name.reachabilityChanged,
@@ -52,61 +52,114 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
self.updateReachabilityStatus(reachability: internetReachability!)
return true
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
reachabilityAlert?.dismiss(animated: true, completion: nil)
reachabilityAlertShown = false
internetReachability?.stopNotifier()
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
reachabilityAlert?.dismiss(animated: true, completion: nil)
reachabilityAlertShown = false
internetReachability?.stopNotifier()
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
self.internetReachability?.startNotifier()
self.updateReachabilityStatus(reachability: self.internetReachability!)
}
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
connectToFcm()
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
self.internetReachability?.startNotifier()
self.updateReachabilityStatus(reachability: self.internetReachability!)
}
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
reachabilityAlert?.dismiss(animated: true, completion: nil)
reachabilityAlertShown = false
internetReachability?.stopNotifier()
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
completionHandler(UIBackgroundFetchResult.noData)
completionHandler(UIBackgroundFetchResult.newData)
}
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
return true
}
@objc func reachabilityChanged(note: NSNotification) {
if let reachability = note.object as? Reachability {
updateReachabilityStatus(reachability: reachability)
}
}
private func updateReachabilityStatus(reachability: Reachability) {
if reachability.currentReachabilityStatus() == NetworkStatus.NotReachable {
if (!reachabilityAlertShown) {
let topWindow = UIWindow(frame: UIScreen.main.bounds)
topWindow.rootViewController = UIViewController()
topWindow.windowLevel = UIWindowLevelAlert + 1
reachabilityAlert = UIAlertController(title: "Network Error", message: "Your device seems to be offline", preferredStyle: .alert)
let reachabilityRetryAction = UIAlertAction(title: "Retry", style: .default, handler: { (action) in
self.reachabilityAlertShown = false
self.updateReachabilityStatus(reachability: self.internetReachability!)
topWindow.isHidden = true
})
let reachabilityCancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: { (action) in
self.reachabilityAlertShown = false
self.reachabilityAlert?.dismiss(animated: true, completion: nil)
topWindow.isHidden = true
})
reachabilityAlert?.addAction(reachabilityRetryAction)
reachabilityAlert?.addAction(reachabilityCancelAction)
topWindow.makeKeyAndVisible()
topWindow.rootViewController?.present(reachabilityAlert!, animated: true, completion: nil)
reachabilityAlertShown = true
}
} else {
if (reachabilityAlertShown) {
reachabilityAlert?.dismiss(animated: true, completion: nil)
reachabilityAlertShown = false
}
}
}
//MARK : - UNUserNotificationCenterDelegate
func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
if (challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) {
if challenge.protectionSpace.host == Server.hostURL {
completionHandler(.useCredential, URLCredential(trust: challenge.protectionSpace.serverTrust!))
} else {
completionHandler(.performDefaultHandling, nil)
}
}
}
}
extension AppDelegate : UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .sound])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
var assetId : String = ""
var attributeName : String = ""
@@ -121,13 +174,13 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
}
case ActionType.ACTION_ACTUATOR :
NSLog("Action asked : %@", response.actionIdentifier)
if let actions = response.notification.request.content.userInfo["actions"] {
assetId = (actions as! Dictionary<String,String>)["assetId"]!
attributeName = (actions as! Dictionary<String,String>)["attributeName"]!
rawJson = (actions as! Dictionary<String,String>)["rawJson"]!
}
(self.window?.rootViewController as! ViewController).updateAssetAttribute(assetId : assetId, attributeName : attributeName, rawJson : rawJson)
default : break
}
@@ -174,79 +227,17 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
completionHandler()
}
}
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
return true
}
func connectToFcm() {
Messaging.messaging().connect { (error) in
if (error != nil) {
if let token = InstanceID.instanceID().token() {
NSLog("Connected to FCM. Token is %@",token as String)
TokenManager.sharedInstance.storeDeviceId(token: token)
}
} else {
if let token = InstanceID.instanceID().token() {
NSLog("Connected to FCM. Token is %@",token as String)
TokenManager.sharedInstance.storeDeviceId(token: token)
}
}
}
}
@objc func tokenRefreshNotification(notification: NSNotification) {
// Connect to FCM since connection may have failed when attempted before having a token.
connectToFcm()
}
@objc func reachabilityChanged(note: NSNotification) {
if let reachability = note.object as? Reachability {
updateReachabilityStatus(reachability: reachability)
}
}
private func updateReachabilityStatus(reachability: Reachability) {
if reachability.currentReachabilityStatus() == NetworkStatus.NotReachable {
if (!reachabilityAlertShown) {
let topWindow = UIWindow(frame: UIScreen.main.bounds)
topWindow.rootViewController = UIViewController()
topWindow.windowLevel = UIWindowLevelAlert + 1
reachabilityAlert = UIAlertController(title: "Network Error", message: "Your device seems to be offline", preferredStyle: .alert)
let reachabilityRetryAction = UIAlertAction(title: "Retry", style: .default, handler: { (action) in
self.reachabilityAlertShown = false
self.updateReachabilityStatus(reachability: self.internetReachability!)
topWindow.isHidden = true
})
let reachabilityCancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: { (action) in
self.reachabilityAlertShown = false
self.reachabilityAlert?.dismiss(animated: true, completion: nil)
topWindow.isHidden = true
})
reachabilityAlert?.addAction(reachabilityRetryAction)
reachabilityAlert?.addAction(reachabilityCancelAction)
topWindow.makeKeyAndVisible()
topWindow.rootViewController?.present(reachabilityAlert!, animated: true, completion: nil)
reachabilityAlertShown = true
}
} else {
if (reachabilityAlertShown) {
reachabilityAlert?.dismiss(animated: true, completion: nil)
reachabilityAlertShown = false
}
}
}
extension AppDelegate : MessagingDelegate {
func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
print("Firebase registration token: \(fcmToken)")
TokenManager.sharedInstance.storeDeviceId(token: fcmToken)
}
func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
if (challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) {
if challenge.protectionSpace.host == Server.hostURL {
completionHandler(.useCredential, URLCredential(trust: challenge.protectionSpace.serverTrust!))
} else {
completionHandler(.performDefaultHandling, nil)
}
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print("Firebase registration token: \(fcmToken)")
TokenManager.sharedInstance.storeDeviceId(token: fcmToken)
}
}

0 comments on commit ad2366a

Please sign in to comment.