diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/api/SSEListener.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/api/SSEListener.kt index 4049fbe..1fb962e 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/api/SSEListener.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/api/SSEListener.kt @@ -15,6 +15,7 @@ import org.unifiedpush.distributor.nextpush.services.FailureHandler import org.unifiedpush.distributor.nextpush.services.RestartNetworkCallback import org.unifiedpush.distributor.nextpush.services.RestartWorker import org.unifiedpush.distributor.nextpush.services.StartService +import org.unifiedpush.distributor.nextpush.utils.NotificationUtils.showLowKeepaliveNotification import org.unifiedpush.distributor.nextpush.utils.NotificationUtils.showStartErrorNotification import org.unifiedpush.distributor.nextpush.utils.TAG import java.lang.Exception @@ -47,6 +48,9 @@ class SSEListener(val context: Context) : EventSourceListener() { val message = Gson().fromJson(data, SSEResponse::class.java) keepalive = message.keepalive Log.d(TAG, "New keepalive: $keepalive") + if (keepalive < 25) { + showLowKeepaliveNotification(context, keepalive) + } } "message" -> { val message = Gson().fromJson(data, SSEResponse::class.java) diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/utils/NotificationUtils.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/utils/NotificationUtils.kt index 40eca4e..7f4b665 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/utils/NotificationUtils.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/utils/NotificationUtils.kt @@ -16,7 +16,6 @@ import org.unifiedpush.distributor.nextpush.activities.MainActivity const val NOTIFICATION_ID_FOREGROUND = 51115 const val NOTIFICATION_ID_WARNING = 51215 -const val NOTIFICATION_ID_START_ERROR = 51315 private data class ChannelData( val id: String, @@ -34,6 +33,8 @@ private data class NotificationData( ) object NotificationUtils { + private val Context.WARNING_CHANNEL_ID + get() = "${this.getString(R.string.app_name)}.Warning" private var warningShown = false private fun createNotificationChannel(context: Context, channelData: ChannelData) { @@ -139,7 +140,7 @@ object NotificationUtils { if (warningShown) { return } - val notificationChannelId = "${context.getString(R.string.app_name)}.Warning" + val notificationChannelId = context.WARNING_CHANNEL_ID createNotificationChannel( context, @@ -169,16 +170,23 @@ object NotificationUtils { warningShown = true } + fun deleteWarningNotification(context: Context) { + val notificationManager = + context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager + notificationManager.cancel(NOTIFICATION_ID_WARNING) + warningShown = false + } + fun showStartErrorNotification(context: Context) { - val notificationChannelId = "${context.getString(R.string.app_name)}.StartError" + val notificationChannelId = context.WARNING_CHANNEL_ID createNotificationChannel( context, ChannelData( notificationChannelId, - "Start error", + "Warning", NotificationManager.IMPORTANCE_HIGH, - context.getString(R.string.start_error_notif_description) + context.getString(R.string.warning_notif_description) ) ) @@ -186,7 +194,7 @@ object NotificationUtils { context, NotificationData( context.getString(R.string.start_error_notif_content), - context.getString(R.string.start_error_notif_ticker), + context.getString(R.string.warning_notif_ticker), Notification.PRIORITY_HIGH, false, notificationChannelId @@ -195,13 +203,35 @@ object NotificationUtils { true ) - show(context, NOTIFICATION_ID_START_ERROR, notification) + show(context, NOTIFICATION_ID_WARNING, notification) } - fun deleteWarningNotification(context: Context) { - val notificationManager = - context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager - notificationManager.cancel(NOTIFICATION_ID_WARNING) - warningShown = false + fun showLowKeepaliveNotification(context: Context, keepalive: Int) { + val notificationChannelId = context.WARNING_CHANNEL_ID + + createNotificationChannel( + context, + ChannelData( + notificationChannelId, + "Warning", + NotificationManager.IMPORTANCE_HIGH, + context.getString(R.string.warning_notif_description) + ) + ) + + val notification = createNotification( + context, + NotificationData( + context.getString(R.string.low_keepalive_notif_content).format(keepalive), + context.getString(R.string.warning_notif_ticker), + Notification.PRIORITY_HIGH, + false, + notificationChannelId + ), + null, + true + ) + + show(context, NOTIFICATION_ID_WARNING, notification) } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 4e7ae66..ba43f06 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -15,12 +15,11 @@ Account You are connected as: %s Restart Service - The service could not be started correctly. Check the configuration of your server. - The service can not start correctly - Error NextPush is disconnected - Warn when NextPush is disconnected + Warn when NextPush is disconnected or an issue occured. Warning + The service could not be started correctly. Check the configuration of your server. + The server app is configured with a low keepalive: %ss. It will drain your battery. We recommend using a higher keepalive. Foreground Nextcloud Files is not installed on your device.\nPlease install it market://details?id=com.nextcloud.client