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

MAUI中Firebase推送通知(应用关闭时)页面导航实现问题

解决MAUI中Firebase推送冷启动导航失效问题及通知数据获取

一、查看通知数据的方法

不管应用处于后台还是关闭状态,推送通知的自定义数据都会通过对应渠道传递,可通过以下方式获取:

1. 后台/前台状态

直接在CrossFirebaseCloudMessaging的MessageReceived事件中提取数据:

CrossFirebaseCloudMessaging.Current.MessageReceived += (s, e) =>
{
    var notificationData = e.Data; // 获取通知携带的自定义数据集合
    // 示例:提取目标页面路由
    if (notificationData.TryGetValue("target_page", out var targetPage))
    {
        // 后续导航逻辑
    }
};

2. 冷启动状态

应用关闭时点击通知启动,数据会被传递到平台启动入口,需从平台特定位置提取:


二、Android平台冷启动处理

Android冷启动时,通知数据会包含在启动Intent的Extras中,需在MainActivity中提取:

  1. 修改Platforms/Android/MainActivity.cs:
protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);

    // 检查是否由通知点击启动应用
    if (Intent.Extras != null)
    {
        foreach (var key in Intent.Extras.KeySet())
        {
            var value = Intent.Extras.GetString(key);
            // 将数据存入全局容器,供MAUI层使用
            App.NotificationLaunchData.Add(key, value);
        }
    }
}
  1. 在App.xaml.cs中初始化全局存储容器:
public static Dictionary<string, string> NotificationLaunchData { get; } = new Dictionary<string, string>();

三、iOS平台冷启动处理

iOS冷启动时,通知数据会在FinishedLaunching方法的options参数中传递,需在AppDelegate中处理:

  1. 修改Platforms/iOS/AppDelegate.cs:
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
    // 检查是否由远程通知启动应用
    if (options != null && options.ContainsKey(UIApplication.LaunchOptionsRemoteNotificationKey))
    {
        var notificationData = options[UIApplication.LaunchOptionsRemoteNotificationKey] as NSDictionary;
        if (notificationData != null)
        {
            foreach (var key in notificationData.Keys)
            {
                var nsKey = key as NSString;
                var value = notificationData[nsKey] as NSString;
                App.NotificationLaunchData.Add(nsKey.ToString(), value.ToString());
            }
        }
    }

    return base.FinishedLaunching(app, options);
}

四、统一导航逻辑

不管是后台热启动还是冷启动,都可以在MAUI层统一处理导航:

  1. 在App.xaml.cs的OnStart方法中检查冷启动数据:
protected override void OnStart()
{
    if (App.NotificationLaunchData.Count > 0)
    {
        // 提取目标页面路由并导航
        if (App.NotificationLaunchData.TryGetValue("target_page", out var targetPage))
        {
            Shell.Current.GoToAsync(targetPage);
        }
        // 清空数据,避免重复触发导航
        App.NotificationLaunchData.Clear();
    }
}
  1. 后台状态下的导航直接在MessageReceived事件中处理:
CrossFirebaseCloudMessaging.Current.MessageReceived += (s, e) =>
{
    if (e.Data.TryGetValue("target_page", out var targetPage))
    {
        Shell.Current.GoToAsync(targetPage);
    }
};

关键注意事项

  • 推送通知的自定义数据必须放在data字段中,不要仅放在notification字段里(iOS冷启动时无法获取notification字段内容)。
  • Android需在AndroidManifest.xml中配置MainActivity的启动模式,避免重复创建实例:
<activity android:name=".MainActivity"
          android:launchMode="singleTop">

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 07:30:02