You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xamarin Android推送通知:后台/杀死模式下OnMessageReceived()未触发求助

解决Xamarin Forms推送通知后台/杀死状态下页面跳转问题

问题根源

当推送负载同时包含notification和data字段时,Android系统在应用后台或被杀死状态下,会直接由系统层级接管通知展示,不会触发FirebaseMessagingService的OnMessageReceived方法,而是将data参数附加到应用启动的Intent中,这就是后台/杀死状态下你的跳转逻辑失效的原因。

解决方案

1. 修改推送负载(推荐方案)

删除notification字段,仅保留data字段。这种情况下,无论应用处于前台、后台还是杀死状态,都会触发OnMessageReceived方法,你可以在方法内自定义通知展示逻辑,同时处理页面跳转:

{
  "data" : {
    "title" : "Sample App",
    "body" : "New items are available",
    "Id" : "876df123",
    "pageName" : "ClothingsPage"
  }
}

在OnMessageReceived中手动构建通知并携带跳转参数:

public override void OnMessageReceived(RemoteMessage message)
{
    var title = message.Data["title"];
    var body = message.Data["body"];
    var id = message.Data["Id"];
    var pageName = message.Data["pageName"];

    // 构建带参数的跳转Intent
    var intent = new Intent(this, typeof(MainActivity));
    intent.PutExtra("Id", id);
    intent.PutExtra("pageName", pageName);
    intent.AddFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop);
    var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.UpdateCurrent | PendingIntentFlags.Immutable);

    // 构建并显示通知
    var notificationBuilder = new NotificationCompat.Builder(this, "your_channel_id")
        .SetSmallIcon(Resource.Drawable.ic_notification)
        .SetContentTitle(title)
        .SetContentText(body)
        .SetAutoCancel(true)
        .SetContentIntent(pendingIntent);

    var notificationManager = NotificationManagerCompat.From(this);
    notificationManager.Notify(0, notificationBuilder.Build());
}

2. 处理后台/杀死状态的启动Intent

如果无法修改推送负载,需要在MainActivity中获取Intent附加的data参数,实现页面跳转:

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);
    // 处理应用从杀死状态启动的场景
    HandleNotificationIntent(Intent);
}

protected override void OnNewIntent(Intent intent)
{
    base.OnNewIntent(intent);
    // 处理应用从后台唤醒的场景
    HandleNotificationIntent(intent);
}

private void HandleNotificationIntent(Intent intent)
{
    if (intent?.Extras != null)
    {
        var id = intent.GetStringExtra("Id");
        var pageName = intent.GetStringExtra("pageName");
        
        if (!string.IsNullOrEmpty(pageName))
        {
            // 在UI线程执行Xamarin Forms导航逻辑
            Xamarin.Forms.Device.BeginInvokeOnMainThread(() =>
            {
                switch (pageName)
                {
                    case "ClothingsPage":
                        App.Current.MainPage.Navigation.PushAsync(new ClothingsPage(id));
                        break;
                    // 其他页面跳转逻辑
                }
            });
        }
    }
}

3. 配置Android通知渠道(Android 8.0+)

Android 8.0及以上版本必须配置通知渠道,否则通知无法正常显示,在MainActivity的OnCreate方法中添加渠道初始化代码:

if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
    var channel = new NotificationChannel("your_channel_id", "通知渠道名称", NotificationImportance.Default)
    {
        Description = "通知渠道描述"
    };
    var notificationManager = (NotificationManager)GetSystemService(NotificationService);
    notificationManager.CreateNotificationChannel(channel);
}

内容的提问来源于stack exchange,提问作者Xamarin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 20:25:52