11using System . Diagnostics ;
22using System . Diagnostics . CodeAnalysis ;
3+ using Android . App ;
34using Android . Content ;
5+ using Android . Content . PM ;
6+ using Android . Util ;
47using Android . Views ;
58using Android . Widget ;
69using AndroidX . Media3 . Common ;
@@ -21,6 +24,7 @@ namespace CommunityToolkit.Maui.Core.Views;
2124
2225public partial class MediaManager : Java . Lang . Object , IPlayerListener
2326{
27+ const string Tag = "MediaManager" ;
2428 const int bufferState = 2 ;
2529 const int readyState = 3 ;
2630 const int endedState = 4 ;
@@ -37,6 +41,14 @@ public partial class MediaManager : Java.Lang.Object, IPlayerListener
3741 MediaItem . Builder ? mediaItem ;
3842 BoundServiceConnection ? connection ;
3943
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+
4052 /// <summary>
4153 /// The platform native counterpart of <see cref="MediaElement"/>.
4254 /// </summary>
@@ -174,6 +186,35 @@ or PlaybackState.StateSkippingToQueueItem
174186 var mediaSession = new MediaSession . Builder ( Platform . AppContext , Player ) ;
175187 mediaSession . SetId ( Convert . ToBase64String ( Guid . NewGuid ( ) . ToByteArray ( ) ) [ ..8 ] ) ;
176188
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+
177218 session ??= mediaSession . Build ( ) ?? throw new InvalidOperationException ( "Session cannot be null" ) ;
178219 ArgumentNullException . ThrowIfNull ( session . Id ) ;
179220
0 commit comments