https://kotlinlang.org logo
#ios
Title
# ios
m

Maximilian Pröll

10/19/2023, 11:39 AM
Hi! Has anyone tried creating an iOS notification from the shared Kotlin code? in Swift, we would need to create the notification request like so:
Copy code
// Create the request
let uuidString = UUID().uuidString
let request = UNNotificationRequest(identifier: uuidString, 
            content: content, trigger: trigger)
However, in Kotlin, there is no constructor with these parameters. We only have:
Copy code
public open expect class UNNotificationRequest : platform.darwin.NSObject, platform.Foundation.NSCopyingProtocol, platform.Foundation.NSSecureCodingProtocol {
   @kotlin.commonizer.ObjCCallable public constructor(coder: platform.Foundation.NSCoder) { /* compiled code */ }
   @kotlin.commonizer.ObjCCallable public constructor() { /* compiled code */ }
   ...
}
Also, the fields are val in the Kotlin platform file, so they cannot be reassigned:
Copy code
public expect final val content: platform.UserNotifications.UNNotificationContent /* compiled code */
public expect final val identifier: kotlin.String /* compiled code */
public expect final val trigger: platform.UserNotifications.UNNotificationTrigger? /* compiled code */
So how would you implement that in the shared module? I somehow need to do it that way, since I use compose multiplatform. So there is no swift UI from which the notification would be triggered.
d

Daniel Seither

10/19/2023, 12:21 PM
Since Kotlin is using Objective-C interfaces, take a look at the Objective-C variant of the UNNotificationRequest docs instead of the Swift version. There, you can see that in Objective-C and therefore Kotlin there’s no constructor for you to use but a static method
requestWithIdentifier:content:trigger:
that you can call. I’ve not tried this from Kotlin though.
👍 1
12 Views