Yokan / rebuild
Course 1 8624561

NotificationService.android.kt

composeApp/src/androidMain/kotlin/eu/friedcutlet/yokan/services/notifications/NotificationService.android.kt

implementation75 lines
1package eu.friedcutlet.yokan.services.notifications23import eu.friedcutlet.yokan.AndroidAppContext4import eu.friedcutlet.yokan.AndroidForegroundActivity5import android.Manifest6import android.app.Notification7import android.app.NotificationChannel as AndroidChannel8import android.app.NotificationManager9import android.content.Context10import android.content.pm.PackageManager11import android.os.Build12import eu.friedcutlet.yokan.domain.ProfileId1314actual fun createPlatformNotificationService(profileId: ProfileId): NotificationService = AndroidNotificationService(profileId)1516private val ChannelDefinitions = mapOf(17    NotificationChannel.Downloads to ChannelInfo("yokan_downloads", "Downloads"),18    NotificationChannel.LibraryUpdates to ChannelInfo("yokan_library_updates", "Library updates"),19    NotificationChannel.Backup to ChannelInfo("yokan_backup", "Backup"),20    NotificationChannel.Sync to ChannelInfo("yokan_sync", "Sync"),21)2223private data class ChannelInfo(val id: String, val label: String)2425/** The Android id of [channel], created on first use; the download foreground service posts on it too. */26internal fun NotificationManager.ensureChannel(channel: NotificationChannel): String? {27    val info = ChannelDefinitions[channel] ?: return null28    if (getNotificationChannel(info.id) == null) {29        createNotificationChannel(AndroidChannel(info.id, info.label, NotificationManager.IMPORTANCE_LOW))30    }31    return info.id32}3334private class AndroidNotificationService(profileId: ProfileId) : NotificationService {35    private val namespace = ProfileNotificationNamespace(profileId)36    override fun supported(): Boolean = AndroidAppContext.appContext != null37    override fun requestPermission() {38        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) return39        val activity = AndroidForegroundActivity.activity ?: return40        if (activity.checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {41            activity.requestPermissions(arrayOf(Manifest.permission.POST_NOTIFICATIONS), 1001)42        }43    }4445    override fun notify(payload: NotificationPayload) {46        val context = AndroidAppContext.appContext ?: return47        if (!hasNotificationPermission(context)) return48        val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as? NotificationManager ?: return49        val channelId = manager.ensureChannel(payload.channel) ?: return50        val builder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {51            Notification.Builder(context, channelId)52        } else {53            @Suppress("DEPRECATION") Notification.Builder(context)54        }55        builder56            .setContentTitle(payload.title)57            .setContentText(payload.message)58            .setSmallIcon(context.applicationInfo.icon)59            .setAutoCancel(true)60        runCatching {61            manager.notify(namespace.androidTag(payload.channel, payload.tag), namespace.androidId(channelId, payload.tag), builder.build())62        }63    }6465    override fun cancel(channel: NotificationChannel, tag: String?) {66        val context = AndroidAppContext.appContext ?: return67        val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as? NotificationManager ?: return68        val info = ChannelDefinitions[channel] ?: return69        manager.cancel(namespace.androidTag(channel, tag), namespace.androidId(info.id, tag))70    }7172    private fun hasNotificationPermission(context: Context): Boolean =73        Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU ||74            context.checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED75}76

Search chapters, reference and source filenames.