Skip to content

Commit db78861

Browse files
committed
Use approach from fix PR on CommunityToolkit/Maui/pull/2979
1 parent 2855859 commit db78861

File tree

2 files changed

+18
-48
lines changed

2 files changed

+18
-48
lines changed

libraries/CommunityToolkit.Maui.MediaElement/Services/MediaControlsService.android.cs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,23 @@ public void UpdateNotifications(in MediaSession session, in PlatformMediaElement
9393

9494
notificationBuilder.SetStyle(style);
9595

96-
// This bypasses view interception and ensures notification body taps work for Android 14+
97-
var sessionActivityPendingIntent = Core.Views.MediaManager.SessionActivityPendingIntent;
98-
if (sessionActivityPendingIntent != null)
99-
{
100-
notificationBuilder.SetContentIntent(sessionActivityPendingIntent);
101-
}
102-
10396
NotificationManagerCompat.From(Platform.AppContext)?.Notify(1, notificationBuilder.Build());
10497
}
10598

106-
[MemberNotNull(nameof(playerNotificationManager))]
99+
static PendingIntent CreateActivityPendingIntent()
100+
{
101+
var packageName = Platform.AppContext.PackageName ?? throw new InvalidOperationException("PackageName cannot be null");
102+
var packageManager = Platform.AppContext.PackageManager ?? throw new InvalidOperationException("PackageManager cannot be null");
103+
var launchIntent = packageManager.GetLaunchIntentForPackage(packageName) ?? throw new InvalidOperationException("Launch intent cannot be null");
104+
105+
launchIntent.SetFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop);
106+
107+
var flags = PendingIntentFlags.UpdateCurrent | PendingIntentFlags.Immutable;
108+
return PendingIntent.GetActivity(Platform.AppContext, 0, launchIntent, flags)
109+
?? throw new InvalidOperationException("PendingIntent cannot be null");
110+
}
111+
112+
[MemberNotNull(nameof(playerNotificationManager))]
107113
public void SetLegacyNotifications(in MediaSession session, in PlatformMediaElement mediaElement)
108114
{
109115
ArgumentNullException.ThrowIfNull(session);
@@ -168,12 +174,14 @@ void StartForegroundServices()
168174
NotificationManager ??= GetSystemService(NotificationService) as NotificationManager ?? throw new InvalidOperationException($"{nameof(NotificationManager)} cannot be null");
169175
notificationBuilder ??= new NotificationCompat.Builder(Platform.AppContext, "1");
170176

171-
notificationBuilder.SetSmallIcon(Resource.Drawable.media3_notification_small_icon);
177+
var pendingIntent = CreateActivityPendingIntent();
178+
notificationBuilder.SetSmallIcon(Resource.Drawable.media3_notification_small_icon);
172179
notificationBuilder.SetAutoCancel(false);
173180
notificationBuilder.SetForegroundServiceBehavior(NotificationCompat.ForegroundServiceImmediate);
174181
notificationBuilder.SetVisibility(NotificationCompat.VisibilityPublic);
182+
notificationBuilder.SetContentIntent(pendingIntent);
175183

176-
CreateNotificationChannel(NotificationManager);
184+
CreateNotificationChannel(NotificationManager);
177185

178186
if (OperatingSystem.IsAndroidVersionAtLeast(29))
179187
{

libraries/CommunityToolkit.Maui.MediaElement/Views/MediaManager.android.cs

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ namespace CommunityToolkit.Maui.Core.Views;
2424

2525
public partial class MediaManager : Java.Lang.Object, IPlayerListener
2626
{
27-
const string Tag = "MediaManager";
2827
const int bufferState = 2;
2928
const int readyState = 3;
3029
const int endedState = 4;
@@ -41,14 +40,6 @@ public partial class MediaManager : Java.Lang.Object, IPlayerListener
4140
MediaItem.Builder? mediaItem;
4241
BoundServiceConnection? connection;
4342

44-
// Store sessionActivityPendingIntent for notification contentIntent fallback (Android 14+)
45-
static PendingIntent? _sessionActivityPendingIntent;
46-
47-
/// <summary>
48-
/// Gets the session activity PendingIntent for notification contentIntent fallback (Android 14+ Pixel 7a fix).
49-
/// </summary>
50-
public static PendingIntent? SessionActivityPendingIntent => _sessionActivityPendingIntent;
51-
5243
/// <summary>
5344
/// The platform native counterpart of <see cref="MediaElement"/>.
5445
/// </summary>
@@ -186,35 +177,6 @@ or PlaybackState.StateSkippingToQueueItem
186177
var mediaSession = new MediaSession.Builder(Platform.AppContext, Player);
187178
mediaSession.SetId(Convert.ToBase64String(Guid.NewGuid().ToByteArray())[..8]);
188179

189-
// Set session activity PendingIntent to bring app to foreground when notification body is tapped (Android 14+)
190-
// This is the recommended approach for Android 14+ notification body taps
191-
// Uses explicit Intent targeting MainActivity with CLEAR_TOP | SINGLE_TOP flags for 100% reliability
192-
try
193-
{
194-
var activity = Microsoft.Maui.ApplicationModel.Platform.CurrentActivity
195-
?? throw new InvalidOperationException("CurrentActivity is null - this should never happen");
196-
197-
var sessionIntent = new Intent(activity, activity.GetType())
198-
.AddFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop)
199-
.SetAction(Intent.ActionMain)
200-
.AddCategory(Intent.CategoryLauncher);
201-
202-
var sessionActivityPendingIntent = PendingIntent.GetActivity(
203-
activity, // ⚠️ CRITICAL: Use the Activity, NOT AppContext
204-
0,
205-
sessionIntent,
206-
PendingIntentFlags.Immutable | PendingIntentFlags.UpdateCurrent);
207-
208-
mediaSession.SetSessionActivity(sessionActivityPendingIntent);
209-
210-
// Store for notification contentIntent fallback (Android 14+ Pixel 7a fix)
211-
_sessionActivityPendingIntent = sessionActivityPendingIntent;
212-
}
213-
catch (Exception ex)
214-
{
215-
Android.Util.Log.Error(Tag, $"Failed to set session activity: {ex}");
216-
}
217-
218180
session ??= mediaSession.Build() ?? throw new InvalidOperationException("Session cannot be null");
219181
ArgumentNullException.ThrowIfNull(session.Id);
220182

0 commit comments

Comments
 (0)