@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) package eu.friedcutlet.yokan.services.notifications import eu.friedcutlet.yokan.services.logging.AppLogger import eu.friedcutlet.yokan.domain.ProfileId import platform.UserNotifications.UNAuthorizationOptionAlert import platform.UserNotifications.UNAuthorizationOptionSound import platform.UserNotifications.UNMutableNotificationContent import platform.UserNotifications.UNNotificationRequest import platform.UserNotifications.UNUserNotificationCenter actual fun createPlatformNotificationService(profileId: ProfileId): NotificationService = IosNotificationService(profileId) private class IosNotificationService(profileId: ProfileId) : NotificationService { private val namespace = ProfileNotificationNamespace(profileId) override fun supported(): Boolean = true override fun requestPermission() { UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions( UNAuthorizationOptionAlert or UNAuthorizationOptionSound, ) { _, error -> error?.let { AppLogger.warn("Could not request notification permission: ${it.localizedDescription}") } } } override fun notify(payload: NotificationPayload) { val content = UNMutableNotificationContent().apply { setTitle(payload.title) setBody(payload.message) } val request = UNNotificationRequest.requestWithIdentifier( namespace.iosIdentifier(payload.channel, payload.tag), content, trigger = null, ) UNUserNotificationCenter.currentNotificationCenter().addNotificationRequest(request) { error -> error?.let { AppLogger.warn("Could not post notification: ${it.localizedDescription}") } } } override fun cancel(channel: NotificationChannel, tag: String?) { val identifiers = listOf(namespace.iosIdentifier(channel, tag)) val center = UNUserNotificationCenter.currentNotificationCenter() center.removeDeliveredNotificationsWithIdentifiers(identifiers) center.removePendingNotificationRequestsWithIdentifiers(identifiers) } }