Fixes to new chapters notifcations

Dismissal of notifcations when reading a chapter from a manga
This commit is contained in:
Jay 2019-11-23 17:09:29 -08:00
parent 2bb960c24b
commit ffd1f950d8
5 changed files with 36 additions and 32 deletions

View File

@ -454,7 +454,6 @@ class LibraryUpdateService(
val manga = it.first val manga = it.first
val chapters = it.second val chapters = it.second
val chapterNames = chapters.map { chapter -> chapter.name.chop(45) }.toSet() val chapterNames = chapters.map { chapter -> chapter.name.chop(45) }.toSet()
NotificationReceiver.dismissNotification(this, manga.id.hashCode())
notifications.add(Pair(notification(Notifications.CHANNEL_NEW_CHAPTERS) { notifications.add(Pair(notification(Notifications.CHANNEL_NEW_CHAPTERS) {
setSmallIcon(R.drawable.ic_tachiyomi_icon) setSmallIcon(R.drawable.ic_tachiyomi_icon)
try { try {
@ -481,10 +480,10 @@ class LibraryUpdateService(
) )
addAction(R.drawable.ic_glasses_black_24dp, getString(R.string.action_mark_as_read), addAction(R.drawable.ic_glasses_black_24dp, getString(R.string.action_mark_as_read),
NotificationReceiver.markAsReadPendingBroadcast(this@LibraryUpdateService, NotificationReceiver.markAsReadPendingBroadcast(this@LibraryUpdateService,
manga, chapters, Notifications.GROUP_NEW_CHAPTERS)) manga, chapters, Notifications.ID_NEW_CHAPTERS))
addAction(R.drawable.ic_book_white_24dp, getString(R.string.action_view_chapters), addAction(R.drawable.ic_book_white_24dp, getString(R.string.action_view_chapters),
NotificationReceiver.openChapterPendingActivity(this@LibraryUpdateService, NotificationReceiver.openChapterPendingActivity(this@LibraryUpdateService,
manga, Notifications.GROUP_NEW_CHAPTERS)) manga, Notifications.ID_NEW_CHAPTERS))
setAutoCancel(true) setAutoCancel(true)
}, manga.id.hashCode())) }, manga.id.hashCode()))
} }
@ -494,8 +493,6 @@ class LibraryUpdateService(
notify(it.second, it.first) notify(it.second, it.first)
} }
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N || notificationManager
.activeNotifications.find { it.groupKey == Notifications.GROUP_NEW_CHAPTERS } == null) {
notify(Notifications.ID_NEW_CHAPTERS, notification(Notifications.CHANNEL_NEW_CHAPTERS) { notify(Notifications.ID_NEW_CHAPTERS, notification(Notifications.CHANNEL_NEW_CHAPTERS) {
setSmallIcon(R.drawable.ic_tachiyomi_icon) setSmallIcon(R.drawable.ic_tachiyomi_icon)
setLargeIcon(notificationBitmap) setLargeIcon(notificationBitmap)
@ -510,7 +507,6 @@ class LibraryUpdateService(
}) })
} }
} }
}
/** /**
* Cancels the progress notification. * Cancels the progress notification.

View File

@ -73,7 +73,7 @@ class NotificationReceiver : BroadcastReceiver() {
ACTION_MARK_AS_READ -> { ACTION_MARK_AS_READ -> {
val notificationId = intent.getIntExtra(EXTRA_NOTIFICATION_ID, -1) val notificationId = intent.getIntExtra(EXTRA_NOTIFICATION_ID, -1)
if (notificationId > -1) dismissNotification( if (notificationId > -1) dismissNotification(
context, notificationId, intent.getStringExtra(EXTRA_GROUP_ID) context, notificationId, intent.getIntExtra(EXTRA_GROUP_ID, 0)
) )
val urls = intent.getStringArrayExtra(EXTRA_CHAPTER_URL) ?: return val urls = intent.getStringArrayExtra(EXTRA_CHAPTER_URL) ?: return
val mangaId = intent.getLongExtra(EXTRA_MANGA_ID, -1) val mangaId = intent.getLongExtra(EXTRA_MANGA_ID, -1)
@ -304,16 +304,23 @@ class NotificationReceiver : BroadcastReceiver() {
* @param notificationId id of notification * @param notificationId id of notification
* @return [PendingIntent] * @return [PendingIntent]
*/ */
internal fun dismissNotification(context: Context, notificationId: Int, groupId: String? internal fun dismissNotification(context: Context, notificationId: Int, groupId: Int? =
= null) { null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
val groupKey = context.notificationManager.activeNotifications.find {
it.id == notificationId
}?.groupKey
if (groupId != null && groupId != 0 && groupKey != null && groupKey.isNotEmpty()) {
val notifications = context.notificationManager.activeNotifications.filter {
it.groupKey == groupKey
}
if (notifications.size == 2) {
context.notificationManager.cancel(groupId)
return
}
}
}
context.notificationManager.cancel(notificationId) context.notificationManager.cancel(notificationId)
if (groupId != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
val notifications = context.notificationManager.activeNotifications.filter { it
.groupKey.contains(groupId) }
if (notifications.size == 1) {
context.notificationManager.cancel(notifications.first().id)
}
}
} }
/** /**
@ -375,7 +382,7 @@ class NotificationReceiver : BroadcastReceiver() {
* @param context context of application * @param context context of application
* @param manga manga of chapter * @param manga manga of chapter
*/ */
internal fun openChapterPendingActivity(context: Context, manga: Manga, groupId: String): internal fun openChapterPendingActivity(context: Context, manga: Manga, groupId: Int):
PendingIntent { PendingIntent {
val newIntent = val newIntent =
Intent(context, MainActivity::class.java).setAction(MainActivity.SHORTCUT_MANGA) Intent(context, MainActivity::class.java).setAction(MainActivity.SHORTCUT_MANGA)
@ -395,7 +402,7 @@ class NotificationReceiver : BroadcastReceiver() {
* @param manga manga of chapter * @param manga manga of chapter
*/ */
internal fun markAsReadPendingBroadcast(context: Context, manga: Manga, chapters: internal fun markAsReadPendingBroadcast(context: Context, manga: Manga, chapters:
Array<Chapter>, groupId: String): Array<Chapter>, groupId: Int):
PendingIntent { PendingIntent {
val newIntent = Intent(context, NotificationReceiver::class.java).apply { val newIntent = Intent(context, NotificationReceiver::class.java).apply {
action = ACTION_MARK_AS_READ action = ACTION_MARK_AS_READ

View File

@ -257,7 +257,7 @@ class MainActivity : BaseActivity() {
private fun handleIntentAction(intent: Intent): Boolean { private fun handleIntentAction(intent: Intent): Boolean {
val notificationId = intent.getIntExtra("notificationId", -1) val notificationId = intent.getIntExtra("notificationId", -1)
if (notificationId > -1) NotificationReceiver.dismissNotification( if (notificationId > -1) NotificationReceiver.dismissNotification(
applicationContext, notificationId, intent.getStringExtra("groupId") applicationContext, notificationId, intent.getIntExtra("groupId", 0)
) )
when (intent.action) { when (intent.action) {
SHORTCUT_LIBRARY -> setSelectedDrawerItem(R.id.nav_drawer_library) SHORTCUT_LIBRARY -> setSelectedDrawerItem(R.id.nav_drawer_library)

View File

@ -72,7 +72,7 @@ class MangaController : RxController, TabbedController {
val notificationId = bundle.getInt("notificationId", -1) val notificationId = bundle.getInt("notificationId", -1)
val context = applicationContext ?: return val context = applicationContext ?: return
if (notificationId > -1) NotificationReceiver.dismissNotification( if (notificationId > -1) NotificationReceiver.dismissNotification(
context, notificationId, bundle.getString("groupId", "") context, notificationId, bundle.getInt("groupId", 0)
) )
} }

View File

@ -22,6 +22,7 @@ import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.database.DatabaseHelper import eu.kanade.tachiyomi.data.database.DatabaseHelper
import eu.kanade.tachiyomi.data.database.models.Chapter import eu.kanade.tachiyomi.data.database.models.Chapter
import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.data.notification.NotificationReceiver
import eu.kanade.tachiyomi.data.notification.Notifications import eu.kanade.tachiyomi.data.notification.Notifications
import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.data.preference.getOrDefault import eu.kanade.tachiyomi.data.preference.getOrDefault
@ -152,7 +153,7 @@ class ReaderActivity : BaseRxActivity<ReaderPresenter>(),
finish() finish()
return return
} }
NotificationReceiver.dismissNotification(this, manga.hashCode(), Notifications.ID_NEW_CHAPTERS)
if (chapter > -1) presenter.init(manga, chapter) if (chapter > -1) presenter.init(manga, chapter)
else presenter.init(manga, chapterUrl) else presenter.init(manga, chapterUrl)
} }