Skip to content

Commit 804a177

Browse files
committed
Fix tap not working on android play notification
1 parent d18747e commit 804a177

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System.Diagnostics.CodeAnalysis;
1+
using System;
2+
using System.Diagnostics.CodeAnalysis;
3+
using System.Reflection;
24
using System.Runtime.Versioning;
35
using Android.App;
46
using Android.Content;
@@ -90,6 +92,14 @@ public void UpdateNotifications(in MediaSession session, in PlatformMediaElement
9092
}
9193

9294
notificationBuilder.SetStyle(style);
95+
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+
93103
NotificationManagerCompat.From(Platform.AppContext)?.Notify(1, notificationBuilder.Build());
94104
}
95105

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
using System.Diagnostics;
22
using System.Diagnostics.CodeAnalysis;
3+
using Android.App;
34
using Android.Content;
5+
using Android.Content.PM;
6+
using Android.Util;
47
using Android.Views;
58
using Android.Widget;
69
using AndroidX.Media3.Common;
@@ -21,6 +24,7 @@ namespace CommunityToolkit.Maui.Core.Views;
2124

2225
public 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

Comments
 (0)