From 08a60f196383121991368420cf1c2a4cf45739c7 Mon Sep 17 00:00:00 2001 From: James Crutchley Date: Mon, 1 Dec 2025 01:00:44 -0800 Subject: [PATCH] Fix clicking on notification does not restore activity --- .../Services/MediaControlsService.android.cs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/CommunityToolkit.Maui.MediaElement/Services/MediaControlsService.android.cs b/src/CommunityToolkit.Maui.MediaElement/Services/MediaControlsService.android.cs index c1b6de91d3..07f40aad18 100644 --- a/src/CommunityToolkit.Maui.MediaElement/Services/MediaControlsService.android.cs +++ b/src/CommunityToolkit.Maui.MediaElement/Services/MediaControlsService.android.cs @@ -157,11 +157,12 @@ void StartForegroundServices() { NotificationManager ??= GetSystemService(NotificationService) as NotificationManager ?? throw new InvalidOperationException($"{nameof(NotificationManager)} cannot be null"); notificationBuilder ??= new NotificationCompat.Builder(Platform.AppContext, "1"); - + var pendingIntent = CreateActivityPendingIntent(); notificationBuilder.SetSmallIcon(Resource.Drawable.media3_notification_small_icon); notificationBuilder.SetAutoCancel(false); notificationBuilder.SetForegroundServiceBehavior(NotificationCompat.ForegroundServiceImmediate); notificationBuilder.SetVisibility(NotificationCompat.VisibilityPublic); + notificationBuilder.SetContentIntent(pendingIntent); CreateNotificationChannel(NotificationManager); @@ -177,4 +178,17 @@ void StartForegroundServices() StartForeground(1, notificationBuilder.Build()); } } + + static PendingIntent CreateActivityPendingIntent() + { + 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"); + + launchIntent.SetFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop); + + var flags = PendingIntentFlags.UpdateCurrent | PendingIntentFlags.Immutable; + return PendingIntent.GetActivity(Platform.AppContext, 0, launchIntent, flags) + ?? throw new InvalidOperationException("PendingIntent cannot be null"); + } } \ No newline at end of file