Skip to content

armgit / core

Public Imported from GitHub
0

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

Implemented new notification data structure.

Michael Rademaker43d54cac142b
diff --git a/console/android/src/main/java/org/openremote/android/MainActivity.java b/console/android/src/main/java/org/openremote/android/MainActivity.java
index 87d5e1cc5..16064f8ce 100644
--- a/console/android/src/main/java/org/openremote/android/MainActivity.java
+++ b/console/android/src/main/java/org/openremote/android/MainActivity.java
@@ -119,8 +119,7 @@ public class MainActivity extends Activity {
protected String getClientUrl() {
String platform = "Android " + Build.VERSION.RELEASE;
- String url = getString(R.string.OR_BASE_SERVER) + getString(R.string.OR_CONSOLE_URL) + "?consolePlatform=" + platform;
- return url;
+ return getString(R.string.OR_BASE_SERVER) + getString(R.string.OR_CONSOLE_URL) + "?consolePlatform=" + platform;
}
@Override
@@ -136,21 +135,19 @@ public class MainActivity extends Activity {
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
// Enable remote debugging of WebView from Chrome Debugger tools
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
- if (0 != (getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE)) {
- LOG.info("Enabling remote debugging");
- WebView.setWebContentsDebuggingEnabled(true);
- }
+ if (0 != (getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE)) {
+ LOG.info("Enabling remote debugging");
+ WebView.setWebContentsDebuggingEnabled(true);
}
setContentView(R.layout.activity_web);
- webView = (WebView) findViewById(R.id.webview);
+ webView = findViewById(R.id.webview);
if (savedInstanceState != null) {
webView.restoreState(savedInstanceState);
} else {
initializeWebView();
- if (!getIntent().hasExtra("url")) {
+ if (!getIntent().hasExtra("appUrl")) {
String url = getClientUrl();
LOG.fine("Loading web view: " + url);
webView.loadUrl(url);
@@ -167,9 +164,9 @@ public class MainActivity extends Activity {
}
protected void openIntentUrl(Intent intent) {
- if (intent.hasExtra("url")) {
+ if (intent.hasExtra("appUrl")) {
String url = getClientUrl();
- String intentUrl = intent.getStringExtra("url");
+ String intentUrl = intent.getStringExtra("appUrl");
if (intentUrl != null) {
if (intentUrl.startsWith("http") || intentUrl.startsWith("https")) {
url = intentUrl;
@@ -178,8 +175,14 @@ public class MainActivity extends Activity {
}
}
- LOG.fine("Loading web view: " + url);
- webView.loadUrl(url);
+ boolean openInBrowser = intent.getBooleanExtra("openInBrowser", false);
+ if (openInBrowser) {
+ Intent browserUrl = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
+ startActivity(browserUrl);
+ } else {
+ LOG.fine("Loading web view: " + url);
+ webView.loadUrl(url);
+ }
}
}
@@ -388,28 +391,28 @@ public class MainActivity extends Activity {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
geofenceProvider.enable(MainActivity.this,
- String.format("%s/%s",
- getString(R.string.OR_BASE_SERVER),
- getString(R.string.OR_REALM)),
- consoleId,
- new GeofenceProvider.EnableCallback() {
- @Override
- public void accept(@NotNull Map<String, ?> responseData) {
- try {
- final String jsonString = new ObjectMapper().writeValueAsString(responseData);
- MainActivity.this.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- webView.evaluateJavascript(String.format(
- "openremote.INSTANCE.console.handleProviderResponse('%s')",
- jsonString), null);
- }
- });
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
+ String.format("%s/%s",
+ getString(R.string.OR_BASE_SERVER),
+ getString(R.string.OR_REALM)),
+ consoleId,
+ new GeofenceProvider.EnableCallback() {
+ @Override
+ public void accept(@NotNull Map<String, ?> responseData) {
+ try {
+ final String jsonString = new ObjectMapper().writeValueAsString(responseData);
+ MainActivity.this.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ webView.evaluateJavascript(String.format(
+ "openremote.INSTANCE.console.handleProviderResponse('%s')",
+ jsonString), null);
+ }
+ });
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ });
}
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
@@ -486,15 +489,15 @@ public class MainActivity extends Activity {
if (consoleId != null) {
((MainActivity) activity).consoleId = consoleId;
geofenceProvider.enable(MainActivity.this, String.format("%s/%s",
- getString(R.string.OR_BASE_SERVER),
- getString(R.string.OR_REALM)),
- consoleId, new GeofenceProvider.EnableCallback() {
- @Override
- public void accept(@NotNull Map<String, ?> responseData) {
- //noinspection unchecked
- notifyClient((Map<String, Object>) responseData);
- }
- });
+ getString(R.string.OR_BASE_SERVER),
+ getString(R.string.OR_REALM)),
+ consoleId, new GeofenceProvider.EnableCallback() {
+ @Override
+ public void accept(@NotNull Map<String, ?> responseData) {
+ //noinspection unchecked
+ notifyClient((Map<String, Object>) responseData);
+ }
+ });
}
}
} else if (action.equalsIgnoreCase("PROVIDER_DISABLE")) {
@@ -519,6 +522,13 @@ public class MainActivity extends Activity {
response.put("success", true);
notifyClient(response);
} else if (action.equalsIgnoreCase("PROVIDER_ENABLE")) {
+ if (data != null) {
+ String consoleId = data.getString("consoleId");
+ if (consoleId != null) {
+ sharedPreferences.edit().putString(GeofenceProvider.Companion.getConsoleIdKey(), consoleId).apply();
+ }
+ }
+
// TODO: Implement topic support
String fcmToken = FirebaseInstanceId.getInstance().getToken();
Map<String, Object> response = new HashMap<>();
diff --git a/console/android/src/main/java/org/openremote/android/ORFirebaseMessagingService.java b/console/android/src/main/java/org/openremote/android/ORFirebaseMessagingService.java
index 3d6761c7b..13da1b464 100644
--- a/console/android/src/main/java/org/openremote/android/ORFirebaseMessagingService.java
+++ b/console/android/src/main/java/org/openremote/android/ORFirebaseMessagingService.java
@@ -12,15 +12,16 @@ import android.net.Uri;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.app.NotificationCompat;
+import android.text.TextUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.firebase.messaging.RemoteMessage;
-import org.openremote.android.service.AlertAction;
-import org.openremote.android.service.AlertNotification;
+import org.openremote.android.service.AlertButton;
import org.openremote.android.service.GeofenceProvider;
import org.openremote.android.service.TokenService;
+import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -76,7 +77,35 @@ public class ORFirebaseMessagingService extends com.google.firebase.messaging.Fi
if (remoteMessage.getNotification() != null) {
LOG.fine("Message contains notification body: " + remoteMessage.getNotification().getBody());
} else if (remoteMessage.getData() != null && !remoteMessage.getData().isEmpty()) {
- if (remoteMessage.getData().containsKey("action")) {
+
+ Map<String, String> messageData = remoteMessage.getData();
+
+ String notificationId = messageData.get("notification-id");
+ if (notificationId != null && !notificationId.isEmpty()) {
+ Long notiId = Long.parseLong(notificationId);
+ String consoleId = getSharedPreferences(getApplicationContext().getString(R.string.app_name), Context.MODE_PRIVATE).getString("consoleId", "");
+ if (!TextUtils.isEmpty(consoleId)) {
+ tokenService.notificationDelivered(notiId, consoleId);
+ }
+ }
+
+ if (messageData.containsKey("buttons")) {
+ String buttonsJson = messageData.get("buttons");
+ AlertButton[] buttons = null;
+
+ if (buttonsJson != null && buttonsJson.length() > 0) {
+ try {
+ buttons = new ObjectMapper().readValue(buttonsJson, AlertButton[].class);
+ } catch (Exception e) {
+ LOG.log(Level.SEVERE, "Failed to de-serialise alert actions", e);
+ }
+ }
+ String title = messageData.get("or-title");
+ String body = messageData.get("or-body");
+
+ handleNotification(title, body, notificationId, buttons);
+
+ } else if (messageData.containsKey("action")) {
String action = remoteMessage.getData().get("action");
switch (action) {
@@ -93,54 +122,15 @@ public class ORFirebaseMessagingService extends com.google.firebase.messaging.Fi
} else {
// Check if message contains a data payload with or-title or-body and actions
LOG.fine("Message data payload: " + remoteMessage.getData());
- String title = remoteMessage.getData().get("or-title");
- String body = remoteMessage.getData().get("or-body");
- String actionsJson = remoteMessage.getData().get("actions");
- AlertAction[] actions = null;
+ String title = messageData.get("or-title");
+ String body = messageData.get("or-body");
- if (actionsJson != null && actionsJson.length() > 0) {
- try {
- actions = new ObjectMapper().readValue(actionsJson, AlertAction[].class);
- } catch (Exception e) {
- LOG.log(Level.SEVERE, "Failed to de-serialise alert actions", e);
- }
+ if (title != null) {
+ handleNotification(title, body, notificationId, null);
}
- handleNotification(title, body, actions);
}
}
-
-// tokenService.getAlerts(new Callback<List<AlertNotification>>() {
-// @Override
-// public void onResponse(Call<List<AlertNotification>> call, Response<List<AlertNotification>> response) {
-// List<AlertNotification> alertNotifications = response.body();
-// Set<String> actualAlertIds = new HashSet<>();
-// SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
-// Set<String> previousAlertIds = sharedPreferences.getStringSet(getString(R.string.SHARED_PREF_OPENED_ALERT), new HashSet<String>());
-//
-// for (AlertNotification alertNotification : alertNotifications) {
-// if (!previousAlertIds.contains(alertNotification.getId().toString())) {
-// sendNotification(alertNotification);
-// previousAlertIds.add(alertNotification.getId().toString());
-// }
-// actualAlertIds.add(alertNotification.getId().toString());
-// LOG.fine("Retrieved alert notifications from service: " + alertNotification);
-// }
-// previousAlertIds.retainAll(actualAlertIds);
-// sharedPreferences.edit().putStringSet(getString(R.string.SHARED_PREF_OPENED_ALERT) ,previousAlertIds ).commit();
-// }
-//
-// @Override
-// public void onFailure(Call<List<AlertNotification>> call, Throwable t) {
-// LOG.log(Level.SEVERE, "Error retrieving alert", t);
-// }
-// });
-
-
- // Also if you intend on generating your own notifications as a result of a received FCM
- // message, here is where that should be initiated. See sendNotification method below.
}
- // [END receive_message]
-
@RequiresApi(Build.VERSION_CODES.O)
private void createNotificationChannel() {
@@ -152,12 +142,9 @@ public class ORFirebaseMessagingService extends com.google.firebase.messaging.Fi
service.createNotificationChannel(channel);
}
- private void handleNotification(String title, String body, AlertAction[] actions) {
+ private void handleNotification(String title, String body, String notificationId, AlertButton[] buttons) {
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
- // TODO: ID to come from backend
- int id = title.hashCode() + body.hashCode();
-
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTICATION_CHANNEL_ID)
.setContentTitle(title)
.setStyle(new NotificationCompat.BigTextStyle().bigText(body))
@@ -167,73 +154,38 @@ public class ORFirebaseMessagingService extends com.google.firebase.messaging.Fi
.setPriority(Notification.PRIORITY_MAX)
.setSound(defaultSoundUri);
- if (actions != null && actions.length > 0) {
- for (AlertAction alertAction : actions) {
+ if (buttons != null && buttons.length > 0) {
+ for (AlertButton alertButton : buttons) {
Intent actionIntent = new Intent(this, ORMessagingActionService.class);
-
- actionIntent.putExtra("notificationId", id);
- actionIntent.putExtra("action", alertAction);
+ actionIntent.putExtra("notificationId", notificationId);
actionIntent.setAction(Long.toString(System.currentTimeMillis()));
- PendingIntent actionPendingIntent = PendingIntent.getService(this,
- 0,
- actionIntent,
- PendingIntent.FLAG_ONE_SHOT);
- notificationBuilder = notificationBuilder.addAction(new NotificationCompat.Action(R.drawable.empty,
- alertAction.getTitle(),
- actionPendingIntent));
- }
- }
-
- LOG.fine("Showing notification (" + (actions != null ? actions.length + " actions): " : "") + body);
-
- NotificationManager notificationManager =
- (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
- notificationManager.notify(id, notificationBuilder.build());
- }
-
-
- private void sendNotification(AlertNotification alertNotification) {
- int notificationId = alertNotification.getId().hashCode();
- Intent intent = new Intent(this, ORMessagingActionService.class);
- intent.putExtra("notification", alertNotification);
- PendingIntent pendingIntent = PendingIntent.getService(this,
- notificationId,
- intent,
- PendingIntent.FLAG_ONE_SHOT);
-
- Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
+ if (alertButton.getAction() != null) {
+ actionIntent.putExtra("appUrl", alertButton.getAction().getUrl());
+ actionIntent.putExtra("httpMethod", alertButton.getAction().getHttpMethod());
+ actionIntent.putExtra("silent", alertButton.getAction().isSilent());
+ actionIntent.putExtra("openInBrowser", alertButton.getAction().isOpenInBrowser());
+ actionIntent.putExtra("data", alertButton.getAction().getData());
+ actionIntent.putExtra("acknowledgement", "ACKNOWLEDGED");
+ } else {
+ actionIntent.putExtra("acknowledgement", "DECLINED");
+ }
- NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
- .setContentTitle(alertNotification.getTitle())
- .setStyle(new NotificationCompat.BigTextStyle().bigText(alertNotification.getMessage()))
- .setContentText(alertNotification.getMessage())
- .setDeleteIntent(pendingIntent)
- .setSmallIcon(R.drawable.notification_icon)
- .setWhen(0)
- .setPriority(Notification.PRIORITY_MAX)
- .setSound(defaultSoundUri);
+ PendingIntent actionPendingIntent = PendingIntent.getService(this,
+ 0,
+ actionIntent,
+ PendingIntent.FLAG_ONE_SHOT);
+ notificationBuilder = notificationBuilder.addAction(new NotificationCompat.Action(R.drawable.empty,
+ alertButton.getTitle(),
+ actionPendingIntent));
- for (AlertAction alertAction : alertNotification.getActions()) {
- Intent actionIntent = new Intent(this, ORMessagingActionService.class);
-
- actionIntent.putExtra("action", alertAction);
- actionIntent.putExtra("url", alertAction.getAppUrl());
- actionIntent.putExtra("notification", alertNotification);
- actionIntent.setAction(Long.toString(System.currentTimeMillis()));
- PendingIntent actionPendingIntent = PendingIntent.getService(this,
- notificationId,
- actionIntent,
- PendingIntent.FLAG_ONE_SHOT);
- notificationBuilder = notificationBuilder.addAction(new NotificationCompat.Action(R.drawable.empty,
- alertAction.getTitle(),
- actionPendingIntent));
+ }
}
- LOG.fine("Showing notification (" + alertNotification.getActions().size() + " actions): " + alertNotification.getMessage());
+ LOG.fine("Showing notification (" + (buttons != null ? buttons.length + " buttons): " : "") + body);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
- notificationManager.notify(notificationId, notificationBuilder.build());
+ notificationManager.notify(notificationId.hashCode(), notificationBuilder.build());
}
}
\ No newline at end of file
diff --git a/console/android/src/main/java/org/openremote/android/ORMessagingActionService.java b/console/android/src/main/java/org/openremote/android/ORMessagingActionService.java
index 3eafc6c62..eb7fd9c7b 100644
--- a/console/android/src/main/java/org/openremote/android/ORMessagingActionService.java
+++ b/console/android/src/main/java/org/openremote/android/ORMessagingActionService.java
@@ -3,11 +3,11 @@ package org.openremote.android;
import android.app.IntentService;
import android.app.NotificationManager;
import android.app.Service;
+import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
+import android.text.TextUtils;
-import org.openremote.android.service.AlertAction;
-import org.openremote.android.service.AlertNotification;
import org.openremote.android.service.TokenService;
public class ORMessagingActionService extends IntentService {
@@ -30,42 +30,31 @@ public class ORMessagingActionService extends IntentService {
Intent it = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
this.sendBroadcast(it);
- if (intent.hasExtra("notification")) {
- AlertNotification notification = (AlertNotification) intent.getSerializableExtra("notification");
- tokenService.deleteAlert(notification.getId());
- NotificationManager manager = (NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE);
- manager.cancel(notification.getId().hashCode());
- } else if (intent.hasExtra("notificationId")) {
- int id = intent.getIntExtra("notificationId", 0);
- if (id != 0) {
- NotificationManager manager = (NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE);
- manager.cancel(id);
- }
- }
+ if (intent.hasExtra("notificationId")) {
+ String notificationId = intent.getStringExtra("notificationId");
+ String acknowledgement = intent.getStringExtra("acknowledgement");
- AlertAction alertAction = (AlertAction) intent.getSerializableExtra("action");
+ String consoleId = getSharedPreferences(getApplicationContext().getString(R.string.app_name), Context.MODE_PRIVATE).getString("consoleId", "");
+ if (!TextUtils.isEmpty(consoleId)) {
+ tokenService.notificationAcknowledged(Long.parseLong(notificationId), consoleId, acknowledgement);
- if(alertAction != null) {
- if (AlertNotification.ActionType.ACTUATOR.equals(alertAction.getType())) {
- tokenService.executeAction(alertAction);
- } else if (alertAction.getAppUrl() != null) {
- // TODO: decide how to handle absolute URLs (i.e. open in the normal browser or inside the console?)
- // For now replicated behaviour of iOS for important demo
- Intent activityIntent = new Intent(this, MainActivity.class);
- activityIntent.setAction(Intent.ACTION_MAIN);
- activityIntent.addCategory(Intent.CATEGORY_LAUNCHER);
- activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
- Intent.FLAG_ACTIVITY_SINGLE_TOP);
- activityIntent.putExtra("url", alertAction.getAppUrl());
- startActivity(activityIntent);
}
- } else if (intent.hasExtra("url")) {
+
+ NotificationManager manager = (NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE);
+ manager.cancel(notificationId.hashCode());
+ }
+
+ if (intent.hasExtra("appUrl")) {
Intent activityIntent = new Intent(this, MainActivity.class);
activityIntent.setAction(Intent.ACTION_MAIN);
activityIntent.addCategory(Intent.CATEGORY_LAUNCHER);
activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
- activityIntent.putExtra("url", intent.getStringExtra("url"));
+ activityIntent.putExtra("appUrl", intent.getStringExtra("appUrl"));
+ activityIntent.putExtra("silent", intent.getBooleanExtra("silent", false));
+ activityIntent.putExtra("httpMethod", intent.getStringExtra("httpMethod"));
+ activityIntent.putExtra("openInBrowser", intent.getBooleanExtra("openInBrowser", false));
+ activityIntent.putExtra("data", intent.getStringExtra("data"));
startActivity(activityIntent);
}
}
diff --git a/console/android/src/main/java/org/openremote/android/service/AlertAction.java b/console/android/src/main/java/org/openremote/android/service/AlertAction.java
index 28b72fa2d..1c259bb02 100644
--- a/console/android/src/main/java/org/openremote/android/service/AlertAction.java
+++ b/console/android/src/main/java/org/openremote/android/service/AlertAction.java
@@ -3,69 +3,60 @@ package org.openremote.android.service;
import java.io.Serializable;
public class AlertAction implements Serializable {
- private AlertNotification.ActionType type;
- private String title;
- private String assetId;
- private String attributeName;
- private String appUrl;
- private String rawJson;
+ private String url;
+ private String httpMethod;
+ private String data;
+ private boolean silent;
+ private boolean openInBrowser;
- public String getAssetId() {
- return assetId;
+ public String getUrl() {
+ return url;
}
- public void setAssetId(String assetId) {
- this.assetId = assetId;
+ public void setUrl(String url) {
+ this.url = url;
}
- public String getAttributeName() {
- return attributeName;
+ public String getHttpMethod() {
+ return httpMethod;
}
- public void setAttributeName(String attributeName) {
- this.attributeName = attributeName;
+ public void setHttpMethod(String httpMethod) {
+ this.httpMethod = httpMethod;
}
- public String getRawJson() {
- return rawJson;
+ public String getData() {
+ return data;
}
- public void setRawJson(String rawJson) {
- this.rawJson = rawJson;
+ public void setData(String data) {
+ this.data = data;
}
- public AlertAction() {
+ public boolean isSilent() {
+ return silent;
}
- public AlertNotification.ActionType getType() {
- return type;
+ public void setSilent(boolean silent) {
+ this.silent = silent;
}
- public void setType(AlertNotification.ActionType type) {
- this.type = type;
+ public boolean isOpenInBrowser() {
+ return openInBrowser;
}
- public String getTitle() {
- return title;
- }
-
- public void setTitle(String title) {
- this.title = title;
- }
-
- public String getAppUrl() {
- return appUrl;
- }
-
- public void setAppUrl(String appUrl) {
- this.appUrl = appUrl;
+ public void setOpenInBrowser(boolean openInBrowser) {
+ this.openInBrowser = openInBrowser;
}
@Override
public String toString() {
return "AlertAction{" +
- "type=" + type +
- ", title='" + title + '\'' +
+ "url=" + url +
+ ", httpMethod=" + httpMethod +
+ ", data=" + data +
+ ", silent=" + silent +
+ ", openInBrowser=" + openInBrowser +
'}';
}
}
\ No newline at end of file
diff --git a/console/android/src/main/java/org/openremote/android/service/AlertButton.java b/console/android/src/main/java/org/openremote/android/service/AlertButton.java
new file mode 100644
index 000000000..138d67e5a
--- /dev/null
+++ b/console/android/src/main/java/org/openremote/android/service/AlertButton.java
@@ -0,0 +1,24 @@
+package org.openremote.android.service;
+
+import java.io.Serializable;
+
+public class AlertButton implements Serializable {
+ private String title;
+ private AlertAction action;
+
+ public String getTitle() {
+ return title;
+ }
+
+ public void setTitle(String title) {
+ this.title = title;
+ }
+
+ public AlertAction getAction() {
+ return action;
+ }
+
+ public void setAction(AlertAction action) {
+ this.action = action;
+ }
+}
diff --git a/console/android/src/main/java/org/openremote/android/service/RestApiResource.java b/console/android/src/main/java/org/openremote/android/service/RestApiResource.java
index c917f461e..1d56a5140 100644
--- a/console/android/src/main/java/org/openremote/android/service/RestApiResource.java
+++ b/console/android/src/main/java/org/openremote/android/service/RestApiResource.java
@@ -22,4 +22,13 @@ public interface RestApiResource {
@PUT("/{realm}/asset/{assetId}/attribute/{attributeName}")
@Headers("Content-Type:application/json")
Call<Void> updateAssetAction(@Path("realm") String realm, @Header("Authorization") String accessToken,@Path("assetId") String assetId,@Path("attributeName") String attributeName,@Body String rawJson);
+
+ @PUT("/{realm}/notification/{notificationId}/delivered")
+ @Headers("Content-Type:application/json")
+ Call<Void> notificationDelivered(@Path("realm") String realm, @Path("notificationId") Long notificationId, @Query("targetId") String targetId);
+
+ @PUT("/{realm}/notification/{notificationId}/acknowledged")
+ @Headers("Content-Type:application/json")
+ Call<Void> notificationAcknowledged(@Path("realm") String realm, @Path("notificationId") Long notificationId, @Query("targetId") String targetId, @Body String acknowledgement);
+
}
diff --git a/console/android/src/main/java/org/openremote/android/service/TokenService.java b/console/android/src/main/java/org/openremote/android/service/TokenService.java
index 2e5440478..82927d861 100644
--- a/console/android/src/main/java/org/openremote/android/service/TokenService.java
+++ b/console/android/src/main/java/org/openremote/android/service/TokenService.java
@@ -46,7 +46,7 @@ public class TokenService {
.baseUrl(context.getString(R.string.OR_BASE_SERVER))
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(JacksonConverterFactory.create());
- if (Boolean.parseBoolean(context.getString(R.string.SSL_IGNORE))) {
+ if (Boolean.parseBoolean(context.getString(R.string.SSL_IGNORE))) {
builder.client(getUnsafeOkHttpClient());
}
Retrofit retrofit = builder.build();
@@ -59,7 +59,7 @@ public class TokenService {
deviceIdKey = context.getString(R.string.SHARED_PREF_DEVICE_ID);
realm = context.getString(R.string.OR_REALM);
String refreshToken = sharedPref.getString(refreshTokenKey, null);
- String fcmToken = sharedPref.getString(fcmTokenKey, null);
+ String fcmToken = sharedPref.getString(fcmTokenKey, null);
String deviceId = sharedPref.getString(deviceIdKey, null);
if (refreshToken != null && fcmToken != null && deviceId != null) {
LOG.fine("On create, have refresh token, sending FCM token");
@@ -75,7 +75,7 @@ public class TokenService {
editor.putString(refreshTokenKey, refreshToken);
editor.commit();
- String fcmToken = sharedPref.getString(fcmTokenKey, null);
+ String fcmToken = sharedPref.getString(fcmTokenKey, null);
String deviceId = sharedPref.getString(deviceIdKey, null);
if (fcmToken != null && deviceId != null) {
LOG.fine("On save refresh token, sending FCM token");
@@ -142,30 +142,30 @@ public class TokenService {
public void onToken(String accessToken) {
Call call = restApiResource.updateToken(realm, accessToken, fcmToken, id, "ANDROID");
- call.enqueue(new Callback() {
- @Override
- public void onResponse(Call call, Response response) {
- if (response.code() != 204) {
- LOG.severe("Sending FCM device token failed for device ID: " + id +", response code: " + response.code());
- } else {
- LOG.fine("Sending FCM device token successful for device ID: " + id);
-
- // #40: Don't clean-up the FCM information from SharedPreferences, this means they are always sent
- // This ensures that if it gets cleaned from server, it is re-send again and notifications can be send
+ call.enqueue(new Callback() {
+ @Override
+ public void onResponse(Call call, Response response) {
+ if (response.code() != 204) {
+ LOG.severe("Sending FCM device token failed for device ID: " + id + ", response code: " + response.code());
+ } else {
+ LOG.fine("Sending FCM device token successful for device ID: " + id);
+
+ // #40: Don't clean-up the FCM information from SharedPreferences, this means they are always sent
+ // This ensures that if it gets cleaned from server, it is re-send again and notifications can be send
/*
SharedPreferences.Editor editor = sharedPref.edit();
editor.remove(fcmTokenKey);
editor.remove(deviceIdKey);
editor.commit();
*/
- }
}
+ }
- @Override
- public void onFailure(Call call, Throwable t) {
- LOG.log(Level.SEVERE, "Sending FCM device token failed for device ID: " + id, t);
- }
- });
+ @Override
+ public void onFailure(Call call, Throwable t) {
+ LOG.log(Level.SEVERE, "Sending FCM device token failed for device ID: " + id, t);
+ }
+ });
}
@Override
@@ -186,7 +186,7 @@ public class TokenService {
@Override
public void onFailure(Throwable t) {
- callback.onFailure(null,t);
+ callback.onFailure(null, t);
}
});
}
@@ -228,42 +228,69 @@ public class TokenService {
});
}
- public void executeAction(final AlertAction alertAction) {
- withAccessToken(new TokenCallback() {
+// public void executeAction(final AlertAction alertAction) {
+// withAccessToken(new TokenCallback() {
+// @Override
+// public void onToken(String accessToken) throws IOException {
+// restApiResource.updateAssetAction(realm, accessToken, alertAction.getAssetId(), alertAction.getAttributeName(), alertAction.getRawJson()).enqueue(new Callback<Void>() {
+// @Override
+// public void onResponse(Call<Void> call, Response<Void> response) {
+// if (response.code() != 204) {
+// LOG.severe("Error executing asset write: " + alertAction + ", response code: " + response.code());
+// } else {
+// LOG.fine("Asset write executed successfully: " + alertAction);
+// }
+// }
+//
+// @Override
+// public void onFailure(Call<Void> call, Throwable t) {
+// LOG.log(Level.SEVERE, "Error executing asset write: " + alertAction, t);
+// }
+// });
+//
+// }
+//
+// @Override
+// public void onFailure(Throwable t) {
+// // TODO We should tell the user to login again or it will never work
+// LOG.log(Level.SEVERE, "Error executing asset write (no access token): " + alertAction, t);
+// }
+// });
+//
+// }
+
+ public void notificationDelivered(final Long notificationId, final String fcmToken) {
+ restApiResource.notificationDelivered(realm, notificationId, fcmToken).enqueue(new Callback<Void>() {
@Override
- public void onToken(String accessToken) throws IOException {
- restApiResource.updateAssetAction(realm, accessToken, alertAction.getAssetId(), alertAction.getAttributeName(), alertAction.getRawJson()).enqueue(new Callback<Void>() {
- @Override
- public void onResponse(Call<Void> call, Response<Void> response) {
- if (response.code() != 204) {
- LOG.severe("Error executing asset write: " + alertAction + ", response code: " + response.code());
- } else {
- LOG.fine("Asset write executed successfully: " + alertAction);
- }
- }
-
- @Override
- public void onFailure(Call<Void> call, Throwable t) {
- LOG.log(Level.SEVERE, "Error executing asset write: " + alertAction, t);
- }
- });
+ public void onResponse(Call<Void> call, Response<Void> response) {
}
@Override
- public void onFailure(Throwable t) {
- // TODO We should tell the user to login again or it will never work
- LOG.log(Level.SEVERE, "Error executing asset write (no access token): " + alertAction, t);
+ public void onFailure(Call<Void> call, Throwable t) {
+
}
});
-
}
+ public void notificationAcknowledged(final Long notificationId, final String fcmToken, final String acknowledgement) {
+ restApiResource.notificationAcknowledged(realm, notificationId, fcmToken, acknowledgement).enqueue(new Callback<Void>() {
+ @Override
+ public void onResponse(Call<Void> call, Response<Void> response) {
+
+ }
+
+ @Override
+ public void onFailure(Call<Void> call, Throwable t) {
+
+ }
+ });
+ }
static OkHttpClient getUnsafeOkHttpClient() {
try {
// Create a trust manager that does not validate certificate chains
- final TrustManager[] trustAllCerts = new TrustManager[] {
+ final TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {