Skip to content

Conversation

@philipphofmann
Copy link
Member

📜 Description

This is still a draft PR, I would love to get some feedback on the chosen approach. I initially wanted to fix only the in_foreground app context, but then I realized there's a benefit in knowing if the app is in the foreground, but also if it's active or not. If we agree that this is the right approach, I'm going to add tests.

Fix the mismatch of the app context in foreground between fatal and non-fatal events. Also, fix the breadcrumbs to correctly add in_foreground and in background crumbs, and add new active crumbs for more info for the user.

💡 Motivation and Context

Fixes GH-6178

💚 How did you test it?

📝 Checklist

You have to check all boxes before merging:

  • I added tests to verify the changes.
  • No new PII added or SDK only sends newly added PII if sendDefaultPII is enabled.
  • I updated the docs if needed.
  • I updated the wizard if needed.
  • Review from the native team if needed.
  • No breaking change or entry added to the changelog.
  • No breaking change for hybrid SDKs or communicated to hybrid SDKs.

Fix the mismatch of the app context in foreground between fatal and
non-fatal events. Also, fix the breadcrumbs to correctly add
in_foreground and in background crumbs, and add new active crumbs for
more info for the user.

Fixes GH-6178
@github-actions
Copy link
Contributor

Semver Impact of This PR

🟢 Patch (bug fixes)

📋 Changelog Preview

This is how your changes will appear in the changelog.
Entries from this PR are highlighted with a left border (blockquote style).


New Features ✨

Metrics

  • Add implementation for metrics envelope item by philprime in #6960
  • Add integration with installation by SDK by philprime in #6956

Other

  • Add isiOSAppOnVisionOS, isiOSAppOnMac, isMacCatalystApp to device context by philprime in #6939

Bug Fixes 🐛

  • (logs) Use sendDefaultPii and span_id for attributes by philprime in #7055
  • In_foreground and is_active app context by philipphofmann in #7188
  • Fix incorrect variable assignment for 'sampled' key by xjshi in #7120
  • Mark dark theme deprecated by noahsmartin in #7114
  • Update raw_description in runtime context for Mac Catalyst App by philprime in #7082
  • Use correct parsing for stackframes by noahsmartin in #6908
  • Transport correctly handling 4xx and 5xx by dfed in #6618

Build / dependencies / internal 🔧

Deps

  • Bump ruby/setup-ruby from 1.279.0 to 1.281.0 by dependabot in #7160
  • Bump getsentry/craft from 2.18.1 to 2.18.3 by dependabot in #7161
  • Bump getsentry/craft/.github/workflows/changelog-preview.yml from 2.18.1 to 2.18.3 by dependabot in #7159
  • Bump ruby/setup-ruby from 1.276.0 to 1.279.0 by dependabot in #7117
  • Bump mikepenz/action-junit-report from 6.0.1 to 6.1.0 by dependabot in #7116
  • Update swiftlint version by github-actions in #7109
  • Bump ruby/setup-ruby from 1.275.0 to 1.276.0 by dependabot in #7103
  • Bump codecov/test-results-action from 1.1.1 to 1.2.1 by itaybre in #7087
  • Bump ruby/setup-ruby from 1.270.0 to 1.275.0 by itaybre in #7088
  • Bump peter-evans/create-pull-request from 7.0.11 to 8.0.0 by dependabot in #7084
  • Bump actions/download-artifact from 6 to 7 by dependabot in #7048
  • Bump aws-sdk-s3 from 1.205.0 to 1.208.0 by dependabot in #7074
  • Bump ruby/setup-ruby from 1.269.0 to 1.270.0 by dependabot in #7049
  • Update clang-format version by github-actions in #7056
  • Bump actions/cache from 4 to 5 by dependabot in #7052
  • Bump actions/upload-artifact from 5 to 6 by dependabot in #7050
  • Bump codecov/codecov-action from 5.5.1 to 5.5.2 by dependabot in #7051

