diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d38d82f80..0c6feed9c6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,7 +30,7 @@ }, appRunner: () => runApp(MyApp()), ); - ``` + ``` - Linux native error & obfuscation support ([#2431](https://github.com/getsentry/sentry-dart/pull/2431)) - Improve Device context on plain Dart and Flutter desktop apps ([#2441](https://github.com/getsentry/sentry-dart/pull/2441)) - Add debounce to capturing screenshots ([#2368](https://github.com/getsentry/sentry-dart/pull/2368)) @@ -53,6 +53,7 @@ ### Fixes - OS & device contexts missing on Windows ([#2439](https://github.com/getsentry/sentry-dart/pull/2439)) +- Native iOS/macOS SDK session didn't start after Flutter hot-restart ([#2452](https://github.com/getsentry/sentry-dart/pull/2452)) ### Dependencies diff --git a/flutter/ios/Classes/SentryFlutterPluginApple.swift b/flutter/ios/Classes/SentryFlutterPluginApple.swift index 727928f913..e739fd080f 100644 --- a/flutter/ios/Classes/SentryFlutterPluginApple.swift +++ b/flutter/ios/Classes/SentryFlutterPluginApple.swift @@ -16,18 +16,6 @@ public class SentryFlutterPluginApple: NSObject, FlutterPlugin { private static let nativeClientName = "sentry.cocoa.flutter" - // The Cocoa SDK is init. after the notification didBecomeActiveNotification is registered. - // We need to be able to receive this notification and start a session when the SDK is fully operational. - private var didReceiveDidBecomeActiveNotification = false - - private var didBecomeActiveNotificationName: NSNotification.Name { -#if os(iOS) - return UIApplication.didBecomeActiveNotification -#elseif os(macOS) - return NSApplication.didBecomeActiveNotification -#endif - } - private static var pluginRegistrationTime: Int64 = 0 public static func register(with registrar: FlutterPluginRegistrar) { @@ -40,7 +28,6 @@ public class SentryFlutterPluginApple: NSObject, FlutterPlugin { #endif let instance = SentryFlutterPluginApple(channel: channel) - instance.registerObserver() registrar.addMethodCallDelegate(instance, channel: channel) } @@ -51,22 +38,6 @@ public class SentryFlutterPluginApple: NSObject, FlutterPlugin { private lazy var sentryFlutter = SentryFlutter() - private func registerObserver() { - NotificationCenter.default.addObserver(self, - selector: #selector(applicationDidBecomeActive), - name: didBecomeActiveNotificationName, - object: nil) - } - - @objc private func applicationDidBecomeActive() { - didReceiveDidBecomeActiveNotification = true - // we only need to do that in the 1st time, so removing it - NotificationCenter.default.removeObserver(self, - name: didBecomeActiveNotificationName, - object: nil) - - } - private lazy var iso8601Formatter: DateFormatter = { let formatter = DateFormatter() formatter.locale = Locale(identifier: "en_US_POSIX") @@ -352,15 +323,17 @@ public class SentryFlutterPluginApple: NSObject, FlutterPlugin { } } - if didReceiveDidBecomeActiveNotification && - (PrivateSentrySDKOnly.options.enableAutoSessionTracking || - PrivateSentrySDKOnly.options.enableWatchdogTerminationTracking) { - // We send a SentryHybridSdkDidBecomeActive to the Sentry Cocoa SDK, so the SDK will mimics - // the didBecomeActiveNotification notification. This is needed for session and OOM tracking. - NotificationCenter.default.post(name: Notification.Name("SentryHybridSdkDidBecomeActive"), object: nil) - // we reset the flag for the sake of correctness - didReceiveDidBecomeActiveNotification = false - } + #if os(iOS) || targetEnvironment(macCatalyst) + let appIsActive = UIApplication.shared.applicationState == .active + #else + let appIsActive = NSApplication.shared.isActive + #endif + + // We send a SentryHybridSdkDidBecomeActive to the Sentry Cocoa SDK, to mimic + // the didBecomeActiveNotification notification. This is needed for session, OOM tracking, replays, etc. + if appIsActive { + NotificationCenter.default.post(name: Notification.Name("SentryHybridSdkDidBecomeActive"), object: nil) + } #if canImport(UIKit) && !SENTRY_NO_UIKIT #if os(iOS) || os(tvOS)