mirzemehdi / KMPNotifier

Kotlin Multiplatform Push Notification Library targetting android, iOS, Desktop and Web (JS and Wasm)
http://mirzemehdi.com/KMPNotifier/
Apache License 2.0
340 stars 19 forks source link

Can't access NotifierManager in iOS in Compose Multiplatform #68

Closed Soabbar closed 1 month ago

Soabbar commented 2 months ago

Hi @mirzemehdi KMPNotifier and Kotlin version: kmpnotifier version: 1.2.1, kotlin version: 2.0.0

Describe the bug Setting up the library in Android seems to be working fine but it is not on iOS.

error: cannot find 'NotifierManager' in scope
      NotifierManager.initialize(configuration: NotificationPlatformConfigurationIos(showPushNotification: true))

error: cannot find 'NotificationPlatformConfigurationIos' in scope
      NotifierManager.initialize(configuration: NotificationPlatformConfigurationIos(showPushNotification: true))

The following build commands failed:
    SwiftCompile normal arm64 Compiling\ iOSApp.swift

In which platform bug occurs? ios

To Reproduce I followed the README but I am a bit confused about the Import shared I am using the Compose Multiplatform from Jetbrains, is that compatible?

listOf(
        iosX64(),
        iosArm64(),
        iosSimulatorArm64()
    ).forEach { iosTarget ->

        iosTarget.binaries.framework {
            export("io.github.mirzemehdi:kmpnotifier:1.2.1")
            baseName = "ComposeApp"
            isStatic = true
        }
    }

commonMain.dependencies {
            api("io.github.mirzemehdi:kmpnotifier:1.2.1")
}
import SwiftUI
import FirebaseCore
import FirebaseMessaging

class AppDelegate: NSObject, UIApplicationDelegate {

  func application(_ application: UIApplication,
                   didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {

      FirebaseApp.configure() //important

      //By default showPushNotification value is true.
      //When set showPushNotification to false foreground push  notification will not be shown.
      //You can still get notification content using #onPushNotification listener method.

      NotifierManager.initialize(configuration: NotificationPlatformConfigurationIos(showPushNotification: true))

    return true
  }

  func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        Messaging.messaging().apnsToken = deviceToken
  }

}

@main
struct iOSApp: App {
    @UIApplicationDelegateAdaptor(AppDelegate.self) var delegate

    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}
mirzemehdi commented 2 months ago

@Soabbar you missed import ComposeApp in ios part instead of import shared. This name comes from baseName that is defined in gradle file:


iosTarget.binaries.framework {
    export("io.github.mirzemehdi:kmpnotifier:1.2.1")
    baseName = "ComposeApp" <--- this one
    isStatic = true
}
mubarakaali commented 2 months ago

Hi @mirzemehdi i am facing the above same issue can you please guide what is wrong as it is working fine on android side but kmpnotifier files are not importing in ios side

cocoapods { ios.deploymentTarget = "14.1" version = "1.0.0" summary = "Shared module of CmpDemo Trucker" homepage = "https://github.com/mubarakaali/CMPDemoWithCleanArchitecure/" framework { baseName = "ComposeApp" isStatic = true binaryOption("bundleId", "com.truckitin.trucker.composeApp") export(libs.kmpNotifier) } }

w: Interop library /Users/user/.gradle/caches/modules-2/files-2.1/io.github.mirzemehdi/kmpnotifier-iossimulatorarm64/1.2.1/27a61887b5ed9a8ca27ff4e4dd1738d85bad22f8/kmpnotifier-cinterop-FirebaseMessaging can't be exported with -Xexport-library

Screenshot 2024-09-20 at 10 41 04 AM
andiosdev commented 1 month ago

Receiving the same error but only during Archive in XCode: /.gradle/caches/modules-2/files-2.1/io.github.mirzemehdi/kmpnotifier-iosarm64/1.2.1/c5fcf18c756435afc89eba49421b9ee1d3468865/kmpnotifier-cinterop-FirebaseMessaging can't be exported with -Xexport-library

Note: No problem running iOS app on either Android Studio or XCode.

mirzemehdi commented 1 month ago

@mubarakaali

ios setup should be like this:

https://github.com/mirzemehdi/KMPNotifier?tab=readme-ov-file#ios-setup


import SwiftUI
import shared
import FirebaseCore
import FirebaseMessaging

class AppDelegate: NSObject, UIApplicationDelegate {

  func application(_ application: UIApplication,
                   didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {

      FirebaseApp.configure() //important

      //By default showPushNotification value is true.
      //When set showPushNotification to false foreground push  notification will not be shown.
      //You can still get notification content using #onPushNotification listener method.
      NotifierManager.shared.initialize(configuration: NotificationPlatformConfigurationIos(
            showPushNotification: true,
            askNotificationPermissionOnStart: true)
      )

    return true
  }

  func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        Messaging.messaging().apnsToken = deviceToken
  }

}

@main
struct iOSApp: App {

    @UIApplicationDelegateAdaptor(AppDelegate.self) var delegate

    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}
mirzemehdi commented 1 month ago

@andiosdev can you please share more info from your xcode/ios configuration. Can't reproduce it. Maybe just try invalidate cache and restart.

andiosdev commented 1 month ago

@mirzemehdi this is what my Build iosApp - Log

Screenshot 2024-09-23 at 7 15 14 AM
mirzemehdi commented 1 month ago

@andiosdev error is related to heap space. in gradle properties search for jvmargs and set heap space value 4GB as below:

org.gradle.jvmargs=-Xmx4G
kotlin.daemon.jvmargs=-Xmx4G
andiosdev commented 1 month ago

org.gradle.jvmargs=-Xmx4G kotlin.daemon.jvmargs=-Xmx4G

Thank you @mirzemehdi . This fixes the issue.

Vkalns commented 1 month ago

@Soabbar you missed import ComposeApp in ios part instead of import shared. This name comes from baseName that is defined in gradle file:

iosTarget.binaries.framework {
    export("io.github.mirzemehdi:kmpnotifier:1.2.1")
    baseName = "ComposeApp" <--- this one
    isStatic = true
}

@mirzemehdi Perhaps setup guide should be updated to clarify this? I am not a iOS dev, and I wasted a few hours trying to solve this.

mirzemehdi commented 1 month ago

@Vkalns It is actually common Kotlin Multiplatform knowledge already. But will add some note as well then since it can cause some confusion. Thanks for mentioning