Hi everyone. I need to check app notifications set...
# kotlin-native
n
Hi everyone. I need to check app notifications settings on the device, and in order to do this, I use the following snippet for the iOS target :
Copy code
actual suspend fun areNotificationsSystemEnabled() : Boolean = suspendCoroutine { continuation ->
    val settingsCompletionHandler = { settings: UNNotificationSettings? ->
        val enabled = settings?.authorizationStatus == UNAuthorizationStatusAuthorized
        continuation.resume(enabled)
    }.freeze()

    val notificationCenter = UNUserNotificationCenter.currentNotificationCenter()
    notificationCenter.getNotificationSettingsWithCompletionHandler(settingsCompletionHandler)
}
My problem here is that the “continuation” used inside the completion handler is frozen, so it crashes on the
continuation.resume(enabled)
:
Copy code
Uncaught Kotlin exception: kotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen kotlin.coroutines.SafeContinuation@80487928
[...]
    at 7   KMM                                 0x00000001071d43e0 kfun:kotlin.coroutines.SafeContinuation#resumeWith(kotlin.Result<1:0>)
I think my issue is more or less the same than this one : https://youtrack.jetbrains.com/issue/KT-43566 I don’t know if I should ask this on #coroutines but if anyone already hit this issue, I’ll be glad to hear from you 😄 Thanks !
r
I've found that using
CompletableDeferred
instead of
suspendCoroutine
tends to work better if you're running into freezing issues
🙌 1
n
Thank you @russhwolf ! This was definitely the way to go
Copy code
actual suspend fun areNotificationsSystemEnabled() : Boolean {
    val deferred = CompletableDeferred<Boolean>()

    val settingsCompletionHandler = { settings: UNNotificationSettings? ->
        val enabled = settings?.authorizationStatus == UNAuthorizationStatusAuthorized
        deferred.complete(enabled)

        // returns Unit to avoid error "the feature "Unit conversion" is disabled"
        Unit
    }.freeze()

    val notificationCenter = UNUserNotificationCenter.currentNotificationCenter()
    notificationCenter.getNotificationSettingsWithCompletionHandler(settingsCompletionHandler)

    return deferred.await()
}