MAUI中Foreground模式下NotificationTapped事件未触发的问题
MAUI中Plugin.Firebase前台模式下NotificationTapped事件不触发的解决方法
核心原因
当应用处于**Foreground(前台)**状态时,FCM不会自动在系统通知栏显示推送通知,用户无法点击通知触发NotificationTapped事件,需手动处理前台通知的接收、显示及点击逻辑。
解决步骤
1. 订阅FCM的Received事件并绑定本地通知点击逻辑
在App.xaml.cs中新增Received事件订阅,用于捕获前台状态下的通知;同时订阅本地通知的点击事件,将其转发到原有处理逻辑:
public partial class App : Application { public App() { InitializeComponent(); CrossFirebaseCloudMessaging.Current.NotificationTapped += Current_NotificationTapped; // 订阅FCM通知接收事件(处理前台场景) CrossFirebaseCloudMessaging.Current.Received += Current_Received; // 订阅本地通知点击事件 LocalNotificationCenter.Current.NotificationClicked += OnLocalNotificationClicked; } private void Current_NotificationTapped(object sender, FCMNotificationTappedEventArgs e) { try { var data = e.Notification.Data; // 处理通知数据与页面跳转逻辑 } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Exception:>>" + ex); } } // 处理前台状态下的FCM通知接收 private void Current_Received(object sender, FCMNotificationReceivedEventArgs e) { if (Application.Current?.CurrentState == ApplicationState.Active) { // 构建本地通知请求 var notificationRequest = new NotificationRequest { NotificationId = Guid.NewGuid().GetHashCode(), Title = e.Notification.Title, Description = e.Notification.Body, Data = e.Notification.Data, Android = new AndroidOptions { ChannelId = "fcm_foreground_channel" // 对应后续创建的通知通道ID } }; // 发送本地通知到系统通知栏 _ = LocalNotificationCenter.Current.Show(notificationRequest); } } // 将本地通知点击事件转发到原有处理方法 private void OnLocalNotificationClicked(object sender, NotificationEventArgs e) { var fcmArgs = new FCMNotificationTappedEventArgs(new FCMNotification { Data = e.Request.Data }); Current_NotificationTapped(this, fcmArgs); } }
2. 配置Android通知通道(Android 8.0+ 必需)
在Android项目的MainApplication.cs中添加通知通道创建逻辑,确保前台通知能正常显示:
using Android.App; using Android.Content; using Android.OS; using Firebase; namespace YourAppNamespace.Droid; [Application] public class MainApplication : MauiApplication { public MainApplication(IntPtr handle, JniHandleOwnership ownership) : base(handle, ownership) { } protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp(); public override void OnCreate() { base.OnCreate(); FirebaseApp.InitializeApp(this); // 创建前台通知通道 CreateForegroundNotificationChannel(); } private void CreateForegroundNotificationChannel() { if (Build.VERSION.SdkInt >= BuildVersionCodes.O) { var channel = new NotificationChannel( "fcm_foreground_channel", "FCM前台通知", NotificationImportance.Default) { Description = "用于显示应用前台时的FCM推送通知" }; var notificationManager = (NotificationManager)GetSystemService(NotificationService); notificationManager.CreateNotificationChannel(channel); } } }
3. 添加Android通知权限
在Android项目的AndroidManifest.xml中添加POST_NOTIFICATIONS权限:
<manifest ...> <uses-permission android:name="android.permission.POST_NOTIFICATIONS" /> ... </manifest>
4. 安装本地通知依赖包
通过NuGet安装Plugin.LocalNotification包,用于前台通知的显示与点击处理:
Install-Package Plugin.LocalNotification
说明
- 前台状态下,FCM推送先触发
Received事件,我们通过该事件创建本地通知并显示在系统通知栏 - 用户点击本地通知时,触发
NotificationClicked事件,将其转发到原有的Current_NotificationTapped方法,实现逻辑复用 - 通知通道是Android 8.0及以上版本的强制要求,未配置则通知无法显示
内容的提问来源于stack exchange,提问作者Matthew Pans
相关产品推荐
相关产品推荐

