@@ -14,7 +14,9 @@ import android.content.pm.PackageManager
14
14
import android.graphics.BitmapFactory
15
15
import android.net.Uri
16
16
import android.os.Build
17
+ import android.os.TransactionTooLargeException
17
18
import android.provider.Browser.EXTRA_CREATE_NEW_TAB
19
+ import android.util.Log
18
20
import androidx.annotation.RequiresApi
19
21
import androidx.core.app.NotificationCompat
20
22
import androidx.core.app.NotificationCompat.GROUP_ALERT_SUMMARY
@@ -45,8 +47,10 @@ import org.kodein.di.android.closestDI
45
47
import org.kodein.di.instance
46
48
47
49
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"
50
54
51
55
suspend fun notify (
52
56
appContext : Context ,
@@ -68,18 +72,24 @@ suspend fun notify(
68
72
69
73
val nm: NotificationManagerCompat by di.instance()
70
74
75
+ // If too many it can cause a crash, so it's limited to 20
71
76
val feedItems = getItemsToNotify(di)
72
77
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
+ }
79
86
}
87
+ // Shown on API Level < 24
88
+ nm.notify(summaryNotificationId, inboxNotification(appContext, feedItems))
80
89
}
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)
83
93
}
84
94
}
85
95
@@ -119,7 +129,7 @@ private fun createNotificationChannel(context: Context) {
119
129
val notificationManager: NotificationManager =
120
130
context.getSystemService(NOTIFICATION_SERVICE ) as NotificationManager
121
131
122
- val channel = NotificationChannel (channelId , name, NotificationManager .IMPORTANCE_LOW )
132
+ val channel = NotificationChannel (CHANNEL_ID , name, NotificationManager .IMPORTANCE_LOW )
123
133
channel.description = description
124
134
125
135
notificationManager.createNotificationChannel(channel)
@@ -156,7 +166,7 @@ private suspend fun singleNotification(context: Context, item: FeedItemWithFeed)
156
166
157
167
builder.setContentText(text)
158
168
.setContentTitle(title)
159
- .setGroup(articleNotificationGroup )
169
+ .setGroup(ARTICLE_NOTIFICATION_GROUP )
160
170
.setGroupAlertBehavior(GROUP_ALERT_SUMMARY )
161
171
.setDeleteIntent(getPendingDeleteIntent(context, item))
162
172
.setNumber(1 )
@@ -313,7 +323,7 @@ private fun inboxNotification(context: Context, feedItems: List<FeedItemWithFeed
313
323
builder.setContentText(text)
314
324
.setContentTitle(title)
315
325
.setContentIntent(pendingIntent)
316
- .setGroup(articleNotificationGroup )
326
+ .setGroup(ARTICLE_NOTIFICATION_GROUP )
317
327
.setGroupAlertBehavior(GROUP_ALERT_SUMMARY )
318
328
.setGroupSummary(true )
319
329
.setDeleteIntent(getDeleteIntent(context, feedItems))
@@ -359,7 +369,7 @@ private fun getPendingDeleteIntent(context: Context, feedItem: FeedItemWithFeed)
359
369
private fun notificationBuilder (context : Context ): NotificationCompat .Builder {
360
370
val bm = BitmapFactory .decodeResource(context.resources, R .mipmap.ic_launcher)
361
371
362
- return NotificationCompat .Builder (context, channelId )
372
+ return NotificationCompat .Builder (context, CHANNEL_ID )
363
373
.setSmallIcon(R .drawable.ic_stat_f)
364
374
.setLargeIcon(bm)
365
375
.setAutoCancel(true )
0 commit comments