OneSignal / OneSignal-Flutter-SDK

OneSignal is a free push notification service for mobile apps. This plugin makes it easy to integrate your flutter app with OneSignal
https://www.onesignal.com
Other
623 stars 213 forks source link

[Bug]: java.lang.reflect.InvocationTargetException #943

Closed Marabushka closed 2 months ago

Marabushka commented 2 months ago

What happened?

I updated to 5.2.3 version of oneSignal and the app started to crash

Steps to reproduce?

1. install Flutter SDK 3.24.1
2. install onesignal_flutter 5.2.3

Run this code in Release mode:

await OneSignal.Debug.setLogLevel(_env.oneSignal.logLevel);
OneSignal.initialize(_env.oneSignal.appId);
OneSignal.Notifications.addClickListener(_clickListener);
OneSignal.Notifications.addForegroundWillDisplayListener(_foregroundWillDisplayListener);
unawaited(OneSignal.User.setLanguage(_settingsService.getLocale().languageCode));

What did you expect to happen?

not crash

OneSignal Flutter SDK version

5.2.3

Which platform(s) are affected?

Relevant log output

E/AndroidRuntime(14912): FATAL EXCEPTION: Thread-6
E/AndroidRuntime(14912): java.lang.reflect.InvocationTargetException
E/AndroidRuntime(14912):    at java.lang.reflect.Constructor.newInstance0(Native Method)
E/AndroidRuntime(14912):    at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
E/AndroidRuntime(14912):    at com.onesignal.common.services.ServiceRegistrationReflection.resolve(ServiceRegistration.kt:238)
E/AndroidRuntime(14912):    at com.onesignal.common.services.ServiceProvider.getServiceOrNull(ServiceProvider.kt:54)
E/AndroidRuntime(14912):    at com.onesignal.common.services.ServiceProvider.getService(ServiceProvider.kt:6)
E/AndroidRuntime(14912):    at com.onesignal.common.services.ServiceRegistrationReflection.resolve(ServiceRegistration.kt:208)
E/AndroidRuntime(14912):    at com.onesignal.common.services.ServiceProvider.getServiceOrNull(ServiceProvider.kt:54)
E/AndroidRuntime(14912):    at com.onesignal.common.services.ServiceProvider.getService(ServiceProvider.kt:6)
E/AndroidRuntime(14912):    at com.onesignal.common.services.ServiceRegistrationReflection.resolve(ServiceRegistration.kt:208)
E/AndroidRuntime(14912):    at com.onesignal.common.services.ServiceProvider.getAllServices(ServiceProvider.kt:52)
E/AndroidRuntime(14912):    at com.onesignal.core.internal.startup.StartupService.scheduleStart$lambda-2(StartupService.kt:10)
E/AndroidRuntime(14912):    at com.onesignal.core.internal.startup.StartupService.a(StartupService.kt:1)
E/AndroidRuntime(14912):    at np.a.run(R8$$SyntheticClass:3)
E/AndroidRuntime(14912):    at java.lang.Thread.run(Thread.java:920)
E/AndroidRuntime(14912): Caused by: java.lang.IllegalStateException: Method addObserver must be called on the main thread
E/AndroidRuntime(14912):    at androidx.lifecycle.n.f(LifecycleRegistry.kt:44)
E/AndroidRuntime(14912):    at androidx.lifecycle.n.a(LifecycleRegistry.kt:8)
E/AndroidRuntime(14912):    at java.lang.Class.newInstance(Native Method)
E/AndroidRuntime(14912):    at com.onesignal.notifications.internal.lifecycle.impl.NotificationLifecycleService.setupNotificationServiceExtension(NotificationLifecycleService.kt:53)
E/AndroidRuntime(14912):    at com.onesignal.notifications.internal.lifecycle.impl.NotificationLifecycleService.<init>(NotificationLifecycleService.kt:62)

Code of Conduct

Marabushka commented 2 months ago

in our service extended from INotificationServiceExtension we called the addObserver method, the solution was to call this method in the main thread

Handler(Looper.getMainLooper()).post { ProcessLifecycleOwner.get().lifecycle.addObserver(this) }