This commit is contained in:
Amine 2018-11-06 20:29:00 +01:00
parent f6ab909f8b
commit 1e02ad2041
5 changed files with 36 additions and 2 deletions

View File

@ -68,11 +68,18 @@ class MyApp : MultiDexApplication() {
private fun handleNotificationChannels() { private fun handleNotificationChannels() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
val name = getString(R.string.notification_channel_sync) val name = getString(R.string.notification_channel_sync)
val importance = NotificationManager.IMPORTANCE_LOW val importance = NotificationManager.IMPORTANCE_LOW
val mChannel = NotificationChannel(Config.syncChannelId, name, importance) val mChannel = NotificationChannel(Config.syncChannelId, name, importance)
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
val newItemsChannelname = getString(R.string.new_items_channel_sync)
val newItemsChannelimportance = NotificationManager.IMPORTANCE_DEFAULT
val newItemsChannelmChannel = NotificationChannel(Config.newItemsChannelId, newItemsChannelname, newItemsChannelimportance)
notificationManager.createNotificationChannel(mChannel) notificationManager.createNotificationChannel(mChannel)
notificationManager.createNotificationChannel(newItemsChannelmChannel)
} }
} }

View File

@ -2,9 +2,9 @@ package apps.amine.bou.readerforselfoss.background
import android.app.NotificationManager import android.app.NotificationManager
import android.content.Context import android.content.Context
import android.os.Handler
import android.preference.PreferenceManager import android.preference.PreferenceManager
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationCompat.PRIORITY_DEFAULT
import androidx.core.app.NotificationCompat.PRIORITY_LOW import androidx.core.app.NotificationCompat.PRIORITY_LOW
import androidx.room.Room import androidx.room.Room
import androidx.work.Worker import androidx.work.Worker
@ -51,6 +51,7 @@ class LoadingWorker(val context: Context, params: WorkerParameters) : Worker(con
this.context.getSharedPreferences(Config.settingsName, Context.MODE_PRIVATE) this.context.getSharedPreferences(Config.settingsName, Context.MODE_PRIVATE)
val sharedPref = PreferenceManager.getDefaultSharedPreferences(this.context) val sharedPref = PreferenceManager.getDefaultSharedPreferences(this.context)
val shouldLogEverything = sharedPref.getBoolean("should_log_everything", false) val shouldLogEverything = sharedPref.getBoolean("should_log_everything", false)
val notifyNewItems = sharedPref.getBoolean("notify_new_items", false)
db = Room.databaseBuilder( db = Room.databaseBuilder(
applicationContext, applicationContext,
@ -81,6 +82,20 @@ class LoadingWorker(val context: Context, params: WorkerParameters) : Worker(con
db.itemsDao().deleteAllItems() db.itemsDao().deleteAllItems()
db.itemsDao() db.itemsDao()
.insertAllItems(*(apiItems.map { it.toEntity() }).toTypedArray()) .insertAllItems(*(apiItems.map { it.toEntity() }).toTypedArray())
val newSize = apiItems.filter { it.unread }.size
if (notifyNewItems && newSize > 0) {
val newItemsNotification = NotificationCompat.Builder(applicationContext, Config.newItemsChannelId)
.setContentTitle(context.getString(R.string.new_items_notification_title))
.setContentText(context.getString(R.string.new_items_notification_text, newSize))
.setPriority(PRIORITY_DEFAULT)
.setChannelId(Config.newItemsChannelId)
.setSmallIcon(R.drawable.ic_fiber_new_black_24dp)
Timer("", false).schedule(4000) {
notificationManager.notify(2, newItemsNotification.build())
}
}
} }
Timer("", false).schedule(4000) { Timer("", false).schedule(4000) {
notificationManager.cancel(1) notificationManager.cancel(1)

View File

@ -38,6 +38,8 @@ class Config(c: Context) {
const val syncChannelId = "sync-channel-id" const val syncChannelId = "sync-channel-id"
const val newItemsChannelId = "new-items-channel-id"
fun logoutAndRedirect( fun logoutAndRedirect(
c: Context, c: Context,
callingActivity: Activity, callingActivity: Activity,

View File

@ -161,4 +161,8 @@
<string name="loading_notification_title">Loading ...</string> <string name="loading_notification_title">Loading ...</string>
<string name="loading_notification_text">Selfoss is syncing your articles</string> <string name="loading_notification_text">Selfoss is syncing your articles</string>
<string name="notification_channel_sync">Sync notification</string> <string name="notification_channel_sync">Sync notification</string>
<string name="new_items_channel_sync">New items notification</string>
<string name="new_items_notification_title">New items !</string>
<string name="new_items_notification_text">%1$d new items loaded.</string>
<string name="pref_switch_notify_new_items">Notify on new items synced.</string>
</resources> </resources>

View File

@ -28,4 +28,10 @@
android:key="refresh_when_charging" android:key="refresh_when_charging"
android:dependency="periodic_refresh" android:dependency="periodic_refresh"
android:title="@string/pref_switch_refresh_when_charging" /> android:title="@string/pref_switch_refresh_when_charging" />
<SwitchPreference
android:defaultValue="false"
android:key="notify_new_items"
android:dependency="periodic_refresh"
android:title="@string/pref_switch_notify_new_items" />
</PreferenceScreen> </PreferenceScreen>