@@ -3,7 +3,6 @@ package org.nypl.simplified.notifications
33import android.app.NotificationChannel
44import android.app.NotificationManager
55import android.content.Context
6- import android.os.Build
76import org.nypl.simplified.profiles.api.ProfileNoneCurrentException
87import org.nypl.simplified.profiles.controller.api.ProfilesControllerType
98import 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}
0 commit comments