Skip to content

armgit / core

Public Imported from GitHub
0

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

Added silent notifications and moeved providers in preparation for updating the cocoapod

Michael Rademakera40303c9becd
diff --git a/console/iOS/ORLib/ORLib.xcodeproj/project.xcworkspace/xcuserdata/michael.xcuserdatad/UserInterfaceState.xcuserstate b/console/iOS/ORLib/ORLib.xcodeproj/project.xcworkspace/xcuserdata/michael.xcuserdatad/UserInterfaceState.xcuserstate
index 4c2ad43b2..376273854 100644
Binary files a/console/iOS/ORLib/ORLib.xcodeproj/project.xcworkspace/xcuserdata/michael.xcuserdatad/UserInterfaceState.xcuserstate and b/console/iOS/ORLib/ORLib.xcodeproj/project.xcworkspace/xcuserdata/michael.xcuserdatad/UserInterfaceState.xcuserstate differ
diff --git a/console/iOS/ORLib/ORLib/ORAppDelegate.swift b/console/iOS/ORLib/ORLib/ORAppDelegate.swift
index 7259170e0..8def41bf4 100644
--- a/console/iOS/ORLib/ORLib/ORAppDelegate.swift
+++ b/console/iOS/ORLib/ORLib/ORAppDelegate.swift
@@ -109,33 +109,12 @@ open class ORAppDelegate: UIResponder, UIApplicationDelegate, URLSessionDelegate
open func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
- if let actionsString = userInfo["actions"] as? String {
- if let actionsData = actionsString.data(using: .utf8){
- if let actions = try? JSONSerialization.jsonObject(with: actionsData, options: []) as? [[String: String]], actions != nil {
- if let aps = userInfo["aps"] as? [String: Any] {
- if let alert = aps["alert"] as? [String: String] {
- let title = alert["title"]
- let message = alert["body"]
-
- let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
-
- for i in (0..<actions!.count) {
- let actionTitle = actions![i]["title"]
- let actionType = actions![i]["type"]
-
- switch actionType {
- case ActionType.ACTION_DEEP_LINK :
- alertController.addAction(UIAlertAction(title: actionTitle, style: .default, handler: { alertAction in
- if let urlToOpen = actions![i]["appUrl"] {
- guard let urlRequest = URL(string: urlToOpen) else { return }
- (self.window?.rootViewController as! ORViewcontroller).loadURL(url: urlRequest)
- }
- }))
-
- default : break
- }
- }
- self.window?.rootViewController?.present(alertController, animated: true, completion: nil)
+ if let actionsString = userInfo[DefaultsKey.dataKey] as? String {
+ if let actionData = actionsString.data(using: .utf8){
+ if let actionDict = try? JSONSerialization.jsonObject(with: actionData, options: []) as? [[String: String]], actions != nil {
+ if let action = actionDict[DefaultsKey.actionKey] as? String {
+ if action == Actions.geofenceRefresh {
+ (self.window?.rootViewController as! ORViewcontroller).geofenceProvider?.refreshGeofences()
}
}
}
diff --git a/console/iOS/ORLib/ORLib/ORViewController.swift b/console/iOS/ORLib/ORLib/ORViewController.swift
index 1fb4f3eed..bdad4f231 100644
--- a/console/iOS/ORLib/ORLib/ORViewController.swift
+++ b/console/iOS/ORLib/ORLib/ORViewController.swift
@@ -28,6 +28,9 @@ open class ORViewcontroller : UIViewController, URLSessionDelegate, WKScriptMess
public var myWebView : WKWebView?
public var defaults : UserDefaults?
public var webCfg : WKWebViewConfiguration?
+
+ var geofenceProvider: GeofenceProvider?
+ var pushProvider: PushNotificationProvider?
open override func viewDidLoad() {
super.viewDidLoad()
@@ -39,18 +42,78 @@ open class ORViewcontroller : UIViewController, URLSessionDelegate, WKScriptMess
self.configureAccess()
}
- open func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
+ open override func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
let jsonDictionnary = message.body as? [String : Any]
- let type = jsonDictionnary?["type"] as! String
- switch (type) {
- case "token":
- let tokenJsonDictionnary = jsonDictionnary?["data"] as? [String : String]
- TokenManager.sharedInstance.storeTokens(tokenJsonDictionnary: tokenJsonDictionnary)
-
- default:
- print("Unknown message type: \(type )")
+ if let type = jsonDictionnary?["type"] as? String {
+ switch (type) {
+ case "token":
+ let tokenJsonDictionnary = jsonDictionnary?[DefaultsKey.dataKey] as? [String : String]
+ TokenManager.sharedInstance.storeTokens(tokenJsonDictionnary: tokenJsonDictionnary)
+ case "provider":
+ if let postMessageDict = jsonDictionnary?[DefaultsKey.dataKey] as? [String: Any] {
+ if let action = postMessageDict[DefaultsKey.actionKey] as? String {
+ if let provider = postMessageDict[DefaultsKey.providerKey] as? String {
+ if provider == Providers.push {
+ if action == Actions.providerInit {
+ pushProvider = PushNotificationProvider()
+ let initalizeData = pushProvider!.initialize()
+ sendData(data: initalizeData)
+ } else if action == Actions.providerEnable {
+ if let enableData = pushProvider?.enable() {
+ sendData(data: enableData)
+ }
+ } else if action == Actions.providerDisable {
+ if let disableData = pushProvider?.disbale() {
+ sendData(data: disableData)
+ }
+ }
+ }
+ if provider == Providers.geofence {
+ if action == Actions.providerInit {
+ geofenceProvider = GeofenceProvider()
+ geofenceProvider!.initialize { initalizeData in
+ self.sendData(data: initalizeData)
+ }
+ } else if action == Actions.providerEnable {
+ if let data = postMessageDict[DefaultsKey.dataKey] as? [String: String] {
+ if let consoleId = data["consoleId"] {
+ if let enableData = geofenceProvider?.enable(baseUrl: "\(ORServer.baseUrl)\(ORServer.realm)", consoleId: consoleId) {
+ sendData(data: enableData)
+
+ DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(5)) {
+ self.geofenceProvider?.fetchGeofences()
+ }
+ }
+ }
+ }
+ } else if action == Actions.geofenceRefresh {
+ geofenceProvider?.refreshGeofences()
+ } else if action == Actions.providerDisable {
+ if let disableData = geofenceProvider?.disbale() {
+ sendData(data: disableData)
+ }
+ }
+ }
+ }
+ }
+ }
+ default:
+ print("Unknown message type: \(type )")
+ }
+ }
+ }
+
+ open func sendData(data: [String: Any]) {
+ if let theJSONData = try? JSONSerialization.data(
+ withJSONObject: data,
+ options: []) {
+ let theJSONText = String(data: theJSONData,
+ encoding: .utf8)
+ let returnMessage = "openremote.INSTANCE.console.handleProviderResponse('\(theJSONText ?? "null")')"
+ myWebView?.evaluateJavaScript("\(returnMessage)", completionHandler: { (any, error) in
+ print("JSON string = \(theJSONText!)")
+ })
}
-
}
open func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) {
diff --git a/manager/src/main/java/org/openremote/manager/notification/FCMDeliveryService.java b/manager/src/main/java/org/openremote/manager/notification/FCMDeliveryService.java
index 064521df0..592b922e8 100644
--- a/manager/src/main/java/org/openremote/manager/notification/FCMDeliveryService.java
+++ b/manager/src/main/java/org/openremote/manager/notification/FCMDeliveryService.java
@@ -172,6 +172,9 @@ public class FCMDeliveryService {
if (data != null) {
builder.putAllData(data);
+ if (notification == null) {
+ apsBuilder.setContentAvailable(true);
+ }
}
androidConfigBuilder.setPriority(priority == FCMMessagePriority.HIGH ? AndroidConfig.Priority.HIGH : AndroidConfig.Priority.NORMAL);
diff --git a/manager/src/main/java/org/openremote/manager/notification/NotificationService.java b/manager/src/main/java/org/openremote/manager/notification/NotificationService.java
index 6dee1adaa..86d342b80 100644
--- a/manager/src/main/java/org/openremote/manager/notification/NotificationService.java
+++ b/manager/src/main/java/org/openremote/manager/notification/NotificationService.java
@@ -19,9 +19,6 @@
*/
package org.openremote.manager.notification;
-import com.google.firebase.messaging.AndroidConfig;
-import com.google.firebase.messaging.ApnsConfig;
-import com.google.firebase.messaging.Notification;
import org.openremote.container.Container;
import org.openremote.container.ContainerService;
import org.openremote.container.persistence.PersistenceService;
@@ -34,14 +31,11 @@ import org.openremote.model.asset.BaseAssetQuery;
import org.openremote.model.attribute.AttributeType;
import org.openremote.model.console.ConsoleConfiguration;
import org.openremote.model.console.ConsoleProvider;
-import org.openremote.model.notification.AlertAction;
-import org.openremote.model.notification.DeviceNotificationToken;
import org.openremote.model.notification.AlertNotification;
import org.openremote.model.notification.DeliveryStatus;
+import org.openremote.model.notification.DeviceNotificationToken;
import org.openremote.model.user.UserQuery;
import org.openremote.model.util.TextUtil;
-import org.openremote.model.value.ObjectValue;
-import org.openremote.model.value.Values;
import javax.persistence.EntityManager;
import javax.persistence.Query;
@@ -120,12 +114,20 @@ public class NotificationService implements ContainerService {
//TODO: Unify the notification service to support push, email, SMS, etc.
public void notifyConsole(String consoleId, AlertNotification alertNotification) {
+ notifyConsole(consoleId, alertNotification, null);
+ }
+
+ public void notifyConsoleSilently(String consoleId, Map<String, String> data) {
+ notifyConsole(consoleId, null, data);
+ }
+
+ public void notifyConsole(String consoleId, AlertNotification alertNotification, Map<String, String> data) {
AssetQuery consoleQuery = new AssetQuery()
.select(new BaseAssetQuery.Select(BaseAssetQuery.Include.ALL_EXCEPT_PATH,
- false,
- AttributeType.CONSOLE_PROVIDERS.getName()))
+ false,
+ AttributeType.CONSOLE_PROVIDERS.getName()))
.attributeValue(AttributeType.CONSOLE_PROVIDERS.getName(),
- new BaseAssetQuery.ObjectValueKeyPredicate("push"))
+ new BaseAssetQuery.ObjectValueKeyPredicate("push"))
.id(consoleId);
@@ -142,29 +144,23 @@ public class NotificationService implements ContainerService {
if ("fcm".equals(pushProviderVersion)) {
if (consolePushProvider.getData() == null || !consolePushProvider.getData().getString("token")
- .map(token -> TextUtil.isNullOrEmpty(token) ? null : token).isPresent()) {
+ .map(token -> TextUtil.isNullOrEmpty(token) ? null : token).isPresent()) {
LOG.warning("Console 'fcm' push provider doesn't contain an FCM token");
return;
}
String token = consolePushProvider.getData().getString("token").orElse(null);
- FCMNotification notification = new FCMNotification(alertNotification.getTitle(), alertNotification.getMessage());
- Map<String, String> data = new HashMap<>();
-
- if (alertNotification.getActions() != null && !alertNotification.getActions().isEmpty()) {
- data.put("actions", alertNotification.getActions().toJson());
-// alertNotification.getActions().stream()
-// .map(value -> Values.getObject(value).orElse(null))
-// .filter(objectValue -> !Objects.isNull(objectValue))
-// .forEach(actionObj -> {
-// String type = actionObj.getString("type").orElse(null);
-// String title = actionObj.getString("title").orElse(null);
-// String appUrl = actionObj.getString("appUrl").orElse(null);
-// String assetId = actionObj.getString("assetId").orElse(null);
-// String rawJson = actionObj.getString("rawJson").orElse(null);
-// String attributeName = actionObj.getString("attributeName").orElse(null);
-// });
+ FCMNotification notification = null;
+ if (alertNotification != null) {
+ notification = new FCMNotification(alertNotification.getTitle(), alertNotification.getMessage());
+ if (data == null) {
+ data = new HashMap<>();
+ }
+
+ if (alertNotification.getActions() != null && !alertNotification.getActions().isEmpty()) {
+ data.put("actions", alertNotification.getActions().toJson());
+ }
}
// Push alert directly inside the FCM message
@@ -175,7 +171,7 @@ public class NotificationService implements ContainerService {
data,
FCMMessagePriority.HIGH,
0 // 0 TTL gives best performance and this is assuming notifications are time critical
- );
+ );
} else {
LOG.warning("Unsupported push provider version: " + pushProviderVersion);
}
diff --git a/manager/src/main/java/org/openremote/manager/rules/geofence/ORConsoleGeofenceAssetAdapter.java b/manager/src/main/java/org/openremote/manager/rules/geofence/ORConsoleGeofenceAssetAdapter.java
index 05f61d874..08371a45d 100644
--- a/manager/src/main/java/org/openremote/manager/rules/geofence/ORConsoleGeofenceAssetAdapter.java
+++ b/manager/src/main/java/org/openremote/manager/rules/geofence/ORConsoleGeofenceAssetAdapter.java
@@ -197,8 +197,9 @@ public class ORConsoleGeofenceAssetAdapter extends RouteBuilder implements Geofe
}
protected void notifyAssetGeofencesChanged(String assetId) {
- // TODO: implement geofence push notification
-// notificationService.findDeviceToken(assetId);
+ Map<String, String> data = new HashMap<>();
+ data.put("action", "GEOFENCE_REFRESH");
+ notificationService.notifyConsoleSilently(assetId, data);
}
protected void processConsoleAssetChange(Asset asset, PersistenceEvent persistenceEvent) {