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

点击本地通知后如何实现Xamarin Form导航并返回应用

Xamarin.Forms 点击Plugin.LocalNotifications通知唤起应用+导航实现方案

你当前使用的Plugin.LocalNotifications插件本身已经封装了通知点击的基础逻辑,只需要补齐平台配置和全局事件监听即可实现点击通知唤起前台+页面跳转,具体步骤如下:

1. 平台端必要配置

Android端

  • 给MainActivity配置单例启动模式,避免点击通知时重复创建应用实例,找到MainActivity的Activity特性,添加LaunchMode = LaunchMode.SingleTop配置:
[Activity(Label = "你的应用名", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, LaunchMode = LaunchMode.SingleTop, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
  • 重写两个生命周期方法,透传通知点击的Intent给插件,覆盖热启动(应用在后台)和冷启动(应用被杀死)两种场景:
protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);
    global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
    // 冷启动场景下接收通知参数
    NotificationCenter.Current.NotificationTapped(Intent);
    LoadApplication(new App());
}

protected override void OnNewIntent(Intent intent)
{
    base.OnNewIntent(intent);
    // 热启动场景下接收通知参数
    NotificationCenter.Current.NotificationTapped(intent);
}

iOS端

  • 打开AppDelegate.cs,在FinishedLaunching方法中完成基础配置和通知权限申请:
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
    global::Xamarin.Forms.Forms.Init();
    LoadApplication(new App());
    // 申请通知权限
    UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound, (granted, error) => { });
    return base.FinishedLaunching(app, options);
}

完成上述平台配置后,点击本地通知就会自动把应用唤起至前台,不需要额外编写唤起前台的代码

2. 共享项目端实现导航逻辑

打开共享项目的App.xaml.cs,在构造函数中注册全局通知点击事件,处理页面导航:

public App()
{
    InitializeComponent();
    // 你的应用根页面配置,确保用NavigationPage包裹才能支持页面导航
    MainPage = new NavigationPage(new MainPage());
    // 注册全局通知点击监听
    NotificationCenter.Current.NotificationTapped += OnNotificationTapped;
}

private void OnNotificationTapped(NotificationTappedEventArgs e)
{
    // 所有UI操作必须切到主线程执行
    Device.BeginInvokeOnMainThread(async () =>
    {
        // e.ReturningData 就是你创建通知时传入的ReturningData字段,可以用来传递页面标识、业务参数
        var returnData = e.ReturningData?.ToString();
        var navPage = MainPage as NavigationPage;
        if (navPage == null) return;

        // 根据传递的参数判断要跳转的页面,举个例子:
        if (returnData == "Dummy data")
        {
            // 避免重复导航到同一个页面
            if (navPage.CurrentPage is not MeetingDetailPage)
            {
                // 如果需要传业务参数,比如会议ID,提前序列化存在ReturningData里,这里反序列化后传入即可
                await navPage.PushAsync(new MeetingDetailPage());
            }
        }
    });
}

3. 常见坑点说明

  • 不要在ReturningData里传递复杂实体对象,跨平台序列化容易失败,建议把需要的参数(比如业务ID、页面标识)序列化成JSON字符串传递,点击的时候再反序列化使用。
  • 不要在非主线程执行导航操作,必须包裹在Device.BeginInvokeOnMainThread里,否则会触发UI线程异常。
  • 测试时不要用VS的热重载功能,热重载会导致全局事件注册失效,全量编译部署后再测试通知点击逻辑。
  • 如果是Android 13及以上版本,记得在AndroidManifest里加POST_NOTIFICATIONS权限,并且运行时动态申请,否则通知弹不出来。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.26 14:48:21