Xamarin Forms Android端Firebase推送通知不显示问题求助
解决Xamarin Forms Android Firebase推送通知不显示问题
核心问题分析
问题根源在于通知通道配置不统一,Android 8.0+系统要求必须显式创建通知通道,同时启动页的Intent处理遗漏了推送数据,导致通知无法正常展示。
具体修复步骤
1. 统一通知通道ID并显式创建通道
你的代码中存在通道ID不一致的问题:MainApplication里设置的是FirebasePushNotificationChannel,但strings.xml和AndroidManifest里是default,导致Firebase找不到对应通道。
修改strings.xml,统一通道ID:
<?xml version="1.0" encoding="UTF-8" ?> <resources> <string name="notification_channel_id">FirebasePushNotificationChannel</string> </resources>
在MainApplication的OnCreate方法中添加手动创建通道的代码,确保通道被正确初始化:
public override void OnCreate() { base.OnCreate(); if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O) { var channelId = "FirebasePushNotificationChannel"; FirebasePushNotificationManager.DefaultNotificationChannelId = channelId; FirebasePushNotificationManager.DefaultNotificationChannelName = "General"; // 手动创建通知通道 var channel = new NotificationChannel(channelId, "General", NotificationImportance.Default) { Description = "Firebase推送通知通道" }; var notificationManager = (NotificationManager)GetSystemService(NotificationService); notificationManager.CreateNotificationChannel(channel); } #if DEBUG FirebasePushNotificationManager.Initialize(this, true); #else FirebasePushNotificationManager.Initialize(this,false); #endif CrossFirebasePushNotification.Current.OnNotificationReceived += (s, p) => { }; }
2. 处理SplashActivity的推送Intent
由于启动页是SplashActivity,需要将推送的Intent传递给MainActivity,否则应用冷启动时无法处理推送通知。
修改SplashActivity的SimulateStartup方法:
async Task SimulateStartup() { await Task.Delay(1000); var intent = new Intent(Application.Context, typeof(MainActivity)); intent.PutExtras(Intent.Extras); // 传递所有推送相关数据 StartActivity(intent); }
同时在SplashActivity的OnCreate中处理启动时的推送数据:
protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); if (Intent.Extras != null) { var intent = new Intent(Application.Context, typeof(MainActivity)); intent.PutExtras(Intent.Extras); StartActivity(intent); Finish(); } }
3. 添加并请求通知权限(Android 12+)
你的targetSdkVersion是31(Android 12),需要在AndroidManifest.xml中添加通知权限:
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
在MainActivity中请求该权限:
protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); Xamarin.Essentials.Platform.Init(this, savedInstanceState); global::Xamarin.Forms.Forms.Init(this, savedInstanceState); // 请求Android 12+通知权限 if (Build.VERSION.SdkInt >= BuildVersionCodes.S) { if (CheckSelfPermission(Android.Manifest.Permission.PostNotifications) != Android.Content.PM.Permission.Granted) { RequestPermissions(new[] { Android.Manifest.Permission.PostNotifications }, 100); } } LoadApplication(new App()); FirebasePushNotificationManager.ProcessIntent(this, Intent); }
4. 验证推送Payload格式
确保发送的Firebase推送Payload包含notification字段,仅data字段的推送需要手动构建通知:
示例Payload:
{ "to": "DEVICE_TOKEN", "notification": { "title": "测试通知", "body": "这是一条Firebase测试推送" }, "data": { "key1": "value1", "key2": "value2" } }
如果仅发送data字段,需在OnNotificationReceived事件中手动创建通知:
CrossFirebasePushNotification.Current.OnNotificationReceived += (s, p) => { if (!p.Data.ContainsKey("title") || !p.Data.ContainsKey("body")) return; var title = p.Data["title"].ToString(); var body = p.Data["body"].ToString(); var notificationManager = (NotificationManager)Android.App.Application.Context.GetSystemService(Android.Content.Context.NotificationService); var channelId = FirebasePushNotificationManager.DefaultNotificationChannelId; var notificationBuilder = new NotificationCompat.Builder(Android.App.Application.Context, channelId) .SetContentTitle(title) .SetContentText(body) .SetSmallIcon(Resource.Mipmap.icon_round) .SetAutoCancel(true); notificationManager.Notify(0, notificationBuilder.Build()); };
验证步骤
- 清理项目并重新生成Android版本
- 卸载设备上的旧应用,重新安装
- 发送测试推送,检查通知是否显示
- 查看Logcat,确认无通道相关错误
内容的提问来源于stack exchange,提问作者hk77
相关产品推荐
相关产品推荐

