Skip to content

Commit b634b06

Browse files
committed
Attempt to improve notification handling on Android 10 (PP-3631).
1 parent 538f21e commit b634b06

5 files changed

Lines changed: 58 additions & 32 deletions

File tree

README-CHANGES.xml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,8 +1012,14 @@
10121012
</c:change>
10131013
</c:changes>
10141014
</c:release>
1015-
<c:release date="2026-02-03T12:33:36+00:00" is-open="true" ticket-system="org.lyrasis.jira" version="1.25.0">
1016-
<c:changes/>
1015+
<c:release date="2026-02-03T13:29:02+00:00" is-open="true" ticket-system="org.lyrasis.jira" version="1.25.0">
1016+
<c:changes>
1017+
<c:change date="2026-02-03T13:29:02+00:00" summary="Attempt to allow notifications on Android 10.">
1018+
<c:tickets>
1019+
<c:ticket id="PP-3631"/>
1020+
</c:tickets>
1021+
</c:change>
1022+
</c:changes>
10171023
</c:release>
10181024
</c:releases>
10191025
<c:ticket-systems>
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
package org.nypl.simplified.notifications
22

3-
class NotificationChannelInfo(val id: String, val name: String, val description: String)
3+
data class NotificationChannelInfo(
4+
val id: String,
5+
val name: String,
6+
val description: String
7+
)

palace-notifications/src/main/java/NotificationsService.kt

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package org.nypl.simplified.notifications
33
import android.app.NotificationChannel
44
import android.app.NotificationManager
55
import android.content.Context
6-
import android.os.Build
76
import org.nypl.simplified.profiles.api.ProfileNoneCurrentException
87
import org.nypl.simplified.profiles.controller.api.ProfilesControllerType
98
import org.slf4j.LoggerFactory
@@ -19,40 +18,47 @@ class NotificationsService(
1918
LoggerFactory.getLogger(NotificationsService::class.java)
2019

2120
init {
22-
createNotificationChannels()
21+
this.createNotificationChannels()
2322

2423
try {
2524
httpCalls.registerFCMTokenForProfileAccounts(profilesController.profileCurrent())
26-
} catch (exception: ProfileNoneCurrentException) {
27-
logger.error("No profile to register FCM token")
25+
} catch (_: ProfileNoneCurrentException) {
26+
this.logger.error("No profile to register FCM token")
2827
}
2928
}
3029

3130
private fun createNotificationChannels() {
32-
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
31+
this.logger.debug("Creating notification channels.")
32+
33+
val notificationManager =
34+
this.context.getSystemService(Context.NOTIFICATION_SERVICE)
35+
as? NotificationManager
36+
37+
if (notificationManager == null) {
38+
this.logger.warn("No system notification manager is available.")
3339
return
3440
}
3541

36-
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as?
37-
NotificationManager ?: return
38-
3942
// delete possible usages for the old notifications channel
40-
notificationManager.deleteNotificationChannel(notificationResources.notificationChannelNameOld)
41-
42-
// there's no harm on constantly creating notification channels
43-
notificationResources.notificationChannels.forEach { channel ->
44-
this.logger.debug("Creating notification channel: {}", channel)
45-
46-
notificationManager.createNotificationChannel(
47-
NotificationChannel(
48-
channel.id,
49-
channel.name,
50-
NotificationManager.IMPORTANCE_DEFAULT
51-
).apply {
52-
description = channel.description
53-
enableVibration(true)
54-
}
55-
)
43+
notificationManager.deleteNotificationChannel(this.notificationResources.notificationChannelNameOld)
44+
45+
for (channelDescription in this.notificationResources.notificationChannels) {
46+
this.logger.debug("Creating notification channel: {}", channelDescription)
47+
48+
try {
49+
val channelInfo =
50+
NotificationChannel(
51+
channelDescription.id,
52+
channelDescription.name,
53+
NotificationManager.IMPORTANCE_DEFAULT
54+
)
55+
56+
channelInfo.description = channelDescription.description
57+
channelInfo.enableVibration(true)
58+
notificationManager.createNotificationChannel(channelInfo)
59+
} catch (e: Throwable) {
60+
this.logger.debug("Failed to create notification channel {}: ", channelDescription.id, e)
61+
}
5662
}
5763
}
5864
}

palace-ui/src/main/java/org/nypl/simplified/ui/main/MainLogging.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ object MainLogging {
132132
logbackLevel("org.nypl.simplified.books.covers.BookCoverGenerator", Level.ERROR)
133133
logbackLevel("org.nypl.simplified.books.covers.BookCoverProvider", Level.ERROR)
134134
logbackLevel("org.nypl.simplified.files.FileLocking", Level.ERROR)
135-
logbackLevel("org.nypl.simplified.notifications.NotificationsService", Level.ERROR)
135+
logbackLevel("org.nypl.simplified.notifications.NotificationsService", Level.DEBUG)
136136
logbackLevel("org.sqlite.core.NativeDB", Level.ERROR)
137137
}
138138

palace-ui/src/main/java/org/nypl/simplified/ui/main/MainNotifications.kt

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package org.nypl.simplified.ui.main
33
import android.app.Activity
44
import android.content.Intent
55
import android.content.pm.PackageManager
6+
import android.os.Build
67
import android.provider.Settings
78
import androidx.core.content.ContextCompat
89

@@ -36,11 +37,20 @@ object MainNotifications {
3637
"android.permission.FOREGROUND_SERVICE",
3738
) == PackageManager.PERMISSION_GRANTED
3839

40+
/*
41+
* The FOREGROUND_SERVICE_MEDIA_PLAYBACK permission is required on API 34 and up.
42+
* If we're on an earlier version of Android, we just pretend that it's allowed.
43+
*/
44+
3945
val foregroundMediaOk =
40-
ContextCompat.checkSelfPermission(
41-
activity,
42-
"android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK",
43-
) == PackageManager.PERMISSION_GRANTED
46+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
47+
ContextCompat.checkSelfPermission(
48+
activity,
49+
"android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK",
50+
) == PackageManager.PERMISSION_GRANTED
51+
} else {
52+
true
53+
}
4454

4555
return notificationsOk && foregroundOk && foregroundMediaOk
4656
}

0 commit comments

Comments
 (0)