Commit eed7bcd7 authored by Christophe Henry's avatar Christophe Henry

Merge branch 'fix-fail-notification' into 'develop'

Fix notification regression

See merge request !46
parents 173f7405 bff6ff5a
......@@ -4,7 +4,10 @@ import android.app.Notification
import android.app.Service
import android.content.Intent
import android.os.Binder
import android.os.Build
import androidx.core.app.NotificationCompat
import fr.chenry.android.freshrss.R.drawable
import fr.chenry.android.freshrss.R.string
import fr.chenry.android.freshrss.store.Store
import fr.chenry.android.freshrss.utils.NotificationChannels
import fr.chenry.android.freshrss.utils.NotificationHelper
......@@ -14,24 +17,34 @@ import nl.komponents.kovenant.functional.bind
import nl.komponents.kovenant.ui.alwaysUi
import nl.komponents.kovenant.ui.failUi
class RefresherService : Service() {
class RefresherService: Service() {
private val refreshNotification =
createNotification(R.string.notification_refresh_title, R.string.notification_refresh_description)
createNotification(
NotificationChannels.REFRESH,
string.notification_refresh_title,
string.notification_refresh_description
)
.setCategory(Notification.CATEGORY_PROGRESS)
.setProgress(0, 0, true)
.setSmallIcon(R.drawable.ic_rss_feed_black_24dp)
.setOngoing(true)
.setPriority(NotificationCompat.PRIORITY_MIN)
.build()
private val failNotification =
createNotification(R.string.notification_refresh_failed_title, R.string.notification_refresh_failed_description)
createNotification(
NotificationChannels.ERRORS,
string.notification_refresh_failed_title,
string.notification_refresh_failed_description,
R.color.error
)
.setCategory(Notification.CATEGORY_ERROR)
.setSmallIcon(R.drawable.ic_rss_feed_red_24dp)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.build()
override fun onBind(intent: Intent) = RefresherBinder()
fun refresh(): Promise<Unit, Exception> {
if (Store.refreshingPromise.value != null) return Store.refreshingPromise.value!!
if(Store.refreshingPromise.value != null) return Store.refreshingPromise.value!!
FreshRSSApplication.notificationManager
.notify(NotificationHelper.ONGOING_REFRESH_NOTIFICATION, refreshNotification)
......@@ -39,19 +52,19 @@ class RefresherService : Service() {
Store.ensureToken()
val promise = Store.getSubscriptions()
.bind { Store.getUnreadCount() }
.bind {Store.getUnreadCount()}
.bind {
FreshRSSApplication
.database
.getAllSubcriptionsIds()
.blockingFirst()
.map { Store.getStreamContents(it) }
.let { all(it, cancelOthersOnError = false) }
.map {Store.getStreamContents(it)}
.let {all(it, cancelOthersOnError = false)}
}
.bind { Store.getUnreadItems() }
.bind {Store.getUnreadItems()}
Store.refreshingPromise.postValue(promise)
promise.always { Store.refreshingPromise.postValue(null) }
promise.always {Store.refreshingPromise.postValue(null)}
.failUi {
FreshRSSApplication
.notificationManager
......@@ -63,15 +76,19 @@ class RefresherService : Service() {
return promise
}
private fun createNotification(title: Int, text: Int) = NotificationCompat
.Builder(FreshRSSApplication.application, NotificationChannels.REFRESH.channelId)
.setContentTitle(FreshRSSApplication.getStringR(title))
.setContentText(FreshRSSApplication.getStringR(text))
.setPriority(NotificationCompat.PRIORITY_MIN)
.setOngoing(true)
.setShowWhen(false)
private fun createNotification(chan: NotificationChannels, title: Int, text: Int, color: Int? = null) =
NotificationCompat
.Builder(FreshRSSApplication.application, chan.channelId)
.setContentTitle(FreshRSSApplication.getStringR(title))
.setContentText(FreshRSSApplication.getStringR(text))
.setSmallIcon(drawable.ic_rss_feed_black_24dp)
.setShowWhen(false)
.apply {
if(color != null && Build.VERSION.SDK_INT > Build.VERSION_CODES.M)
this.color = FreshRSSApplication.context.getColor(color)
}
inner class RefresherBinder : Binder() {
inner class RefresherBinder: Binder() {
val service: RefresherService get() = this@RefresherService
}
}
......@@ -12,22 +12,48 @@ object NotificationHelper {
const val FAIL_REFRESH_NOTIFICATION = 2
}
enum class NotificationChannels(nameResourceId: Int, descriptionResourceId: Int, importance: Int? = null) {
REFRESH(R.string.notification_channel_refresh_title, R.string.notification_channel_refresh_description);
enum class NotificationChannels(nameResourceId: Int, descriptionResourceId: Int, importance: Importance) {
REFRESH(
R.string.notification_channel_refresh_title,
R.string.notification_channel_refresh_description,
Importance.IMPORTANCE_LOW
),
ERRORS(
R.string.notification_channel_errors_title,
R.string.notification_channel_errors_description,
Importance.IMPORTANCE_HIGH
);
val channelId: String get() = this.name
init {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) Try {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) Try {
FreshRSSApplication.application.getSystemService(Context.NOTIFICATION_SERVICE).apply {
this as NotificationManager
val n = FreshRSSApplication.getStringR(nameResourceId)
val i = importance ?: NotificationManager.IMPORTANCE_MIN
NotificationChannel(channelId, n, i).apply {
val message = FreshRSSApplication.getStringR(nameResourceId)
NotificationChannel(channelId, message, importance.asAndroid()).apply {
description = FreshRSSApplication.getStringR(descriptionResourceId)
setSound(null, null)
}.apply(::createNotificationChannel)
}
}
}
enum class Importance {
IMPORTANCE_NONE,
IMPORTANCE_MIN,
IMPORTANCE_LOW,
IMPORTANCE_DEFAULT,
IMPORTANCE_HIGH,
IMPORTANCE_MAX;
fun asAndroid() = if(Build.VERSION.SDK_INT < Build.VERSION_CODES.N) 0 else when(this) {
IMPORTANCE_NONE -> NotificationManager.IMPORTANCE_NONE
IMPORTANCE_MIN -> NotificationManager.IMPORTANCE_MIN
IMPORTANCE_LOW -> NotificationManager.IMPORTANCE_LOW
IMPORTANCE_DEFAULT -> NotificationManager.IMPORTANCE_DEFAULT
IMPORTANCE_HIGH -> NotificationManager.IMPORTANCE_HIGH
IMPORTANCE_MAX -> NotificationManager.IMPORTANCE_MAX
}
}
}
<vector
xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#E53935"
android:pathData="M6.18,17.82m-2.18,0a2.18,2.18 0,1 1,4.36 0a2.18,2.18 0,1 1,-4.36 0" />
<path
android:fillColor="#E53935"
android:pathData="M4,4.44v2.83c7.03,0 12.73,5.7 12.73,12.73h2.83c0,-8.59 -6.97,-15.56 -15.56,-15.56zM4,10.1v2.83c3.9,0 7.07,3.17 7.07,7.07h2.83c0,-5.47 -4.43,-9.9 -9.9,-9.9z" />
</vector>
\ No newline at end of file
......@@ -52,4 +52,8 @@
<string name="unable_to">Désolé, l\'application n\'a pas réussi à %s</string>
<string name="notification_channel_refresh_title">Événements de raffraîchissement</string>
<string name="notification_channel_refresh_description">Événements lors de la récupération de contenus de votre serveur FreshRSS</string>
<string name="notification_channel_errors_title">Erreurs</string>
<string name="notification_channel_errors_description">FreshRSS a rencontre une erreur dont vous devriez être
informé
</string>
</resources>
\ No newline at end of file
<resources>
<!-- Do not translate -->
<string name="app_name">FreshRSS</string>
<string name="app_name" translatable="false">FreshRSS</string>
<string-array name="connection_options">
<item>https://</item>
<item>http://</item>
......@@ -23,7 +23,9 @@
<string name="notification_refresh_failed_title">Refreshing failed</string>
<string name="notification_refresh_failed_description">FreshRSS failed to retrieve content from your FreshRSS server</string>
<string name="notification_channel_refresh_title">Refresh events</string>
<string name="notification_channel_refresh_description">Events occurring when trying to fetch content from your FreshRSS server</string>
<string name="notification_channel_refresh_description">FreshRSS is fetching content from your FreshRSS server</string>
<string name="notification_channel_errors_title">Errors</string>
<string name="notification_channel_errors_description">FreshRSS has met an error you should be notified about</string>
<!-- Strings related to subscriptions -->
<string name="subscription_title_label">Title</string>
<string name="rss_icon">RSS icon</string>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment