Skip to content

armgit / core

Public Imported from GitHub
0

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

Android console updates for push notifications

Rich Turner8986d54f1bb2
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 16064f8ce..653b14712 100644
--- a/console/android/src/main/java/org/openremote/android/MainActivity.java
+++ b/console/android/src/main/java/org/openremote/android/MainActivity.java
@@ -147,11 +147,6 @@ public class MainActivity extends Activity {
webView.restoreState(savedInstanceState);
} else {
initializeWebView();
- if (!getIntent().hasExtra("appUrl")) {
- String url = getClientUrl();
- LOG.fine("Loading web view: " + url);
- webView.loadUrl(url);
- }
}
openIntentUrl(getIntent());
@@ -164,7 +159,11 @@ public class MainActivity extends Activity {
}
protected void openIntentUrl(Intent intent) {
- if (intent.hasExtra("appUrl")) {
+ if (!intent.hasExtra("appUrl")) {
+ String url = getClientUrl();
+ LOG.fine("Loading web view: " + url);
+ webView.loadUrl(url);
+ } else {
String url = getClientUrl();
String intentUrl = intent.getStringExtra("appUrl");
if (intentUrl != null) {
@@ -175,14 +174,8 @@ public class MainActivity extends Activity {
}
}
- 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);
- }
+ LOG.fine("Loading web view: " + url);
+ webView.loadUrl(url);
}
}
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 13da1b464..3176888e7 100644
--- a/console/android/src/main/java/org/openremote/android/ORFirebaseMessagingService.java
+++ b/console/android/src/main/java/org/openremote/android/ORFirebaseMessagingService.java
@@ -13,10 +13,9 @@ 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.AlertButton;
import org.openremote.android.service.GeofenceProvider;
import org.openremote.android.service.TokenService;
@@ -80,32 +79,21 @@ public class ORFirebaseMessagingService extends com.google.firebase.messaging.Fi
Map<String, String> messageData = remoteMessage.getData();
- String notificationId = messageData.get("notification-id");
- if (notificationId != null && !notificationId.isEmpty()) {
- Long notiId = Long.parseLong(notificationId);
+ // Mark as delivered on the server
+ String notificationIdStr = messageData.get("notification-id");
+ Long notificationId = null;
+
+ if (notificationIdStr != null && !notificationIdStr.isEmpty()) {
+ notificationId = Long.parseLong(notificationIdStr);
String consoleId = getSharedPreferences(getApplicationContext().getString(R.string.app_name), Context.MODE_PRIVATE).getString("consoleId", "");
if (!TextUtils.isEmpty(consoleId)) {
- tokenService.notificationDelivered(notiId, consoleId);
+ tokenService.notificationDelivered(notificationId, consoleId);
}
}
- if (messageData.containsKey("buttons")) {
- String buttonsJson = messageData.get("buttons");
- AlertButton[] buttons = null;
+ boolean isSilent = !messageData.containsKey("or-title");
- 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")) {
+ if (isSilent) {
String action = remoteMessage.getData().get("action");
switch (action) {
@@ -120,14 +108,39 @@ public class ORFirebaseMessagingService extends com.google.firebase.messaging.Fi
break;
}
} else {
- // Check if message contains a data payload with or-title or-body and actions
- LOG.fine("Message data payload: " + remoteMessage.getData());
+
String title = messageData.get("or-title");
String body = messageData.get("or-body");
+ AlertButton[] buttons = null;
+ AlertAction action = null;
+
+ // Check for action (to be executed when notification is clicked)
+ if (messageData.containsKey("action")) {
+ String actionJson = messageData.get("action");
+
+ if (actionJson != null && !actionJson.isEmpty()) {
+ try {
+ action = new ObjectMapper().readValue(actionJson, AlertAction.class);
+ } catch (Exception e) {
+ LOG.log(Level.SEVERE, "Failed to de-serialise alert action", e);
+ }
+ }
+ }
+
+ // Check for buttons
+ if (messageData.containsKey("buttons")) {
+ String buttonsJson = messageData.get("buttons");
- if (title != null) {
- handleNotification(title, body, notificationId, null);
+ if (buttonsJson != null && !buttonsJson.isEmpty()) {
+ try {
+ buttons = new ObjectMapper().readValue(buttonsJson, AlertButton[].class);
+ } catch (Exception e) {
+ LOG.log(Level.SEVERE, "Failed to de-serialise alert actions", e);
+ }
+ }
}
+
+ handleNotification(notificationId, title, body, action, buttons);
}
}
}
@@ -142,7 +155,7 @@ public class ORFirebaseMessagingService extends com.google.firebase.messaging.Fi
service.createNotificationChannel(channel);
}
- private void handleNotification(String title, String body, String notificationId, AlertButton[] buttons) {
+ private void handleNotification(Long notificationId, String title, String body, AlertAction action, AlertButton[] buttons) {
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTICATION_CHANNEL_ID)
@@ -152,40 +165,45 @@ public class ORFirebaseMessagingService extends com.google.firebase.messaging.Fi
.setSmallIcon(R.drawable.notification_icon)
.setWhen(0)
.setPriority(Notification.PRIORITY_MAX)
- .setSound(defaultSoundUri);
+ .setSound(defaultSoundUri)
+ .setDeleteIntent(createActionIntent(notificationId, "\"CLOSED\"", null));
- if (buttons != null && buttons.length > 0) {
- for (AlertButton alertButton : buttons) {
- Intent actionIntent = new Intent(this, ORMessagingActionService.class);
- actionIntent.putExtra("notificationId", notificationId);
- actionIntent.setAction(Long.toString(System.currentTimeMillis()));
-
- 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");
- }
-
- PendingIntent actionPendingIntent = PendingIntent.getService(this,
- 0,
- actionIntent,
- PendingIntent.FLAG_ONE_SHOT);
- notificationBuilder = notificationBuilder.addAction(new NotificationCompat.Action(R.drawable.empty,
- alertButton.getTitle(),
- actionPendingIntent));
+ if (action != null) {
+ notificationBuilder.setContentIntent(createActionIntent(notificationId, null, action));
+ }
+ if (buttons != null) {
+ for (AlertButton alertButton : buttons) {
+ notificationBuilder.addAction(
+ new NotificationCompat.Action(R.drawable.empty,
+ alertButton.getTitle(),
+ createActionIntent(notificationId, alertButton.getTitle(), alertButton.getAction())));
}
}
LOG.fine("Showing notification (" + (buttons != null ? buttons.length + " buttons): " : "") + body);
- NotificationManager notificationManager =
- (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
- notificationManager.notify(notificationId.hashCode(), notificationBuilder.build());
+ NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
+ if (notificationManager != null) {
+ notificationManager.notify(notificationId.hashCode(), notificationBuilder.build());
+ }
+ }
+
+ private PendingIntent createActionIntent(Long notificationId, String acknowledgement, AlertAction alertAction) {
+
+ Intent actionIntent = new Intent(this, ORMessagingActionService.class);
+ actionIntent.putExtra("notificationId", notificationId);
+ actionIntent.putExtra("acknowledgement", acknowledgement);
+ actionIntent.setAction(Long.toString(System.currentTimeMillis()));
+
+ if (alertAction != null && alertAction.getUrl() != null && !alertAction.getUrl().isEmpty()) {
+ actionIntent.putExtra("appUrl", alertAction.getUrl());
+ actionIntent.putExtra("httpMethod", alertAction.getHttpMethod());
+ actionIntent.putExtra("silent", alertAction.isSilent());
+ actionIntent.putExtra("openInBrowser", alertAction.isOpenInBrowser());
+ actionIntent.putExtra("data", alertAction.getDataJson());
+ }
+
+ return PendingIntent.getService(this, 0, actionIntent, PendingIntent.FLAG_ONE_SHOT);
}
}
\ 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 eb7fd9c7b..83b62cc1a 100644
--- a/console/android/src/main/java/org/openremote/android/ORMessagingActionService.java
+++ b/console/android/src/main/java/org/openremote/android/ORMessagingActionService.java
@@ -5,6 +5,7 @@ import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
+import android.net.Uri;
import android.support.annotation.Nullable;
import android.text.TextUtils;
@@ -26,36 +27,49 @@ public class ORMessagingActionService extends IntentService {
@Override
protected void onHandleIntent(@Nullable Intent intent) {
-
Intent it = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
this.sendBroadcast(it);
- if (intent.hasExtra("notificationId")) {
- String notificationId = intent.getStringExtra("notificationId");
- String acknowledgement = intent.getStringExtra("acknowledgement");
-
- 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);
+ Long notificationId = intent.getLongExtra("notificationId", 0L);
+ String acknowledgement = intent.getStringExtra("acknowledgement");
- }
-
- NotificationManager manager = (NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE);
+ NotificationManager manager = (NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE);
+ if (manager != null) {
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("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);
+ String consoleId = getSharedPreferences(getApplicationContext().getString(R.string.app_name), Context.MODE_PRIVATE).getString("consoleId", "");
+ if (!TextUtils.isEmpty(consoleId)) {
+ tokenService.notificationAcknowledged(notificationId, consoleId, acknowledgement);
+ }
+
+ String appUrl = intent.getStringExtra("appUrl");
+
+ if (!TextUtils.isEmpty(appUrl)) {
+ boolean openInBrowser = intent.getBooleanExtra("openInBrowser", false);
+ boolean silent = intent.getBooleanExtra("silent", false);
+ String data = intent.getStringExtra("data");
+ String httpMethod = intent.getStringExtra("httpMethod");
+ httpMethod = TextUtils.isEmpty(httpMethod) ? "GET" : httpMethod;
+
+ if (openInBrowser) {
+ // Don't load the app just send straight to browser
+ Intent i = new Intent(Intent.ACTION_VIEW);
+ i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+ i.setData(Uri.parse(appUrl));
+ startActivity(i);
+ } else if (silent) {
+ // Do silent HTTP request
+ tokenService.executeRequest(httpMethod, appUrl, data);
+ } else {
+ 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("appUrl", appUrl);
+ 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 1c259bb02..c742f2ad1 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
@@ -1,11 +1,13 @@
package org.openremote.android.service;
+import com.fasterxml.jackson.databind.JsonNode;
+
import java.io.Serializable;
public class AlertAction implements Serializable {
private String url;
private String httpMethod;
- private String data;
+ private JsonNode data;
private boolean silent;
private boolean openInBrowser;
@@ -25,14 +27,18 @@ public class AlertAction implements Serializable {
this.httpMethod = httpMethod;
}
- public String getData() {
+ public JsonNode getData() {
return data;
}
- public void setData(String data) {
+ public void setData(JsonNode data) {
this.data = data;
}
+ public String getDataJson() {
+ return data == null ? null : data.toString();
+ }
+
public boolean isSilent() {
return silent;
}
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 1d56a5140..a25c7386d 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
@@ -12,17 +12,6 @@ public interface RestApiResource {
@PUT("/{realm}/notification/token")
Call<Void> updateToken(@Path("realm") String realm, @Header("Authorization") String authorization, @Field("token") String token, @Field("device_id") String deviceId, @Field("device_type") String deviceType);
-
- @GET("/{realm}/notification/alert")
- Call<List<AlertNotification>> getAlertNotification(@Path("realm") String realm, @Header("Authorization") String authorization);
-
- @DELETE("/{realm}/notification/alert/{id}")
- Call<Void> deleteNotification(@Path("realm") String realm, @Header("Authorization") String authorization,@Path("id") Long id);
-
- @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);
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 82927d861..e1f6e8ad2 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
@@ -4,11 +4,15 @@ import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
+import android.text.TextUtils;
+import okhttp3.*;
+import okhttp3.internal.http.HttpMethod;
import org.openremote.android.R;
import java.io.IOException;
import java.security.cert.CertificateException;
import java.util.List;
+import java.util.Locale;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -20,7 +24,6 @@ import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
-import okhttp3.OkHttpClient;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
@@ -38,17 +41,26 @@ public class TokenService {
private final String fcmTokenKey;
private final String deviceIdKey;
private final String realm;
+ private final String baseUrl;
+ private final OkHttpClient okHttpClient;
private final OAuth2Service oauth2Service;
private final RestApiResource restApiResource;
public TokenService(Context context) {
+ baseUrl = context.getString(R.string.OR_BASE_SERVER);
+
Retrofit.Builder builder = new Retrofit.Builder()
- .baseUrl(context.getString(R.string.OR_BASE_SERVER))
+ .baseUrl(baseUrl)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(JacksonConverterFactory.create());
+
if (Boolean.parseBoolean(context.getString(R.string.SSL_IGNORE))) {
- builder.client(getUnsafeOkHttpClient());
+ okHttpClient = getUnsafeOkHttpClient();
+ } else {
+ okHttpClient = new OkHttpClient();
}
+
+ builder.client(okHttpClient);
Retrofit retrofit = builder.build();
oauth2Service = retrofit.create(OAuth2Service.class);
@@ -58,15 +70,6 @@ public class TokenService {
fcmTokenKey = context.getString(R.string.SHARED_PREF_FCM_TOKEN);
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 deviceId = sharedPref.getString(deviceIdKey, null);
- if (refreshToken != null && fcmToken != null && deviceId != null) {
- LOG.fine("On create, have refresh token, sending FCM token");
- sendFCMToken(fcmToken, deviceId);
- } else {
- LOG.fine("On create, no refresh or FCM token or device ID, skipping update...");
- }
}
public void saveToken(String refreshToken) {
@@ -74,15 +77,6 @@ public class TokenService {
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString(refreshTokenKey, refreshToken);
editor.commit();
-
- 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");
- sendFCMToken(fcmToken, deviceId);
- } else {
- LOG.fine("On save refresh token, no device ID or FCM token, skipping update...");
- }
}
public String getJsonToken() {
@@ -119,113 +113,82 @@ public class TokenService {
}
public void sendOrStoreFCMToken(String fcmToken, String deviceId) {
- String refreshToken_key = sharedPref.getString(this.refreshTokenKey, null);
- storeFCMToken(fcmToken, deviceId);
- if (refreshToken_key != null) {
- sendFCMToken(fcmToken, deviceId);
- } else {
- LOG.fine("On send or store FCM token, no refresh token key (user never logged in on this device?), skipping update...");
- }
- }
-
- private void storeFCMToken(String fcmToken, String deviceId) {
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString(fcmTokenKey, fcmToken);
editor.putString(deviceIdKey, deviceId);
editor.commit();
}
- private void sendFCMToken(final String fcmToken, final String id) {
- LOG.fine("Sending FCM token for device ID: " + id);
- withAccessToken(new TokenCallback() {
- @Override
- 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
- /*
- 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(Throwable t) {
- LOG.log(Level.SEVERE, "Sending FCM device token failed (no access token), for device ID: " + id, t);
- }
- });
-
- }
-
- public void getAlerts(final Callback<List<AlertNotification>> callback) {
-
- withAccessToken(new TokenCallback() {
- @Override
- public void onToken(String accessToken) {
- restApiResource.getAlertNotification(realm, accessToken).enqueue(callback);
- }
-
- @Override
- public void onFailure(Throwable t) {
- callback.onFailure(null, t);
- }
- });
- }
-
public void clearToken() {
SharedPreferences.Editor editor = sharedPref.edit();
editor.remove(refreshTokenKey);
editor.commit();
}
- public void deleteAlert(final Long id) {
+ // TODO: Support other content types
+ public void executeRequest(String httpMethod, String url, String body) {
- withAccessToken(new TokenCallback() {
- @Override
- public void onToken(String accessToken) throws IOException {
- restApiResource.deleteNotification(realm, accessToken, id).enqueue(new Callback<Void>() {
+ // If relative URL assume it is relative to base URL
+ if (!url.toLowerCase(Locale.ROOT).startsWith("http")) {
+ if (url.startsWith("/")) {
+ url = baseUrl + url;
+ } else {
+ url = baseUrl + "/" + url;
+ }
+ }
+
+ // If URL is for our server and refresh token present then use authentication
+ String refreshToken = sharedPref.getString(refreshTokenKey, null);
+ boolean useAuth = url.startsWith(baseUrl) && !TextUtils.isEmpty(refreshToken);
+ final Request.Builder requestBuilder = new Request.Builder().url(url);
+ RequestBody requestBody = !TextUtils.isEmpty(body)
+ ? RequestBody.create(MediaType.parse("application/json; charset=utf-8"), body)
+ : null;
+
+ try {
+ switch (httpMethod.toUpperCase(Locale.ROOT)) {
+ case "GET":
+ requestBuilder.get();
+ break;
+ case "POST":
+ requestBuilder.post(requestBody);
+ break;
+ case "PUT":
+ requestBuilder.put(requestBody);
+ break;
+ case "PATCH":
+ requestBuilder.patch(requestBody);
+ break;
+ case "DELETE":
+ if (requestBody != null) {
+ requestBuilder.delete(requestBody);
+ } else {
+ requestBuilder.delete();
+ }
+ break;
+ default:
+ throw new IllegalArgumentException("Unsupported HTTP Method: " + httpMethod);
+ }
+
+ if (useAuth) {
+ withAccessToken(new TokenCallback() {
@Override
- public void onResponse(Call<Void> call, Response<Void> response) {
- if (response.code() != 204) {
- LOG.severe("Error deleting alert notification: " + id + ", response code: " + response.code());
- } else {
- LOG.fine("Alert notification deleted successfully: " + id);
- }
+ public void onToken(String accessToken) throws IOException {
+ requestBuilder.addHeader("Authorization", accessToken);
+ okHttpClient.newCall(requestBuilder.build()).execute();
}
@Override
- public void onFailure(Call<Void> call, Throwable t) {
- LOG.log(Level.SEVERE, "Error deleting alert notification: " + id, t);
+ public void onFailure(Throwable t) {
+ LOG.log(Level.SEVERE, "HTTP request failed", t);
}
});
-
+ } else {
+ okHttpClient.newCall(requestBuilder.build()).execute();
}
-
- @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 deleting notification (no access token): " + id, t);
- }
- });
+ } catch (Exception e) {
+ LOG.log(Level.SEVERE, "HTTP request failed", e);
+ }
}
// public void executeAction(final AlertAction alertAction) {
@@ -273,7 +236,12 @@ public class TokenService {
});
}
- public void notificationAcknowledged(final Long notificationId, final String fcmToken, final String acknowledgement) {
+ public void notificationAcknowledged(final Long notificationId, final String fcmToken, String acknowledgement) {
+
+ if (TextUtils.isEmpty(acknowledgement)) {
+ acknowledgement = "";
+ }
+
restApiResource.notificationAcknowledged(realm, notificationId, fcmToken, acknowledgement).enqueue(new Callback<Void>() {
@Override
public void onResponse(Call<Void> call, Response<Void> response) {