Skip to content

Fix a crash when application put to background restriction by re…#1731

Open
rahul-lohra wants to merge 3 commits into
bugfix/rahullohra/background-call-disabled-regressionfrom
bugfix/rahullohra/battery-restrictions
Open

Fix a crash when application put to background restriction by re…#1731
rahul-lohra wants to merge 3 commits into
bugfix/rahullohra/background-call-disabled-regressionfrom
bugfix/rahullohra/battery-restrictions

Conversation

@rahul-lohra

@rahul-lohra rahul-lohra commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

Goal

Closes #AND-1251
Stacked on top of PR: #1729
This change improves notification handling when the application is subject to background restrictions.

CallStyle notifications may fail to be posted as foreground service notifications on restricted devices, resulting in a CannotPostForegroundServiceNotificationException ("Bad notification for startForeground").
See GH Issue: Demo app always crashes with restricted battery settings

Implementation

To avoid this issue, the SDK now detects when the application is background-restricted and falls back to a standard notification instead of using CallStyle.

Changes

  1. Detect whether the application is background-restricted using ActivityManager.isBackgroundRestricted.
  2. Avoid using CallStyle notifications when restrictions are active.
  3. Fall back to a standard notification with actions to prevent foreground notification failures.

🎨 UI Changes

None

Testing

  1. Put your application on background restriction mode (It is available on Samsung Devices)
  2. Go to demo-app application's settings
  3. Choose Battery
  4. Choose Restricted
  5. Join a room/ make a call
  6. Put your app in background ~ everything should run smoothly

Additional Notes

  • In background-restricted mode, the ongoing call notification can be dismissed by the user while the call remains active.
  • Similar behavior was observed in WhatsApp and Google Meet.
  • This PR does not introduce any additional handling for this scenario.

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced notification system to prevent potential exceptions when the app is subject to Android background restrictions
    • Improved call notification formatting on Android 12+ devices to gracefully handle background-restricted scenarios
    • Implemented background restriction detection for better compatibility with Android's foreground service notification policies

…ndering normal notification instead of CallStyle notification
@rahul-lohra rahul-lohra self-assigned this Jun 22, 2026
@rahul-lohra rahul-lohra requested a review from a team as a code owner June 22, 2026 11:03
@rahul-lohra rahul-lohra added the pr:bug Fixes a bug label Jun 22, 2026
@github-actions

github-actions Bot commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

PR checklist ✅

All required conditions are satisfied:

  • Title length is OK (or ignored by label).
  • At least one pr: label exists.
  • Sections ### Goal, ### Implementation, and ### Testing are filled, or the PR is bot-authored.
  • An issue is linked (Linear ticket or GitHub issue), or the PR is bot-authored.

🎉 Great job! This PR is ready for review.

@coderabbitai

coderabbitai Bot commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

A new internal BackgroundRestrictions class is added to query ActivityManager.isBackgroundRestricted on API 28+. StreamDefaultNotificationHandler instantiates it and adds a !batteryRestrictions.isRestricted() guard alongside the existing Android 12+ check in both the hang-up and call-actions notification builders.

Changes

Background restriction detection and notification gating

Layer / File(s) Summary
BackgroundRestrictions utility class
stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/BackgroundRestrictions.kt
New internal class with isRestricted() that returns ActivityManager.isBackgroundRestricted on API 28+ and false on earlier versions.
Notification handler integration
stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/notifications/handlers/StreamDefaultNotificationHandler.kt
Adds batteryRestrictions property and extends the call-style notification condition in both the hang-up builder (line 1096–1101) and call-actions builder (line 1120–1125) to also require !batteryRestrictions.isRestricted().

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related issues

  • Demo app always crashes with restricted battery settings #1549: This PR directly addresses the crash by introducing BackgroundRestrictions detection and modifying StreamDefaultNotificationHandler to avoid call-style notifications when the app is background-restricted, preventing CannotPostForegroundServiceNotificationException.

Poem

🐇 Hop hop, the battery's tight,
No fancy call-style in restricted night.
isRestricted() checks the Android gate,
Falls back to plain before it's too late.
A bunny guards the foreground with care! 🔋

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Title check ❓ Inconclusive The title is truncated and incomplete (ends with 're…'), making it vague about what is being fixed. It references a crash with background restrictions but lacks clarity on the solution approach. Complete the title to clearly describe the fix, e.g., 'Fix crash when app is background-restricted by falling back to standard notifications' or similar.
✅ Passed checks (3 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description check ✅ Passed The description covers Goal, Implementation, and Testing sections well, but is missing most of the required checklist items (CLA, assignee, changelog, unit tests, etc.) and lacks UI/visual evidence or a GIF.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch bugfix/rahullohra/battery-restrictions

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In
`@stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/BackgroundRestrictions.kt`:
- Around line 25-32: The isRestricted() function calls getSystemService() which
can return null, but the returned ActivityManager object is used directly
without null-checking. Add null-safe handling by using the safe-call operator
when accessing the isBackgroundRestricted property on the ActivityManager
instance. When the ActivityManager is null, the expression should default to
false to maintain the current behavior for API levels below P.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 981c5233-e99c-4900-bff1-2fad243b86bb

📥 Commits

Reviewing files that changed from the base of the PR and between 44b26fe and 6f893dd.

📒 Files selected for processing (2)
  • stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/BackgroundRestrictions.kt
  • stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/notifications/handlers/StreamDefaultNotificationHandler.kt

@github-actions

github-actions Bot commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

SDK Size Comparison 📏

SDK Before After Difference Status
stream-video-android-core 12.27 MB 12.27 MB 0.00 MB 🟢
stream-video-android-ui-xml 5.68 MB 5.68 MB 0.00 MB 🟢
stream-video-android-ui-compose 6.20 MB 6.20 MB 0.00 MB 🟢

@rahul-lohra rahul-lohra changed the title fix: Fix a crash when application put to background restriction by re… [AND-1251] Fix a crash when application put to background restriction by re… Jun 22, 2026
@rahul-lohra rahul-lohra changed the title [AND-1251] Fix a crash when application put to background restriction by re… Fix a crash when application put to background restriction by re… Jun 22, 2026
…l-disabled-regression' into bugfix/rahullohra/battery-restrictions
@sonarqubecloud

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
66.7% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

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

Labels

pr:bug Fixes a bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant