fix(android): guard foreground notify with runtime permission

pull/2/head
alimu 2 weeks ago
parent ef58ef4b80
commit f031a4c5d7

@ -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()

Loading…
Cancel
Save