Yokan / rebuild
Course 1 8624561

NotificationService.ios.kt

composeApp/src/iosMain/kotlin/eu/friedcutlet/yokan/services/notifications/NotificationService.ios.kt

implementation48 lines
1@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class)23package eu.friedcutlet.yokan.services.notifications45import eu.friedcutlet.yokan.services.logging.AppLogger6import eu.friedcutlet.yokan.domain.ProfileId7import platform.UserNotifications.UNAuthorizationOptionAlert8import platform.UserNotifications.UNAuthorizationOptionSound9import platform.UserNotifications.UNMutableNotificationContent10import platform.UserNotifications.UNNotificationRequest11import platform.UserNotifications.UNUserNotificationCenter1213actual fun createPlatformNotificationService(profileId: ProfileId): NotificationService = IosNotificationService(profileId)1415private class IosNotificationService(profileId: ProfileId) : NotificationService {16    private val namespace = ProfileNotificationNamespace(profileId)17    override fun supported(): Boolean = true18    override fun requestPermission() {19        UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions(20            UNAuthorizationOptionAlert or UNAuthorizationOptionSound,21        ) { _, error ->22            error?.let { AppLogger.warn("Could not request notification permission: ${it.localizedDescription}") }23        }24    }2526    override fun notify(payload: NotificationPayload) {27        val content = UNMutableNotificationContent().apply {28            setTitle(payload.title)29            setBody(payload.message)30        }31        val request = UNNotificationRequest.requestWithIdentifier(32            namespace.iosIdentifier(payload.channel, payload.tag),33            content,34            trigger = null,35        )36        UNUserNotificationCenter.currentNotificationCenter().addNotificationRequest(request) { error ->37            error?.let { AppLogger.warn("Could not post notification: ${it.localizedDescription}") }38        }39    }4041    override fun cancel(channel: NotificationChannel, tag: String?) {42        val identifiers = listOf(namespace.iosIdentifier(channel, tag))43        val center = UNUserNotificationCenter.currentNotificationCenter()44        center.removeDeliveredNotificationsWithIdentifiers(identifiers)45        center.removePendingNotificationRequestsWithIdentifiers(identifiers)46    }4748}49

Search chapters, reference and source filenames.