Refactor foreground notification text

This commit is contained in:
Sternagfonkel
2022-02-27 21:02:45 +01:00
committed by GitHub
parent fff261c1fd
commit bd5b8b2c15
4 changed files with 55 additions and 32 deletions

View File

@@ -243,7 +243,7 @@ class WebSocketConnection {
}
interface OnNetworkFailureRunnable {
void execute(long millis);
void execute(int minutes);
}
enum State {

View File

@@ -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(

View File

@@ -20,9 +20,9 @@
<string name="not_available">Cannot connect to %s</string>
<string name="auth_failed">Server returned 401 unauthorized while trying to get current user. This can be caused by deleting the client for this session.</string>
<string name="other_error">Could not get current user. Server (%s) responded with code %d: body (first 200 chars): %s</string>
<string name="websocket_failed">Connection failed, trying again in %d minutes</string>
<string name="websocket_closed_logout">User action required: Please login, the authentication token isn\'t valid anymore.</string>
<string name="websocket_closed_try_reconnect">Connection closed, trying to establish a new one.</string>
<string name="user_action">User action required</string>
<string name="websocket_closed_logout">Please login, the authentication token isn\'t valid anymore.</string>
<string name="websocket_closed">Connection closed</string>
<string name="grouped_message">Received %d messages while being disconnected</string>
<string name="delete_all">Delete all</string>
<string name="delete_app">Delete this application</string>
@@ -54,8 +54,8 @@
<string name="ack">Are you sure?</string>
<string name="missed_messages">Missed messages</string>
<string name="grouped_notification_text">New Messages</string>
<string name="websocket_listening">Listening to %s</string>
<string name="websocket_could_not_connect">Could not connect %s</string>
<string name="websocket_listening">Connected</string>
<string name="websocket_could_not_connect">Could not connect</string>
<string name="websocket_init">Initializing</string>
<string name="versions">gotify/android v%s; gotify/server v%s</string>
<string name="advanced_settings">Advanced Settings</string>
@@ -65,7 +65,6 @@
<string name="warning">Warning</string>
<string name="http_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.</string>
<string name="i_understand">I Understand</string>
<string name="websocket_no_network">Waiting for network</string>
<string name="connection">%s@%s</string>
<string name="no_messages_yet">There are no messages, yet.\nSend a message to Gotify\nand it will appear here.</string>
<string name="title_activity_settings">Settings</string>
@@ -84,4 +83,11 @@
<string name="push_missing_app_info">There are no applications available on the server to push a message to.</string>
<string name="message_copied_to_clipboard">Content copied to clipboard</string>
<string name="not_loggedin_share">Cannot share to Gotify, because you aren\'t logged in.</string>
<string name="websocket_not_connected">Not connected</string>
<string name="websocket_reconnect">Trying to reconnect</string>
<plurals name="websocket_retry_interval">
<item quantity="one">in %d minute</item>
<item quantity="other">in %d minutes</item>
</plurals>
</resources>