Other

  • (dx) Add structured Makefile with usage description by philprime in #7129
  • (release) Switch from action-prepare-release to Craft (minimal) by BYK in #7153
  • Convert SentryNetworkTrackingIntegration to Swift by itaybre in #7106
  • Update iOS test destination OS version to 18.5 in fast-pr-checks workflow by itaybre in #7168
  • Fix typos in comments in multiple files v2 by philipphofmann in #7139
  • Run visionOS tests on Cirrus Runners + Boot simulator by itaybre in #7147
  • Skip jobs/steps that require secrets for non contributors by itaybre in #7124
  • Add attributable protocol for typed attribute values by philprime in #7077
  • Allow alpha releases on RNSentry.podspec for Cross Platform Test by itaybre in #7130
  • Remove swift5.9 checks by itaybre in #7098
  • Remove duplicate file in project by itaybre in #7093
  • Convert SentryMetricKitIntegration to Swift by noahsmartin in #7076
  • Removes HybridSDK subspec by itaybre in #7019
  • Move testRemoveImageFromTail to flaky plan by itaybre in #7041
  • Use at least xcode 16 for all jobs by itaybre in #7012
  • Cleanup file filter for required files modified by itaybre in #7031
  • Remove assembly workflow files from UI test filter by itaybre in #7030
  • Bumps macOS-14 runner to macOS-15 by itaybre in #7029
  • Ensure required simulators are loaded for all platforms by itaybre in #7022

Other

  • test: Add Options Documentation Sync Tests by philipphofmann in #7075

🤖 This preview updates automatically when you update the PR.

@github-actions
Copy link
Contributor

Fails
🚫 Please consider adding a changelog entry for the next release.

Instructions and example for changelog

Please add an entry to CHANGELOG.md to the "Unreleased" section. Make sure the entry includes this PR's number.

Example:

## Unreleased

### Fixes

- in_foreground and is_active app context ([#7188](https://github.com/getsentry/sentry-cocoa/pull/7188))

If none of the above apply, you can opt out of this check by adding #skip-changelog to the PR description or adding a skip-changelog label.

Generated by 🚫 dangerJS against 2fdafc0

- (void)trackApplicationNotifications
{
#if SENTRY_HAS_UIKIT
NSNotificationName foregroundNotificationName = UIApplicationDidBecomeActiveNotification;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's a key change. The foreground crumb now gets added when UIApplicationWillEnterForegroundNotification and not when UIApplicationDidBecomeActiveNotification. I think that's more accurate.

Comment on lines -726 to -727
BOOL inForeground = appState == UIApplicationStateActive;
app[@"in_foreground"] = @(inForeground);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here was the mismatch between fatal and non-fatal events. Here we used to set in_foreground to true if the app was active, but the app could also be in the foreground but not active and then this was false before. Now it's aligned with SentryCrash

- (void)applicationDidBecomeActive
{
sentrycrash_notifyAppActive(true);
}
- (void)applicationWillResignActive
{
sentrycrash_notifyAppActive(false);
}
- (void)applicationDidEnterBackground
{
sentrycrash_notifyAppInForeground(false);
}
- (void)applicationWillEnterForeground
{
sentrycrash_notifyAppInForeground(true);
}

@codecov
Copy link

codecov bot commented Jan 16, 2026

❌ 3 Tests Failed:

Tests completed Failed Passed Skipped
4214 3 4211 23
View the top 3 failed test(s) by shortest run time
SentryCrashReportConverterTests::testAppContextInForegroundTrue_IsTrue
Stack Traces | 0s run time
.../Tests/SentryTests/SentryCrashReportConverterTests.m:518 - (([one allKeys].count) equal to ([two allKeys].count)) failed: ("1") is not equal to ("2") - one: {
SentryCrashReportConverterTests::testTraceContext
Stack Traces | 0s run time
.../Tests/SentryTests/SentryCrashReportConverterTests.m:518 - (([one allKeys].count) equal to ([two allKeys].count)) failed: ("1") is not equal to ("2") - one: {
SentryTests.SentryClientTests::testCaptureExceptionWithAppStateInForegroudWhenAppIsInactive
Stack Traces | 0s run time
.../Tests/SentryTests/SentryClientTests.swift:1156 - XCTAssertFalse failed

To view more test analytics, go to the Test Analytics Dashboard
📋 Got 3 mins? Take this short survey to help us improve Test Analytics.

withDataValue:@"foreground"];
}];

[NSNotificationCenter.defaultCenter addObserverForName:didBecomeActiveNotificationName
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a swift name

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

App context in_foreground is true without foreground crumb

3 participants