.NET MAUI安卓推送通知发布后OnMessageReceived不触发求助
问题描述
我开发了一个基于.NET MAUI的Android移动应用,通过Intent过滤器和服务,借助OnMessageReceived()和OnNewToken()实现了推送通知功能。开发阶段在连接设备上运行时一切正常,OnMessageReceived()能按预期触发。但打包发布为APK后,OnMessageReceived()不再被调用,反而会自动显示一条本地通知——哪怕应用处于前台状态,而我并没有编写相关代码来启用这个功能。不确定这是.NET MAUI的特定问题还是Firebase相关问题。
推送通知服务代码
[Service(Exported = false)] [IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })] public class PushNotificationFirebaseMessagingService : FirebaseMessagingService { /// <summary> /// Called when a new token is created /// </summary> /// <param name="token">The token created</param> public override void OnNewToken(string token) { try { // Set the token name to secure storage SecureStorage.SetAsync(Constants.PushNotificationTokenName, token) .SafeFireAndForget(false); } catch (Exception) { // Could be secure storage is not supported } } /// <summary> /// Called when any new notification hub message is received /// </summary> /// <param name="message">The message received</param> public override void OnMessageReceived(RemoteMessage message) { // Code executed on UI here } }
MainActivity代码
protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.PostNotifications) != Permission.Granted) { ActivityCompat.RequestPermissions(this, new[] { Manifest.Permission.PostNotifications }, 0); } CreateNotificationChannelIfNeeded(); } private void CreateNotificationChannelIfNeeded() { if (Build.VERSION.SdkInt >= BuildVersionCodes.O) { CreateNotificationChannel(); } } private void CreateNotificationChannel() { var channelId = $"{PackageName}.general"; var notificationManager = (NotificationManager)GetSystemService(NotificationService); var channel = new NotificationChannel(channelId, "General", NotificationImportance.Default); notificationManager.CreateNotificationChannel(channel); FirebaseCloudMessagingImplementation.ChannelId = channelId; //FirebaseCloudMessagingImplementation.SmallIconRef = Resource.Drawable.ic_push_small; }
其他配置说明
- AndroidManifest.xml中已添加权限:
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
可能的原因与解决方法
发布版混淆规则问题
发布版构建时R8混淆可能导致Firebase服务类被误处理,需在ProGuard.cfg(或rules.pro)中添加保留规则:-keep class com.google.firebase.** { *; } -keep class com.google.android.gms.** { *; } -keep class 你的命名空间.PushNotificationFirebaseMessagingService { *; }替换
你的命名空间为服务类实际所在命名空间。Intent Filter未正确注册
检查发布版生成的obj/Release/net7.0-android/android/AndroidManifest.xml,确认服务的Intent Filter是否存在。若缺失,需手动在项目的AndroidManifest.xml中添加服务声明:<service android:exported="false" android:name="你的命名空间.PushNotificationFirebaseMessagingService"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT" /> </intent-filter> </service>推送消息类型错误
Firebase推送分为通知消息和数据消息:- 通知消息由FCM自动展示,不会触发
OnMessageReceived(); - 数据消息会直接传递到
OnMessageReceived(),需自行处理展示逻辑。
确保发送的是数据消息,示例负载格式:
{ "data": { "title": "测试标题", "body": "测试内容", "custom_key": "自定义值" }, "to": "目标设备Token" }- 通知消息由FCM自动展示,不会触发
Firebase配置文件错误
确认发布版使用的google-services.json是对应发布环境的正确配置文件,避免开发/发布配置混淆。NuGet包版本问题
检查并更新Firebase相关NuGet包到最新稳定版,旧版本可能存在发布版兼容性问题。
内容的提问来源于stack exchange,提问作者Lewis
相关产品推荐
相关产品推荐

