Skip to content

[Bug]: "Must call 'initWithContext' before use" crash via InAppMessages.lifecycleInit on app resume during init (v5.5.4) #1165

Description

@saantdahal

What happened?

Native crash occurs when the app resumes/backgrounds while OneSignal.initialize() is still in progress (e.g. init is gated behind an async network call that fetches the OneSignal App ID before calling initialize). The plugin's internal OneSignalInAppMessages.lifecycleInit fires automatically on app resume and calls OneSignal.getInAppMessages() natively, regardless of whether Dart-side initialize() has completed. This throws Must call 'initWithContext' before use from inside DartMessenger's binary message listener it is NOT caught by any try/catch around our own initialize() call, since it originates from the plugin's own internal lifecycle listener, not our call chain.

Steps to reproduce?

1. Call OneSignal.initialize(apiId) where apiId is fetched asynchronously (e.g. from a backend call taking 1–10s)
2.Before that async chain resolves, background the app (or trigger any activity resume — e.g. quickly switch apps and come back)
3.Native side throws Must call 'initWithContext' before use from OneSignalInAppMessages.lifecycleInit -> onMethodCall, crashing the app in production builds

What did you expect to happen?

InAppMessages.lifecycleInit's native call should check whether initWithContext has been called yet and no-op/queue silently instead of throwing, since it's an internal automatic lifecycle hook the app author does not control the timing of.

OneSignal Flutter SDK version

5.5.4

Which platform(s) are affected?

  • iOS
  • Android

Relevant log output

PlatformException(error, Must call 'initWithContext' before use, null, java.lang.IllegalStateException: Must call 'initWithContext' before use
	at com.onesignal.internal.OneSignalImpl.getServiceWithFeatureGate(SourceFile:27)
	at com.onesignal.internal.OneSignalImpl.getInAppMessages(SourceFile:1)
	at com.onesignal.OneSignal.getInAppMessages(SourceFile:7)
	at com.onesignal.flutter.OneSignalInAppMessages.lifecycleInit(SourceFile:1)
	at com.onesignal.flutter.OneSignalInAppMessages.onMethodCall(SourceFile:121)
	at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(SourceFile:18)
	at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(SourceFile:19)
	at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(SourceFile:36)
	at io.flutter.embedding.engine.dart.DartMessenger.a(SourceFile:1)
	at io.flutter.embedding.engine.dart.DartMessenger.run(SourceFile:1)

Code of Conduct

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

Labels

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions