.NET 8 MAUI如何实现推送通知?多种方案尝试均遇阻
.NET 8 MAUI 推送通知实现指南及常见问题解决
.NET 8 MAUI 完全可以实现跨平台推送通知,官方确实已内置相关能力(基于各平台原生推送API封装),以下是具体实现步骤及你遇到的问题的解决方案:
一、分平台配置步骤
Android 端(基于 Firebase Cloud Messaging)
- Firebase 项目配置
- 创建 Firebase 项目并添加 Android 应用,下载
google-services.json文件,放置到 MAUI 项目的Platforms/Android目录,设置其生成操作为GoogleServicesJson。
- 创建 Firebase 项目并添加 Android 应用,下载
- MAUI 项目配置
- 在
Platforms/Android/AndroidManifest.xml中添加权限:<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.POST_NOTIFICATIONS" /> - 在
MainApplication.cs中初始化 Firebase:using Firebase.Messaging; protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); FirebaseMessaging.Instance.AutoInitEnabled = true; } - 实现消息接收服务:创建继承自
FirebaseMessagingService的类,重写核心方法:[Service] [IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })] public class MyFirebaseMessagingService : FirebaseMessagingService { public override void OnMessageReceived(RemoteMessage message) { var notification = message.GetNotification(); if (notification != null) { // 创建通知渠道(Android 8.0+ 必需) var manager = GetSystemService(Context.NotificationService) as NotificationManager; var channel = new NotificationChannel("default", "默认通知", NotificationImportance.Default); manager.CreateNotificationChannel(channel); // 构建并显示通知 var builder = new NotificationCompat.Builder(this, "default") .SetContentTitle(notification.Title) .SetContentText(notification.Body) .SetSmallIcon(Resource.Drawable.appicon); manager.Notify(0, builder.Build()); } } public override void OnNewToken(string token) { // 将设备 Token 上传到你的服务器 base.OnNewToken(token); } } - 安装适配 .NET 8 的 NuGet 包:
Xamarin.Firebase.Messaging(建议使用最新稳定版)。
- 在
iOS 端(基于 APNs)
- Apple 开发者配置
- 在 Apple 开发者后台为应用开启「Push Notifications」能力,生成 APNs 证书并上传到 Firebase(或直接关联 Azure Notification Hubs)。
- MAUI 项目配置
- 在
Platforms/iOS/Info.plist中添加后台模式配置:<key>UIBackgroundModes</key> <array> <string>remote-notification</string> </array> - 在
AppDelegate.cs中处理推送回调:public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken) { // 将设备 Token 转换为字符串并上传服务器 var token = deviceToken.Description.Trim('<', '>').Replace(" ", ""); } public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler) { // 处理收到的通知内容 completionHandler(UIBackgroundFetchResult.NewData); } - 在应用启动时注册推送:在
AppDelegate.FinishedLaunching中调用UIApplication.SharedApplication.RegisterForRemoteNotifications();。
- 在
Windows 端(基于 WNS)
- Azure Notification Hubs 配置
- 在 Azure 门户创建 Notification Hubs 实例,关联 Windows 应用的 Package SID 和 Security Key(从 Microsoft Store 开发者后台获取)。
- MAUI 项目配置
- 在
Platforms/Windows/Package.appxmanifest中开启「Push Notifications」能力。 - 在
App.xaml.cs中注册推送通道:using Windows.Networking.PushNotifications; protected override void OnLaunched(LaunchActivatedEventArgs args) { base.OnLaunched(args); _ = RegisterForPushNotificationsAsync(); } private async Task RegisterForPushNotificationsAsync() { var channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync(); // 将通道 URI 上传到你的服务器或 Azure Notification Hubs channel.PushNotificationReceived += Channel_PushNotificationReceived; } private void Channel_PushNotificationReceived(PushNotificationChannel sender, PushNotificationReceivedEventArgs args) { if (args.NotificationType == PushNotificationType.Toast) { // 处理 Toast 通知 } }
- 在
二、常见问题解决方案
1. Firebase 相关插件文件缺失错误
- 清理 NuGet 缓存:运行命令
dotnet nuget locals all --clear,然后重新还原项目包。 - 删除项目目录下的
obj、bin文件夹,重新生成解决方案。 - 直接使用官方
Xamarin.Firebase系列包替代第三方插件,避免版本兼容性问题。
2. Plugin.FirebasePushNotification 收不到通知
- 检查
AndroidManifest.xml是否正确声明了 Firebase 消息服务权限和组件。 - 确认 Firebase 控制台发送的是数据消息(后台状态下只有数据消息能触发
OnMessageReceived),而非纯通知消息。 - 检查设备是否已开启应用的通知权限。
3. 官方内置能力说明
MAUI 并未提供统一的跨平台推送 API,而是通过封装各平台原生推送服务(Firebase、APNs、WNS)实现,相关文档可在微软 Docs 的 MAUI 平台特定功能章节中找到。
内容的提问来源于stack exchange,提问作者Enrico
相关产品推荐
相关产品推荐

