Skip to content

Commit ccabb49

Browse files
committed
Fixed crash if trying to notify for too many items
1 parent cda338b commit ccabb49

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

app/src/main/java/com/nononsenseapps/feeder/db/room/FeedItemDao.kt

+2
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,8 @@ interface FeedItemDao {
250250
LEFT JOIN feeds ON feed_items.feed_id = feeds.id
251251
WHERE feed_id IN (:feedIds) AND notified IS 0 AND read_time is null
252252
AND NOT EXISTS (SELECT 1 FROM blocklist WHERE lower(feed_items.plain_title) GLOB blocklist.glob_pattern)
253+
ORDER BY $feedItemsListOrderByDesc
254+
LIMIT 20
253255
""",
254256
)
255257
suspend fun loadItemsToNotify(feedIds: List<Long>): List<FeedItemWithFeed>

app/src/main/java/com/nononsenseapps/feeder/model/RssNotifications.kt

+24-14
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ import android.content.pm.PackageManager
1414
import android.graphics.BitmapFactory
1515
import android.net.Uri
1616
import android.os.Build
17+
import android.os.TransactionTooLargeException
1718
import android.provider.Browser.EXTRA_CREATE_NEW_TAB
19+
import android.util.Log
1820
import androidx.annotation.RequiresApi
1921
import androidx.core.app.NotificationCompat
2022
import androidx.core.app.NotificationCompat.GROUP_ALERT_SUMMARY
@@ -45,8 +47,10 @@ import org.kodein.di.android.closestDI
4547
import org.kodein.di.instance
4648

4749
const val summaryNotificationId = 2_147_483_646
48-
private const val channelId = "feederNotifications"
49-
private const val articleNotificationGroup = "com.nononsenseapps.feeder.ARTICLE"
50+
private const val CHANNEL_ID = "feederNotifications"
51+
private const val ARTICLE_NOTIFICATION_GROUP = "com.nononsenseapps.feeder.ARTICLE"
52+
53+
private const val LOG_TAG = "FEEDER_NOTIFY"
5054

5155
suspend fun notify(
5256
appContext: Context,
@@ -68,18 +72,24 @@ suspend fun notify(
6872

6973
val nm: NotificationManagerCompat by di.instance()
7074

75+
// If too many it can cause a crash, so it's limited to 20
7176
val feedItems = getItemsToNotify(di)
7277

73-
if (feedItems.isNotEmpty()) {
74-
if (!updateSummaryOnly) {
75-
feedItems.map {
76-
it.id.toInt() to singleNotification(appContext, it)
77-
}.forEach { (id, notification) ->
78-
nm.notify(id, notification)
78+
try {
79+
if (feedItems.isNotEmpty()) {
80+
if (!updateSummaryOnly) {
81+
feedItems.map {
82+
it.id.toInt() to singleNotification(appContext, it)
83+
}.forEach { (id, notification) ->
84+
nm.notify(id, notification)
85+
}
7986
}
87+
// Shown on API Level < 24
88+
nm.notify(summaryNotificationId, inboxNotification(appContext, feedItems))
8089
}
81-
// Shown on API Level < 24
82-
nm.notify(summaryNotificationId, inboxNotification(appContext, feedItems))
90+
} catch (e: TransactionTooLargeException) {
91+
// This can happen if there are too many notifications
92+
Log.e(LOG_TAG, "Too many notifications", e)
8393
}
8494
}
8595

@@ -119,7 +129,7 @@ private fun createNotificationChannel(context: Context) {
119129
val notificationManager: NotificationManager =
120130
context.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
121131

122-
val channel = NotificationChannel(channelId, name, NotificationManager.IMPORTANCE_LOW)
132+
val channel = NotificationChannel(CHANNEL_ID, name, NotificationManager.IMPORTANCE_LOW)
123133
channel.description = description
124134

125135
notificationManager.createNotificationChannel(channel)
@@ -156,7 +166,7 @@ private suspend fun singleNotification(context: Context, item: FeedItemWithFeed)
156166

157167
builder.setContentText(text)
158168
.setContentTitle(title)
159-
.setGroup(articleNotificationGroup)
169+
.setGroup(ARTICLE_NOTIFICATION_GROUP)
160170
.setGroupAlertBehavior(GROUP_ALERT_SUMMARY)
161171
.setDeleteIntent(getPendingDeleteIntent(context, item))
162172
.setNumber(1)
@@ -313,7 +323,7 @@ private fun inboxNotification(context: Context, feedItems: List<FeedItemWithFeed
313323
builder.setContentText(text)
314324
.setContentTitle(title)
315325
.setContentIntent(pendingIntent)
316-
.setGroup(articleNotificationGroup)
326+
.setGroup(ARTICLE_NOTIFICATION_GROUP)
317327
.setGroupAlertBehavior(GROUP_ALERT_SUMMARY)
318328
.setGroupSummary(true)
319329
.setDeleteIntent(getDeleteIntent(context, feedItems))
@@ -359,7 +369,7 @@ private fun getPendingDeleteIntent(context: Context, feedItem: FeedItemWithFeed)
359369
private fun notificationBuilder(context: Context): NotificationCompat.Builder {
360370
val bm = BitmapFactory.decodeResource(context.resources, R.mipmap.ic_launcher)
361371

362-
return NotificationCompat.Builder(context, channelId)
372+
return NotificationCompat.Builder(context, CHANNEL_ID)
363373
.setSmallIcon(R.drawable.ic_stat_f)
364374
.setLargeIcon(bm)
365375
.setAutoCancel(true)

0 commit comments

Comments
 (0)