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

Xamarin Forms:应用关闭时点击通知如何打开指定页面?

嘿,这个问题我之前帮不少开发者解决过——应用完全关闭时的推送通知导航确实得靠原生层来衔接,因为Forms此时还没启动呢。下面分Android和iOS给你一步步讲怎么实现:

Android 端处理步骤

当应用彻底关闭后点击通知,系统会重新启动你的MainActivity,我们要在这里捕获通知里的导航参数,再传给Forms层:

  1. 打开你的Android项目里的MainActivity.cs,在OnCreate方法里添加逻辑捕获通知数据:

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
    
        Xamarin.Forms.Forms.Init(this, savedInstanceState);
        CrossPushNotification.Current.Register();
    
        // 检查是否从通知点击启动应用
        if (Intent.Extras != null)
        {
            foreach (var key in Intent.Extras.KeySet())
            {
                var value = Intent.Extras.GetString(key);
                // 把导航参数存到Forms项目的静态变量里
                if (key.Equals("target_page", StringComparison.OrdinalIgnoreCase))
                {
                    App.TargetPage = value;
                }
                if (key.Equals("item_id", StringComparison.OrdinalIgnoreCase))
                {
                    App.ItemId = value;
                }
            }
        }
    
        LoadApplication(new App());
    }
    
  2. 回到Forms项目的App.xaml.cs,添加静态变量存储参数,并在OnStart里处理导航:

    public static string TargetPage { get; set; }
    public static string ItemId { get; set; }
    
    protected override void OnStart()
    {
        if (!string.IsNullOrEmpty(TargetPage))
        {
            // 根据参数跳转到对应页面,这里示例是详情页
            switch (TargetPage)
            {
                case "DetailPage":
                    MainPage = new NavigationPage(new DetailPage(ItemId));
                    break;
                // 可以扩展其他页面的逻辑
            }
            // 清空参数,避免下次启动重复跳转
            TargetPage = null;
            ItemId = null;
        }
    }
    
iOS 端处理步骤

iOS的逻辑和Android类似,但要通过UNUserNotificationCenterDelegate来捕获通知点击事件:

  1. 打开iOS项目的AppDelegate.cs,实现IUNUserNotificationCenterDelegate接口,并添加处理逻辑:

    using UserNotifications;
    
    [Register("AppDelegate")]
    public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate, IUNUserNotificationCenterDelegate
    {
        public override bool FinishedLaunching(UIApplication app, NSDictionary options)
        {
            global::Xamarin.Forms.Forms.Init();
            CrossPushNotification.Current.Register();
    
            // 设置通知中心代理
            UNUserNotificationCenter.Current.Delegate = this;
    
            // 处理iOS 10以下的通知启动逻辑
            if (options != null && options.ContainsKey(UIApplication.LaunchOptionsRemoteNotificationKey))
            {
                var notificationData = options[UIApplication.LaunchOptionsRemoteNotificationKey] as NSDictionary;
                HandleNotificationParams(notificationData);
            }
    
            LoadApplication(new App());
            return base.FinishedLaunching(app, options);
        }
    
        // iOS 10+:应用关闭时点击通知会触发这个方法
        [Export("userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:")]
        public void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
        {
            var notificationData = response.Notification.Request.Content.UserInfo;
            HandleNotificationParams(notificationData);
            completionHandler();
        }
    
        // 提取通知里的导航参数,传给Forms
        private void HandleNotificationParams(NSDictionary data)
        {
            if (data.ContainsKey(new NSString("target_page")))
            {
                App.TargetPage = data[new NSString("target_page")].ToString();
            }
            if (data.ContainsKey(new NSString("item_id")))
            {
                App.ItemId = data[new NSString("item_id")].ToString();
            }
        }
    }
    
  2. 同样,Forms项目的App.xaml.cs里的OnStart逻辑和Android端完全一致,不用额外修改。

关键注意点
  • 推送Payload配置:确保你发送的推送通知里包含自定义参数(比如target_page、item_id),Android要放在data字段里,iOS要放在aps字典之外的自定义字段中。
  • 测试方式:一定要彻底关闭应用(Android划掉后台,iOS从后台滑动关闭)再测试,不然会和前台/休眠状态的逻辑混淆。
  • 导航逻辑优化:如果你的应用默认MainPage已经是NavigationPage,也可以用MainPage.Navigation.PushAsync(new DetailPage(ItemId)),不过刚启动时直接设置MainPage更稳妥。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 04:20:01