Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

iOS console : notifications

William Balcaen8bec0b8c55d4
diff --git a/console/iOS/NotificationExtension/Constants.swift b/console/iOS/NotificationExtension/Constants.swift
new file mode 100644
index 000000000..acf0e9e33
--- /dev/null
+++ b/console/iOS/NotificationExtension/Constants.swift
@@ -0,0 +1,19 @@
+//
+// Constants.swift
+// console
+//
+// Created by William Balcaen on 28/03/17.
+// Copyright © 2017 TInSys. All rights reserved.
+//
+
+import Foundation
+
+struct DefaultsKey {
+ static let offlineToken = "offlineToken"
+ static let refreshToken = "refreshToken"
+ static let idToken = "idToken"
+}
+
+struct AppGroup {
+ static let entitlement = "group.org.openremote.console"
+}
diff --git a/console/iOS/NotificationExtension/NotificationExtension.entitlements b/console/iOS/NotificationExtension/NotificationExtension.entitlements
new file mode 100644
index 000000000..e38707f06
--- /dev/null
+++ b/console/iOS/NotificationExtension/NotificationExtension.entitlements
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+ <key>com.apple.security.application-groups</key>
+ <array>
+ <string>group.org.openremote.console</string>
+ <string>group.org.openremote.console.NotificationExtension</string>
+ </array>
+</dict>
+</plist>
diff --git a/console/iOS/NotificationExtension/NotificationService.swift b/console/iOS/NotificationExtension/NotificationService.swift
index a423640f5..4d29a522f 100644
--- a/console/iOS/NotificationExtension/NotificationService.swift
+++ b/console/iOS/NotificationExtension/NotificationService.swift
@@ -19,13 +19,15 @@ class NotificationService: UNNotificationServiceExtension {
print("NotifExtension Change content ",bestAttemptContent?.userInfo ?? "")
if let bestAttemptContent = bestAttemptContent {
// Call backend to get payload and adapt title, body and actions
+ let defaults = UserDefaults(suiteName: AppGroup.entitlement)
+ defaults?.synchronize()
bestAttemptContent.title = "You received an alarm from blok61 :"
bestAttemptContent.body = "payload received from backend..."
guard let tkurlRequest = URL(string: "http://192.168.99.100:8080/auth/realms/blok61/protocol/openid-connect/token") else { return }
let tkRequest = NSMutableURLRequest(url: tkurlRequest)
tkRequest.addValue("application/x-www-form-urlencoded", forHTTPHeaderField:"Content-Type");
tkRequest.httpMethod = "POST"
- let postString = "grant_type=refresh_token&refresh_token=eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJDZXZKWnZHVmdKUWRyZTlGU1kzelF6NWZDZ080WW5iaHd1Ukg2WW9yVkZnIn0.eyJqdGkiOiIzOGI5YTQxOC1hNTZkLTQ1NDYtOTBkOS02OWY0NWEzZWM3ZGUiLCJleHAiOjAsIm5iZiI6MCwiaWF0IjoxNDg5NjcwNjYzLCJpc3MiOiJodHRwOi8vMTkyLjE2OC45OS4xMDA6ODA4MC9hdXRoL3JlYWxtcy9ibG9rNjEiLCJhdWQiOiJvcGVucmVtb3RlIiwic3ViIjoiZTk2M2Y3ZWYtNWUyMy00Njk1LWE5M2QtOTU3Mjk0ODYwM2MxIiwidHlwIjoiT2ZmbGluZSIsImF6cCI6Im9wZW5yZW1vdGUiLCJub25jZSI6IjQ2Nzg3MThlLTdhMGUtNDliNy1hNDQyLTA1ODk2YmFkY2I0YiIsImF1dGhfdGltZSI6MCwic2Vzc2lvbl9zdGF0ZSI6ImU5YmJiODlkLTk2ZDUtNDM0Ny1hMzVhLTFmZjM0OTRmNjRlNCIsImNsaWVudF9zZXNzaW9uIjoiMTliODY0NTAtYWEzMS00OGNlLWE4ODEtMzliNjM0NDkzOThjIiwicmVhbG1fYWNjZXNzIjp7InJvbGVzIjpbIm9mZmxpbmVfYWNjZXNzIiwidW1hX2F1dGhvcml6YXRpb24iXX0sInJlc291cmNlX2FjY2VzcyI6eyJvcGVucmVtb3RlIjp7InJvbGVzIjpbIndyaXRlOmFzc2V0cyIsInJlYWQ6bWFwIiwicmVhZDphc3NldHMiLCJ3cml0ZTp1c2VyIl19LCJhY2NvdW50Ijp7InJvbGVzIjpbIm1hbmFnZS1hY2NvdW50Iiwidmlldy1wcm9maWxlIl19fX0.APnuPAlcyEnGYlOIX8W4znbJHhc-HyNc8tmKrsqVkkudf0Y3Dspgjjc3z907hBW0K_Lh0CXtWMMulZk-AcbTohNq3pnnohNsGEfXKuqWeIRjJwiArItwo5u5XgTveLWn7fPTMl8IplGqVptUCdBex1QKmMa_PfFLRLb9Xjh4XI5MKGp1qy_vL8NH1xhKud1quyTLivxGrZpsC8Dyln2VUGbD5nFCYlzVv4zqTk0K3laaW52v9y5AAp2iYLgjoxrW-WBDExfTWdz9TQ3t5q35_GnyyzlE-uBYvppKMxZ4Hb0YkAslEvKYgUnplRx9UD9EBw8auG1fRQ4e1C4_4PqoWw&client_id=openremote"
+ let postString = String(format: "grant_type=refresh_token&refresh_token=%@&client_id=openremote", defaults!.object(forKey: DefaultsKey.refreshToken) as! CVarArg)
tkRequest.httpBody = postString.data(using: .utf8)
let sessionConfiguration = URLSessionConfiguration.default
let session = URLSession(configuration: sessionConfiguration)
@@ -81,6 +83,8 @@ class NotificationService: UNNotificationServiceExtension {
// Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
print("NotifExtension Time has expired")
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
+ bestAttemptContent.title = "You received an alarm from blok61 :"
+ bestAttemptContent.body = "Please open application to check what's happening"
contentHandler(bestAttemptContent)
}
}
diff --git a/console/iOS/console.xcodeproj/project.pbxproj b/console/iOS/console.xcodeproj/project.pbxproj
index 011ac49b0..0c7056584 100644
--- a/console/iOS/console.xcodeproj/project.pbxproj
+++ b/console/iOS/console.xcodeproj/project.pbxproj
@@ -15,6 +15,7 @@
FA1D73EC1E53374E00F6240A /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = FA1D73EA1E53374E00F6240A /* LaunchScreen.storyboard */; };
FA1D73F61E53392F00F6240A /* ORViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA1D73F51E53392F00F6240A /* ORViewController.swift */; };
FA2D48311E560B7500C95665 /* Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA2D48301E560B7500C95665 /* Constants.swift */; };
+ FA3297AB1E8A4FC6008B77D3 /* Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA3297AA1E8A4FC6008B77D3 /* Constants.swift */; };
FAA226B81E71563F009A8FEC /* TokenManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = FAA226B71E71563F009A8FEC /* TokenManager.swift */; };
FAA226BA1E715ED6009A8FEC /* TokenManagerView.xib in Resources */ = {isa = PBXBuildFile; fileRef = FAA226B91E715ED6009A8FEC /* TokenManagerView.xib */; };
FAA226BC1E7163D9009A8FEC /* TokenManagerViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = FAA226BB1E7163D9009A8FEC /* TokenManagerViewController.swift */; };
@@ -59,6 +60,8 @@
FA1D73ED1E53374E00F6240A /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
FA1D73F51E53392F00F6240A /* ORViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ORViewController.swift; sourceTree = "<group>"; };
FA2D48301E560B7500C95665 /* Constants.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Constants.swift; sourceTree = "<group>"; };
+ FA3297A91E8A4AE5008B77D3 /* NotificationExtension.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = NotificationExtension.entitlements; sourceTree = "<group>"; };
+ FA3297AA1E8A4FC6008B77D3 /* Constants.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Constants.swift; sourceTree = "<group>"; };
FAA226B71E71563F009A8FEC /* TokenManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TokenManager.swift; sourceTree = "<group>"; };
FAA226B91E715ED6009A8FEC /* TokenManagerView.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = TokenManagerView.xib; sourceTree = "<group>"; };
FAA226BB1E7163D9009A8FEC /* TokenManagerViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TokenManagerViewController.swift; sourceTree = "<group>"; };
@@ -158,8 +161,10 @@
FAFF2E2B1E7A95120076BDE3 /* NotificationExtension */ = {
isa = PBXGroup;
children = (
+ FA3297A91E8A4AE5008B77D3 /* NotificationExtension.entitlements */,
FAFF2E2C1E7A95120076BDE3 /* NotificationService.swift */,
FAFF2E2E1E7A95120076BDE3 /* Info.plist */,
+ FA3297AA1E8A4FC6008B77D3 /* Constants.swift */,
);
path = NotificationExtension;
sourceTree = "<group>";
@@ -221,6 +226,9 @@
LastSwiftMigration = 0820;
ProvisioningStyle = Automatic;
SystemCapabilities = {
+ com.apple.ApplicationGroups.iOS = {
+ enabled = 1;
+ };
com.apple.BackgroundModes = {
enabled = 1;
};
@@ -232,6 +240,11 @@
FAFF2E291E7A95120076BDE3 = {
CreatedOnToolsVersion = 8.2.1;
ProvisioningStyle = Automatic;
+ SystemCapabilities = {
+ com.apple.ApplicationGroups.iOS = {
+ enabled = 1;
+ };
+ };
};
};
};
@@ -342,6 +355,7 @@
buildActionMask = 2147483647;
files = (
FAFF2E2D1E7A95120076BDE3 /* NotificationService.swift in Sources */,
+ FA3297AB1E8A4FC6008B77D3 /* Constants.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -505,6 +519,7 @@
FAFF2E331E7A95120076BDE3 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
+ CODE_SIGN_ENTITLEMENTS = NotificationExtension/NotificationExtension.entitlements;
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = NotificationExtension/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @executable_path/../../Frameworks";
@@ -518,6 +533,7 @@
FAFF2E341E7A95120076BDE3 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
+ CODE_SIGN_ENTITLEMENTS = NotificationExtension/NotificationExtension.entitlements;
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = NotificationExtension/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @executable_path/../../Frameworks";
@@ -556,6 +572,7 @@
FAFF2E341E7A95120076BDE3 /* Release */,
);
defaultConfigurationIsVisible = 0;
+ defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
diff --git a/console/iOS/console/Constants.swift b/console/iOS/console/Constants.swift
index 2cc4da39f..a518ffaa7 100644
--- a/console/iOS/console/Constants.swift
+++ b/console/iOS/console/Constants.swift
@@ -29,3 +29,7 @@ struct DefaultsKey {
struct NotificationsNames {
static let isAuthenticated = "isAuthenticated"
}
+
+struct AppGroup {
+ static let entitlement = "group.org.openremote.console"
+}
diff --git a/console/iOS/console/ORViewController.swift b/console/iOS/console/ORViewController.swift
index b6d5a008d..fe45f75ba 100644
--- a/console/iOS/console/ORViewController.swift
+++ b/console/iOS/console/ORViewController.swift
@@ -49,8 +49,9 @@ class ORViewcontroller : UIViewController, URLSessionDelegate, WKScriptMessageHa
}
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
- let defaults = UserDefaults.standard
- defaults.set(message.body, forKey: message.name)
+ let defaults = UserDefaults(suiteName: AppGroup.entitlement)
+ defaults?.set(message.body, forKey: message.name)
+ defaults?.synchronize()
}
diff --git a/console/iOS/console/TokenManager.swift b/console/iOS/console/TokenManager.swift
index a1f5b5a6a..d2df033a8 100644
--- a/console/iOS/console/TokenManager.swift
+++ b/console/iOS/console/TokenManager.swift
@@ -25,11 +25,13 @@ class TokenManager:NSObject, WKScriptMessageHandler, WKUIDelegate, WKNavigationD
viewController = TokenManagerViewController()
let webCfg:WKWebViewConfiguration = WKWebViewConfiguration()
myWebView = WKWebView(frame: viewController.view.frame, configuration: webCfg)
- if let token = UserDefaults.standard.value(forKey: DefaultsKey.offlineToken) {
+ let defaults = UserDefaults(suiteName: AppGroup.entitlement)
+ defaults?.synchronize()
+ if let token = defaults?.value(forKey: DefaultsKey.offlineToken) {
hasToken = true
offlineToken = token as? String
- refreshToken = UserDefaults.standard.value(forKey: DefaultsKey.refreshToken) as? String
- idToken = UserDefaults.standard.value(forKey: DefaultsKey.idToken) as? String
+ refreshToken = defaults?.value(forKey: DefaultsKey.refreshToken) as? String
+ idToken = defaults?.value(forKey: DefaultsKey.idToken) as? String
} else {
offlineToken = nil
refreshToken = nil
@@ -41,7 +43,7 @@ class TokenManager:NSObject, WKScriptMessageHandler, WKUIDelegate, WKNavigationD
func authenticate() {
print("authenticate")
- let defaults = UserDefaults.standard
+ let defaults = UserDefaults(suiteName: AppGroup.entitlement)
let webCfg:WKWebViewConfiguration = WKWebViewConfiguration()
let userController:WKUserContentController = WKUserContentController()
@@ -51,10 +53,10 @@ class TokenManager:NSObject, WKScriptMessageHandler, WKUIDelegate, WKNavigationD
userController.add(self, name: DefaultsKey.idToken)
var exec_template = "var iOSToken"
- if let offlineToken = defaults.object(forKey: DefaultsKey.offlineToken) {
+ if let offlineToken = defaults?.object(forKey: DefaultsKey.offlineToken) {
print("offlinetoken exists")
- let refreshToken = defaults.object(forKey: DefaultsKey.refreshToken)
- let idToken = defaults.object(forKey: DefaultsKey.idToken)
+ let refreshToken = defaults?.object(forKey: DefaultsKey.refreshToken)
+ let idToken = defaults?.object(forKey: DefaultsKey.idToken)
exec_template = String(format: "var iOSToken = \"%@\"; var iOSRefreshToken = \"%@\"; var iOSTokenId = \"%@\";", offlineToken as! String, refreshToken as! String, idToken as! String)
}
@@ -77,16 +79,16 @@ class TokenManager:NSObject, WKScriptMessageHandler, WKUIDelegate, WKNavigationD
offlineToken = nil
refreshToken = nil
idToken = nil
- let defaults = UserDefaults.standard
- defaults.removeObject(forKey: DefaultsKey.offlineToken)
- defaults.removeObject(forKey: DefaultsKey.refreshToken)
- defaults.removeObject(forKey: DefaultsKey.idToken)
+ let defaults = UserDefaults(suiteName: AppGroup.entitlement)
+ defaults?.removeObject(forKey: DefaultsKey.offlineToken)
+ defaults?.removeObject(forKey: DefaultsKey.refreshToken)
+ defaults?.removeObject(forKey: DefaultsKey.idToken)
authenticate()
}
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
- let defaults = UserDefaults.standard
- defaults.set(message.body, forKey: message.name)
+ let defaults = UserDefaults(suiteName: AppGroup.entitlement)
+ defaults?.set(message.body, forKey: message.name)
switch message.name {
case DefaultsKey.offlineToken:
offlineToken = message.body as? String
diff --git a/console/iOS/console/console.entitlements b/console/iOS/console/console.entitlements
index 903def2af..c2389227d 100644
--- a/console/iOS/console/console.entitlements
+++ b/console/iOS/console/console.entitlements
@@ -4,5 +4,10 @@
<dict>
<key>aps-environment</key>
<string>development</string>
+ <key>com.apple.security.application-groups</key>
+ <array>
+ <string>group.org.openremote.console</string>
+ <string>group.org.openremote.console.NotificationExtension</string>
+ </array>
</dict>
</plist>