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 chapters = it.second
val chapterNames = chapters.map { chapter -> chapter.name.chop(45) }.toSet()
NotificationReceiver.dismissNotification(this, manga.id.hashCode())
notifications.add(Pair(notification(Notifications.CHANNEL_NEW_CHAPTERS) {
setSmallIcon(R.drawable.ic_tachiyomi_icon)
try {
@ -481,10 +480,10 @@ class LibraryUpdateService(
)
addAction(R.drawable.ic_glasses_black_24dp, getString(R.string.action_mark_as_read),
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),
NotificationReceiver.openChapterPendingActivity(this@LibraryUpdateService,
manga, Notifications.GROUP_NEW_CHAPTERS))
manga, Notifications.ID_NEW_CHAPTERS))
setAutoCancel(true)
}, manga.id.hashCode()))
}
@ -494,21 +493,18 @@ class LibraryUpdateService(
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) {
setSmallIcon(R.drawable.ic_tachiyomi_icon)
setLargeIcon(notificationBitmap)
setContentTitle(getString(R.string.notification_new_chapters))
color = ContextCompat.getColor(applicationContext, R.color.colorAccentLight)
setContentText(getString(R.string.notification_new_chapters_text, updates.size))
priority = NotificationCompat.PRIORITY_HIGH
setGroup(Notifications.GROUP_NEW_CHAPTERS)
setGroupSummary(true)
setContentIntent(getNotificationIntent())
setAutoCancel(true)
})
}
notify(Notifications.ID_NEW_CHAPTERS, notification(Notifications.CHANNEL_NEW_CHAPTERS) {
setSmallIcon(R.drawable.ic_tachiyomi_icon)
setLargeIcon(notificationBitmap)
setContentTitle(getString(R.string.notification_new_chapters))
color = ContextCompat.getColor(applicationContext, R.color.colorAccentLight)
setContentText(getString(R.string.notification_new_chapters_text, updates.size))
priority = NotificationCompat.PRIORITY_HIGH
setGroup(Notifications.GROUP_NEW_CHAPTERS)
setGroupSummary(true)
setContentIntent(getNotificationIntent())
setAutoCancel(true)
})
}
}

View File

@ -73,7 +73,7 @@ class NotificationReceiver : BroadcastReceiver() {
ACTION_MARK_AS_READ -> {
val notificationId = intent.getIntExtra(EXTRA_NOTIFICATION_ID, -1)
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 mangaId = intent.getLongExtra(EXTRA_MANGA_ID, -1)
@ -304,16 +304,23 @@ class NotificationReceiver : BroadcastReceiver() {
* @param notificationId id of notification
* @return [PendingIntent]
*/
internal fun dismissNotification(context: Context, notificationId: Int, groupId: String?
= null) {
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)
internal fun dismissNotification(context: Context, notificationId: Int, groupId: Int? =
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)
}
/**
@ -375,7 +382,7 @@ class NotificationReceiver : BroadcastReceiver() {
* @param context context of application
* @param manga manga of chapter
*/
internal fun openChapterPendingActivity(context: Context, manga: Manga, groupId: String):
internal fun openChapterPendingActivity(context: Context, manga: Manga, groupId: Int):
PendingIntent {
val newIntent =
Intent(context, MainActivity::class.java).setAction(MainActivity.SHORTCUT_MANGA)
@ -395,7 +402,7 @@ class NotificationReceiver : BroadcastReceiver() {
* @param manga manga of chapter
*/
internal fun markAsReadPendingBroadcast(context: Context, manga: Manga, chapters:
Array<Chapter>, groupId: String):
Array<Chapter>, groupId: Int):
PendingIntent {
val newIntent = Intent(context, NotificationReceiver::class.java).apply {
action = ACTION_MARK_AS_READ

View File

@ -257,7 +257,7 @@ class MainActivity : BaseActivity() {
private fun handleIntentAction(intent: Intent): Boolean {
val notificationId = intent.getIntExtra("notificationId", -1)
if (notificationId > -1) NotificationReceiver.dismissNotification(
applicationContext, notificationId, intent.getStringExtra("groupId")
applicationContext, notificationId, intent.getIntExtra("groupId", 0)
)
when (intent.action) {
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 context = applicationContext ?: return
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.models.Chapter
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.preference.PreferencesHelper
import eu.kanade.tachiyomi.data.preference.getOrDefault
@ -152,7 +153,7 @@ class ReaderActivity : BaseRxActivity<ReaderPresenter>(),
finish()
return
}
NotificationReceiver.dismissNotification(this, manga.hashCode(), Notifications.ID_NEW_CHAPTERS)
if (chapter > -1) presenter.init(manga, chapter)
else presenter.init(manga, chapterUrl)
}