Skip to content

Commit 4ed93ae

Browse files
gugaloAndrii-Horishnii-Glia
authored andcommitted
Fix IllegalStateException when starting NotificationRemovalService
We where not able to reproduce the issue which was reported by the client. However, theoretically Lifecycle.Event.ON_STOP might be called when app is in bacground causing the error 'IllegalStateException: Not allowed to start service app is in background' but in case of Lifecycle.Event.ON_PAUSE app can't be in the background state. MOB-4011
1 parent 59b3e7d commit 4ed93ae

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

widgetssdk/src/main/java/com/glia/widgets/core/notification/device/NotificationManager.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import com.glia.widgets.R
1010
import com.glia.widgets.core.notification.NotificationFactory
1111
import com.glia.widgets.core.notification.NotificationRemovalService
1212
import com.glia.widgets.core.notification.areNotificationsEnabledForChannel
13+
import com.glia.widgets.helper.Logger
14+
import com.glia.widgets.helper.TAG
1315

1416
internal class NotificationManager(private val applicationContext: Application) : INotificationManager {
1517
private val notificationManager: NotificationManager by lazy {
@@ -88,7 +90,12 @@ internal class NotificationManager(private val applicationContext: Application)
8890
override fun startNotificationRemovalService() {
8991
// If this service is not already running, it will be instantiated and started (creating a process for it if needed);
9092
// if it is running then it remains running.
91-
applicationContext.startService(Intent(applicationContext, NotificationRemovalService::class.java))
93+
try {
94+
applicationContext.startService(Intent(applicationContext, NotificationRemovalService::class.java))
95+
} catch (error: Throwable) {
96+
// Above code is known to throw an error if app is in background
97+
Logger.e(TAG, "Failed to launch 'NotificationRemovalService'", error)
98+
}
9299
}
93100

94101
override fun removeCallNotification() {

widgetssdk/src/main/java/com/glia/widgets/di/Dependencies.kt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -252,13 +252,17 @@ internal object Dependencies {
252252
chatBubbleController: ChatHeadContract.Controller
253253
) {
254254
lifecycleManager.addObserver { _, event: Lifecycle.Event ->
255-
if (event == Lifecycle.Event.ON_STOP) {
256-
chatBubbleController.onApplicationStop()
257-
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S && Glia.isInitialized()) {
258-
notificationManager.startNotificationRemovalService()
255+
when(event) {
256+
Lifecycle.Event.ON_PAUSE -> {
257+
// Moved to on pause due to "IllegalStateException: Not allowed to start service app is in background"
258+
// Related bug ticket MOB-4011
259+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S && Glia.isInitialized()) {
260+
notificationManager.startNotificationRemovalService()
261+
}
259262
}
260-
} else if (event == Lifecycle.Event.ON_DESTROY) {
261-
chatBubbleController.onDestroy()
263+
Lifecycle.Event.ON_STOP -> chatBubbleController.onApplicationStop()
264+
Lifecycle.Event.ON_DESTROY -> chatBubbleController.onDestroy()
265+
else -> { /* no-op */ }
262266
}
263267
}
264268
}

0 commit comments

Comments
 (0)