SyncNotificationPermission.kt

1.3 KB · raw

1package com.peachpasswords.sync23import android.Manifest4import android.app.NotificationManager5import android.content.Context6import android.content.pm.PackageManager7import android.os.Build8import androidx.core.app.NotificationManagerCompat9import androidx.core.content.ContextCompat1011/**12 * A foreground service can keep running after Android notification permission13 * is denied, but its status notification disappears from the drawer. Treat14 * that as unavailable so the UI never calls background sync visibly "On".15 */16internal fun canShowBackgroundSyncNotification(context: Context): Boolean {17    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU &&18        ContextCompat.checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS) !=19        PackageManager.PERMISSION_GRANTED20    ) {21        return false22    }23    if (!NotificationManagerCompat.from(context).areNotificationsEnabled()) return false24    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {25        val manager =26            context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager27        val channel = manager.getNotificationChannel(SYNC_NOTIFICATION_CHANNEL_ID)28        if (channel?.importance == NotificationManager.IMPORTANCE_NONE) return false29    }30    return true31}