From bd5b8b2c15d2513c4d7fb53a860e66596334045f Mon Sep 17 00:00:00 2001 From: Sternagfonkel <87871070+Sternagfonkel@users.noreply.github.com> Date: Sun, 27 Feb 2022 21:02:45 +0100 Subject: [PATCH] Refactor foreground notification text --- README.md | 2 +- .../gotify/service/WebSocketConnection.java | 2 +- .../gotify/service/WebSocketService.java | 65 ++++++++++++------- app/src/main/res/values/strings.xml | 18 +++-- 4 files changed, 55 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 5488f8d..271086c 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ See also https://dontkillmyapp.com for phone manufacturer specific instructions *Only possible for Android version >= 8* -The foreground notification with content like `Listening to https://push.yourdomain.eu` can be manually minimized to be less intrusive: +The foreground notification showing the connection status can be manually minimized to be less intrusive: * Open Settings -> Apps -> Gotify * Click Notifications diff --git a/app/src/main/java/com/github/gotify/service/WebSocketConnection.java b/app/src/main/java/com/github/gotify/service/WebSocketConnection.java index 3915d10..30e3e1d 100644 --- a/app/src/main/java/com/github/gotify/service/WebSocketConnection.java +++ b/app/src/main/java/com/github/gotify/service/WebSocketConnection.java @@ -243,7 +243,7 @@ class WebSocketConnection { } interface OnNetworkFailureRunnable { - void execute(long millis); + void execute(int minutes); } enum State { diff --git a/app/src/main/java/com/github/gotify/service/WebSocketService.java b/app/src/main/java/com/github/gotify/service/WebSocketService.java index 6cc241e..658fc91 100644 --- a/app/src/main/java/com/github/gotify/service/WebSocketService.java +++ b/app/src/main/java/com/github/gotify/service/WebSocketService.java @@ -93,7 +93,7 @@ public class WebSocketService extends Service { private void startPushService() { UncaughtExceptionHandler.registerCurrentThread(); - foreground(getString(R.string.websocket_init)); + showForegroundNotification(getString(R.string.websocket_init)); if (lastReceivedMessage.get() == NOT_LOADED) { missingMessageUtil.lastReceivedMessage(lastReceivedMessage::set); @@ -113,8 +113,7 @@ public class WebSocketService extends Service { .onOpen(this::onOpen) .onClose(this::onClose) .onBadRequest(this::onBadRequest) - .onNetworkFailure( - (min) -> foreground(getString(R.string.websocket_failed, min))) + .onNetworkFailure(this::onNetworkFailure) .onMessage(this::onMessage) .onReconnected(this::notifyMissedNotifications) .start(); @@ -126,7 +125,8 @@ public class WebSocketService extends Service { } private void onClose() { - foreground(getString(R.string.websocket_closed_try_reconnect)); + showForegroundNotification( + getString(R.string.websocket_closed), getString(R.string.websocket_reconnect)); ClientFactory.userApiWithToken(settings) .currentUser() .enqueue( @@ -134,7 +134,9 @@ public class WebSocketService extends Service { (ignored) -> this.doReconnect(), (exception) -> { if (exception.code() == 401) { - foreground(getString(R.string.websocket_closed_logout)); + showForegroundNotification( + getString(R.string.user_action), + getString(R.string.websocket_closed_logout)); } else { Log.i( "WebSocket closed but the user still authenticated, trying to reconnect"); @@ -152,11 +154,20 @@ public class WebSocketService extends Service { } private void onBadRequest(String message) { - foreground(getString(R.string.websocket_could_not_connect, message)); + showForegroundNotification(getString(R.string.websocket_could_not_connect), message); + } + + private void onNetworkFailure(int minutes) { + String status = getString(R.string.websocket_not_connected); + String intervalUnit = + getResources() + .getQuantityString(R.plurals.websocket_retry_interval, minutes, minutes); + showForegroundNotification( + status, getString(R.string.websocket_reconnect) + ' ' + intervalUnit); } private void onOpen() { - foreground(getString(R.string.websocket_listening, settings.url())); + showForegroundNotification(getString(R.string.websocket_listening)); } private void notifyMissedNotifications() { @@ -222,28 +233,34 @@ public class WebSocketService extends Service { return null; } - private void foreground(String message) { + private void showForegroundNotification(String title) { + showForegroundNotification(title, null); + } + + private void showForegroundNotification(String title, String message) { Intent notificationIntent = new Intent(this, MessagesActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); - Notification notification = - new NotificationCompat.Builder(this, NotificationSupport.Channel.FOREGROUND) - .setSmallIcon(R.drawable.ic_gotify) - .setOngoing(true) - .setPriority(NotificationCompat.PRIORITY_MIN) - .setShowWhen(false) - .setWhen(0) - .setContentTitle(getString(R.string.app_name)) - .setContentText(message) - .setStyle(new NotificationCompat.BigTextStyle().bigText(message)) - .setContentIntent(pendingIntent) - .setColor( - ContextCompat.getColor( - getApplicationContext(), R.color.colorPrimary)) - .build(); + NotificationCompat.Builder notificationBuilder = + new NotificationCompat.Builder(this, NotificationSupport.Channel.FOREGROUND); + notificationBuilder.setSmallIcon(R.drawable.ic_gotify); + notificationBuilder.setOngoing(true); + notificationBuilder.setPriority(NotificationCompat.PRIORITY_MIN); + notificationBuilder.setShowWhen(false); + notificationBuilder.setWhen(0); + notificationBuilder.setContentTitle(title); - startForeground(NotificationSupport.ID.FOREGROUND, notification); + if (message != null) { + notificationBuilder.setContentText(message); + notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(message)); + } + + notificationBuilder.setContentIntent(pendingIntent); + notificationBuilder.setColor( + ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary)); + + startForeground(NotificationSupport.ID.FOREGROUND, notificationBuilder.build()); } private void showNotification( diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index cfa850c..f2bc129 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -20,9 +20,9 @@ Cannot connect to %s Server returned 401 unauthorized while trying to get current user. This can be caused by deleting the client for this session. Could not get current user. Server (%s) responded with code %d: body (first 200 chars): %s - Connection failed, trying again in %d minutes - User action required: Please login, the authentication token isn\'t valid anymore. - Connection closed, trying to establish a new one. + User action required + Please login, the authentication token isn\'t valid anymore. + Connection closed Received %d messages while being disconnected Delete all Delete this application @@ -54,8 +54,8 @@ Are you sure? Missed messages New Messages - Listening to %s - Could not connect %s + Connected + Could not connect Initializing gotify/android v%s; gotify/server v%s Advanced Settings @@ -65,7 +65,6 @@ Warning Using http is insecure and it\'s recommend to use https instead. Use your favorite search engine to get more information about this topic. I Understand - Waiting for network %s@%s There are no messages, yet.\nSend a message to Gotify\nand it will appear here. Settings @@ -84,4 +83,11 @@ There are no applications available on the server to push a message to. Content copied to clipboard Cannot share to Gotify, because you aren\'t logged in. + + Not connected + Trying to reconnect + + in %d minute + in %d minutes +