100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
iOS console : management of notifications actions (open link)
William Balcaenb6d0cb61b1a3
diff --git a/console/iOS/NotificationExtension/Constants.swift b/console/iOS/NotificationExtension/Constants.swiftindex 839e4a2dd..649d3a9d6 100644--- a/console/iOS/NotificationExtension/Constants.swift+++ b/console/iOS/NotificationExtension/Constants.swift@@ -26,6 +26,6 @@ enum Server {}enum ActionType {- static let ACTION_TYPE1 = "ACTION_TYPE1"- static let ACTION_TYPE2 = "ACTION_TYPE2"+ static let ACTION_DEEP_LINK = "ACTION_DEEP_LINK"+ static let ACTION_ACTUATOR = "ACTION_ACTUATOR"}diff --git a/console/iOS/NotificationExtension/NotificationService.swift b/console/iOS/NotificationExtension/NotificationService.swiftindex da65f03b5..c3a9b6b45 100644--- a/console/iOS/NotificationExtension/NotificationService.swift+++ b/console/iOS/NotificationExtension/NotificationService.swift@@ -48,19 +48,22 @@ class NotificationService: UNNotificationServiceExtension, URLSessionDelegate {bestAttemptContent.body = error.debugDescription} else {do {+ print(String(data: data!, encoding: .utf8) ?? "")let json = try JSONSerialization.jsonObject(with: data!) as? [[String: Any]]+ if (json?.count)! > 0 {let detailedJson = (json?[0])! as [String: Any]bestAttemptContent.title = detailedJson["title"] as! StringbestAttemptContent.body = detailedJson["message"] as! String+ bestAttemptContent.userInfo["appUrl"] = detailedJson["appUrl"]let actions = detailedJson["actions"] as! [[String : Any]]var notificationActions = [UNNotificationAction]()for var i in (0..<actions.count) {let actionTitle = actions[i]["title"]! as! Stringlet actionType = actions[i]["type"]! as! Stringswitch actionType {- case ActionType.ACTION_TYPE1 :+ case ActionType.ACTION_ACTUATOR :notificationActions.append(UNNotificationAction(identifier: actionType, title: actionTitle, options: UNNotificationActionOptions.destructive))- case ActionType.ACTION_TYPE2 :+ case ActionType.ACTION_DEEP_LINK :notificationActions.append(UNNotificationAction(identifier: actionType, title: actionTitle, options: UNNotificationActionOptions.foreground))default : break@@ -71,6 +74,9 @@ class NotificationService: UNNotificationServiceExtension, URLSessionDelegate {let category = UNNotificationCategory(identifier: categoryName, actions: notificationActions, intentIdentifiers: [], options: [])let categories : Set = [category]UNUserNotificationCenter.current().setNotificationCategories(categories)+ } else {+ bestAttemptContent.body = "could not deserialize JSON"+ }} catch {bestAttemptContent.body = "could not deserialize JSON"}@@ -118,10 +124,10 @@ class NotificationService: UNNotificationServiceExtension, URLSessionDelegate {func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {if (challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) {- if challenge.protectionSpace.host == Server.hostURL || challenge.protectionSpace.host == "fonts.googleapis.com" {+ if challenge.protectionSpace.host == Server.hostURL {completionHandler(.useCredential, URLCredential(trust: challenge.protectionSpace.serverTrust!))} else {- print("Error : unsupported domain :",challenge.protectionSpace.serverTrust ?? "")+ completionHandler(.performDefaultHandling,nil)}}diff --git a/console/iOS/console/AppDelegate.swift b/console/iOS/console/AppDelegate.swiftindex ce8e56842..5d4cacb3a 100644--- a/console/iOS/console/AppDelegate.swift+++ b/console/iOS/console/AppDelegate.swift@@ -109,9 +109,12 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDfunc userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {switch response.actionIdentifier {- case ActionType.ACTION_TYPE1 :+ case ActionType.ACTION_DEEP_LINK :+ // open url+ let urlToOpen = response.notification.request.content.userInfo["appUrl"] as! String+ (self.window?.rootViewController as! ViewController).loadUrl(url: URL(string: urlToOpen)!)NSLog("Action asked : %@",response.actionIdentifier)- case ActionType.ACTION_TYPE2 :+ case ActionType.ACTION_ACTUATOR :NSLog("Action asked : %@",response.actionIdentifier)default : break}diff --git a/console/iOS/console/Constants.swift b/console/iOS/console/Constants.swiftindex a901d988b..a4cc2fd79 100644--- a/console/iOS/console/Constants.swift+++ b/console/iOS/console/Constants.swift@@ -13,8 +13,8 @@ enum Server {static let port = "8080"static let initialPath = String(format:"console/%@/index.html",Server.realm)static let realm = "blok61"- static let apiTestResource = String(format:"http://%@:%@/%@/notification/alert",Server.hostURL,Server.port,Server.realm)- static let registerDeviceResource = String(format:"http://%@:%@/%@/notification/token",Server.hostURL,Server.port,Server.realm)+ static let apiTestResource = String(format:"https://%@/%@/notification/alert",Server.hostURL,Server.realm)+ static let registerDeviceResource = String(format:"https://%@/%@/notification/token",Server.hostURL,Server.realm)}enum Client {@@ -47,6 +47,6 @@ enum Notifications {}enum ActionType {- static let ACTION_TYPE1 = "ACTION_TYPE1"- static let ACTION_TYPE2 = "ACTION_TYPE2"+ static let ACTION_DEEP_LINK = "ACTION_DEEP_LINK"+ static let ACTION_ACTUATOR = "ACTION_ACTUATOR"}diff --git a/console/iOS/console/ORViewController.swift b/console/iOS/console/ORViewController.swiftindex e762ae95a..2d73ab647 100644--- a/console/iOS/console/ORViewController.swift+++ b/console/iOS/console/ORViewController.swift@@ -73,7 +73,7 @@ class ORViewcontroller : UIViewController, URLSessionDelegate, WKScriptMessageHaself.present(alertVC, animated: true, completion: nil)}- // just for testing purpose (ask backend to send a notification to device)+ // just for testing purpose (ask backend to get list of notifications or send a notification to device)func apiCall() {guard let tkurlRequest = URL(string: String(format:"https://%@/auth/realms/%@/protocol/openid-connect/token",Server.hostURL,Server.realm)) else { return }let tkRequest = NSMutableURLRequest(url: tkurlRequest)@@ -90,9 +90,9 @@ class ORViewcontroller : UIViewController, URLSessionDelegate, WKScriptMessageHaif ((jsonDictionnary["access_token"]) != nil) {guard let urlRequest = URL(string: String(Server.apiTestResource)) else { return }let request = NSMutableURLRequest(url: urlRequest)- request.httpMethod = "GET"+ request.httpMethod = "GET" //change to post to create a notification alertrequest.addValue("application/json", forHTTPHeaderField: "Content-Type")- request.httpBody = "{\"title\" :\" oula\",\"message\" : \"sdlkfslkdfjslkdfj\",\"appUrl\" : \" kljlkjlkjlk\",\"actions\": [ {\"title\" : \"Action title\" , \"type\": \"ACTION_TYPE1\"},{\"title\" : \"Action title\" , \"type\": \"ACTION_TYPE2\"} ]}".data(using: .utf8)+ request.httpBody = "{\"title\" :\" notif alert title\",\"message\" : \"notif alert message\",\"appUrl\" : \"http://www.openremote.org\",\"actions\": [ {\"title\" : \"Open link\" , \"type\": \"ACTION_DEEP_LINK\"},{\"title\" : \"background call\" , \"type\": \"ACTION_ACTUATOR\"} ]}".data(using: .utf8)request.addValue(String(format:"Bearer %@", jsonDictionnary["access_token"] as! String), forHTTPHeaderField: "Authorization")let sessionConfiguration = URLSessionConfiguration.defaultlet session = URLSession(configuration: sessionConfiguration, delegate: self, delegateQueue : nil)@@ -161,10 +161,10 @@ class ORViewcontroller : UIViewController, URLSessionDelegate, WKScriptMessageHafunc webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {if (challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) {- if challenge.protectionSpace.host == Server.hostURL || challenge.protectionSpace.host == "fonts.googleapis.com" {+ if challenge.protectionSpace.host == Server.hostURL {completionHandler(.useCredential, URLCredential(trust: challenge.protectionSpace.serverTrust!))} else {- print("Error : unsupported domain :",challenge.protectionSpace.serverTrust ?? "")+ completionHandler(.performDefaultHandling, nil)}}@@ -172,10 +172,10 @@ class ORViewcontroller : UIViewController, URLSessionDelegate, WKScriptMessageHafunc urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {if (challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) {- if challenge.protectionSpace.host == Server.hostURL || challenge.protectionSpace.host == "fonts.googleapis.com" {+ if challenge.protectionSpace.host == Server.hostURL {completionHandler(.useCredential, URLCredential(trust: challenge.protectionSpace.serverTrust!))} else {- print("Error : unsupported domain :",challenge.protectionSpace.serverTrust ?? "")+ completionHandler(.performDefaultHandling, nil)}}@@ -209,4 +209,8 @@ class ORViewcontroller : UIViewController, URLSessionDelegate, WKScriptMessageHa//self.login()}+ func loadURL(url : URL) {+ _ = self.myWebView?.load(URLRequest(url:url))+ }+}diff --git a/console/iOS/console/TokenManager.swift b/console/iOS/console/TokenManager.swiftindex 44c938f46..34294322e 100644--- a/console/iOS/console/TokenManager.swift+++ b/console/iOS/console/TokenManager.swift@@ -153,10 +153,10 @@ class TokenManager:NSObject, WKScriptMessageHandler, WKUIDelegate, WKNavigationDfunc webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {if (challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) {- if challenge.protectionSpace.host == Server.hostURL || challenge.protectionSpace.host == "fonts.googleapis.com" {+ if challenge.protectionSpace.host == Server.hostURL {completionHandler(.useCredential, URLCredential(trust: challenge.protectionSpace.serverTrust!))} else {- print("Error : unsupported domain :",challenge.protectionSpace.serverTrust ?? "")+ completionHandler(.performDefaultHandling, nil)}}diff --git a/console/iOS/console/ViewController.swift b/console/iOS/console/ViewController.swiftindex ab99a6160..2bedc6680 100644--- a/console/iOS/console/ViewController.swift+++ b/console/iOS/console/ViewController.swift@@ -11,6 +11,7 @@ import AppAuthclass ViewController: UIViewController {var authState : OIDAuthState?+ let orViewController = ORViewcontroller()@IBOutlet weak var loginViewController: UIWebView!override func viewDidLoad() {super.viewDidLoad()@@ -31,6 +32,7 @@ class ViewController: UIViewController {if (TokenManager.sharedInstance.hasToken && !TokenManager.sharedInstance.didLogOut) {let orVC = ORViewcontroller()self.present(orVC, animated: true, completion: nil)+ self.present(orViewController, animated: true, completion: nil)} else {TokenManager.sharedInstance.authenticate()}