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