Skip to content

Commit 0f0207b

Browse files
committed
feat: add platform-specific initialization handling
- iOS: Use EventChannel-based signaling from native layer - Android/other platforms: Use direct initialization with null check - Add isInitialized flag in AppDelegate to handle late subscriptions - Add debug logging for initialization events
1 parent 71cfc76 commit 0f0207b

2 files changed

Lines changed: 27 additions & 9 deletions

File tree

example/ios/Runner/AppDelegate.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import mParticle_Apple_SDK
55
@main
66
@objc class AppDelegate: FlutterAppDelegate, FlutterStreamHandler {
77
private var eventSink: FlutterEventSink?
8+
9+
var isInitialized = false
810

911
override func application(
1012
_ application: UIApplication,
@@ -28,7 +30,7 @@ import mParticle_Apple_SDK
2830
object: nil
2931
)
3032

31-
let options = MParticleOptions(key: "api-key", secret: "secret")
33+
let options = MParticleOptions(key: "us1-773c44ed5daf6840a65b75832b548be1", secret: "Mqsh_nuVFbffg10jROjoRR2GudqY4t6rZ5ogQcgLBB-v1nL3kUabEEF6we5JOCmr")
3234
options.logLevel = MPILogLevel.verbose
3335
MParticle.sharedInstance().start(with: options)
3436

@@ -39,6 +41,9 @@ import mParticle_Apple_SDK
3941

4042
func onListen(withArguments arguments: Any?, eventSink events: @escaping FlutterEventSink) -> FlutterError? {
4143
self.eventSink = events
44+
if isInitialized {
45+
events(["initialized": true])
46+
}
4247
return nil
4348
}
4449

@@ -50,6 +55,7 @@ import mParticle_Apple_SDK
5055
// MARK: - mParticle Initialization
5156

5257
@objc private func handleMParticleInitialized(notification: Notification) {
58+
isInitialized = true
5359
if let eventSink = self.eventSink {
5460
eventSink(["initialized": true])
5561
}

example/lib/main.dart

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'dart:async';
2+
import 'dart:io';
23

34
import 'package:flutter/material.dart';
45
import 'package:flutter/services.dart';
@@ -76,18 +77,29 @@ class _MyAppState extends State<MyApp> {
7677

7778
// Platform messages are asynchronous, so we initialize in an async method.
7879
Future<void> initMparticle() async {
79-
// Subscribe to mParticle initialization events from native code
80-
_initializationSubscription = _initializationEventChannel
81-
.receiveBroadcastStream()
82-
.listen((event) {
83-
if (event is Map && event['initialized'] == true) {
80+
if (Platform.isIOS) {
81+
// iOS: Subscribe to mParticle initialization events from native code
82+
_initializationSubscription = _initializationEventChannel
83+
.receiveBroadcastStream()
84+
.listen((event) async {
85+
if (event is Map && event['initialized'] == true) {
86+
print("mParticle initialized from native layer");
87+
mpInstance = await MparticleFlutterSdk.getInstance();
88+
setState(() {
89+
_isInitialized = true;
90+
});
91+
}
92+
});
93+
} else {
94+
// Android and other platforms: Use original initialization
95+
mpInstance = await MparticleFlutterSdk.getInstance();
96+
97+
if (mpInstance != null) {
8498
setState(() {
8599
_isInitialized = true;
86100
});
87101
}
88-
});
89-
90-
mpInstance = await MparticleFlutterSdk.getInstance();
102+
}
91103
}
92104

93105
void identityCallbackSuccess(IdentityApiResult successResponse) {

0 commit comments

Comments
 (0)