diff --git a/android-client/app/src/main/java/com/onlinemsg/client/service/ChatForegroundService.kt b/android-client/app/src/main/java/com/onlinemsg/client/service/ChatForegroundService.kt index 0ebaa12..afb0369 100644 --- a/android-client/app/src/main/java/com/onlinemsg/client/service/ChatForegroundService.kt +++ b/android-client/app/src/main/java/com/onlinemsg/client/service/ChatForegroundService.kt @@ -1,5 +1,6 @@ package com.onlinemsg.client.service +import android.Manifest import android.app.Notification import android.app.NotificationChannel import android.app.NotificationManager @@ -7,6 +8,7 @@ import android.app.PendingIntent import android.app.Service import android.content.Context import android.content.Intent +import android.content.pm.PackageManager import android.os.Build import android.os.IBinder import androidx.core.app.NotificationCompat @@ -69,10 +71,18 @@ class ChatForegroundService : Service() { .map { it.status to it.statusHint } .distinctUntilChanged() .collect { (status, hint) -> - NotificationManagerCompat.from(this@ChatForegroundService).notify( - FOREGROUND_NOTIFICATION_ID, - buildForegroundNotification(status, hint) - ) + val canNotify = Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU || + checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED + if (canNotify) { + try { + NotificationManagerCompat.from(this@ChatForegroundService).notify( + FOREGROUND_NOTIFICATION_ID, + buildForegroundNotification(status, hint) + ) + } catch (_: SecurityException) { + // Permission may be revoked while service is running. + } + } if (status == ConnectionStatus.IDLE && !ChatSessionManager.shouldForegroundServiceRun()) { stopForeground(STOP_FOREGROUND_REMOVE) stopSelf()