-
Notifications
You must be signed in to change notification settings - Fork 473
Fix clicking on notification does not restore activity #2979
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix clicking on notification does not restore activity #2979
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This pull request fixes notification click behavior in the MediaControlsService for Android by adding a content intent that properly restores the main app activity when the user taps the foreground service notification.
Key changes:
- Added
CreateActivityPendingIntent()helper method to construct the appropriate PendingIntent - Integrated the PendingIntent into the notification builder via
SetContentIntent()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated 3 comments.
| var packageName = Platform.AppContext.PackageName ?? throw new InvalidOperationException("PackageName cannot be null"); | ||
| var packageManager = Platform.AppContext.PackageManager ?? throw new InvalidOperationException("PackageManager cannot be null"); | ||
| var launchIntent = packageManager.GetLaunchIntentForPackage(packageName) ?? throw new InvalidOperationException("Launch intent cannot be null"); |
Copilot
AI
Jan 19, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to the project's coding guidelines, null checking should use the is pattern instead of the null-coalescing operator with throw. Consider refactoring these null checks to use pattern matching for consistency with the codebase standards.
For example, instead of:
var packageName = Platform.AppContext.PackageName ?? throw new InvalidOperationException("PackageName cannot be null");Use:
if (Platform.AppContext.PackageName is not { } packageName)
{
throw new InvalidOperationException("PackageName cannot be null");
}This pattern should be applied to all three null checks in this method (PackageName, PackageManager, and the launch intent).
| return PendingIntent.GetActivity(Platform.AppContext, 0, launchIntent, flags) | ||
| ?? throw new InvalidOperationException("PendingIntent cannot be null"); |
Copilot
AI
Jan 19, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to the project's coding guidelines, null checking should use the is pattern instead of the null-coalescing operator with throw. Consider refactoring this null check to use pattern matching for consistency with the codebase standards.
For example, instead of:
return PendingIntent.GetActivity(Platform.AppContext, 0, launchIntent, flags) ?? throw new InvalidOperationException("PendingIntent cannot be null");Use:
if (PendingIntent.GetActivity(Platform.AppContext, 0, launchIntent, flags) is not { } pendingIntent)
{
throw new InvalidOperationException("PendingIntent cannot be null");
}
return pendingIntent;|
|
||
| launchIntent.SetFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop); | ||
|
|
||
| var flags = PendingIntentFlags.UpdateCurrent | PendingIntentFlags.Immutable; |
Copilot
AI
Jan 19, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PendingIntentFlags.Immutable flag was introduced in Android 12 (API 31), but this service supports Android 26+ (as indicated by the [SupportedOSPlatform("Android26.0")] attribute). Using this flag unconditionally will cause runtime errors on Android versions 26-30.
Consider adding a version check to use the appropriate flags:
var flags = OperatingSystem.IsAndroidVersionAtLeast(31)
? PendingIntentFlags.UpdateCurrent | PendingIntentFlags.Immutable
: PendingIntentFlags.UpdateCurrent;Alternatively, if Android 12+ is now the minimum supported version, update the SupportedOSPlatform attribute on the class accordingly.
| var flags = PendingIntentFlags.UpdateCurrent | PendingIntentFlags.Immutable; | |
| var flags = OperatingSystem.IsAndroidVersionAtLeast(31) | |
| ? PendingIntentFlags.UpdateCurrent | PendingIntentFlags.Immutable | |
| : PendingIntentFlags.UpdateCurrent; |
Description of Change
This pull request improves the behavior of the foreground service notification in the
MediaControlsServicefor Android by ensuring that tapping the notification brings the user back to the main app activity. The changes introduce a helper method to create the appropriatePendingIntentand attach it to the notification.Enhancement to notification interaction:
MediaControlsServicenow includes a content intent, so tapping the notification will launch (or bring to front) the main app activity. This is achieved by creating aPendingIntentusing the newCreateActivityPendingIntent()method and setting it withSetContentIntenton the notification builder.Internal code improvements:
CreateActivityPendingIntent()method that safely constructs aPendingIntentto launch the main activity, handling potential null references and setting intent flags to ensure correct activity behavior.Linked Issues
PR Checklist
approved(bug) orChampioned(feature/proposal)mainat time of PRAdditional information
Fixes a long standing issue where clicking on notification after leaving app does not restore app. This fixes that issue and restores expected default android behavior.