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

MAUI Blazor安卓端最小化后点击FCM推送通知闪屏卡顿求助

解决MAUI Blazor安卓端最小化后点击FCM推送卡顿问题

针对安卓端应用最小化后点击FCM推送出现紫色闪屏、卡顿无法加载的问题,可从以下几个方向排查修复:

  • 调整Activity启动模式
    确保MainActivity的启动模式设置为SingleTop或SingleTask,避免重复创建Activity实例引发冲突。修改MainActivity的属性配置:

    [Activity(LaunchMode = LaunchMode.SingleTop, 
             Theme = "@style/Maui.SplashTheme", 
             MainLauncher = true)]
    public class MainActivity : MauiAppCompatActivity
    {
        // 原有代码
    }
    
  • 修正推送Intent的Flags配置
    在构建FCM通知的PendingIntent时,添加正确的Flags,确保复用已存在的Activity栈:

    var intent = new Intent(this, typeof(MainActivity));
    intent.AddFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop);
    // 附加推送内容到Intent
    intent.PutExtra("push_payload", notificationData);
    
    var pendingIntent = PendingIntent.GetActivity(
        this, 
        0, 
        intent, 
        PendingIntentFlags.UpdateCurrent | PendingIntentFlags.Immutable
    );
    

    这样点击通知时会直接唤醒后台已有的Activity实例,而非启动新实例,避免生命周期混乱导致的卡顿。

  • 正确处理后台唤醒的Intent数据
    重写MainActivity的OnNewIntent方法,在应用从后台唤醒时处理推送数据,避免阻塞UI线程:

    protected override void OnNewIntent(Intent intent)
    {
        base.OnNewIntent(intent);
        if (intent.HasExtra("push_payload"))
        {
            var payload = intent.GetStringExtra("push_payload");
            // 通过MessagingCenter或弱引用将数据传递给Blazor组件
            MessagingCenter.Send<MainActivity, string>(this, "PushReceived", payload);
        }
    }
    

    同时在Blazor组件中订阅消息,确保数据处理逻辑在主线程执行,不会阻塞UI加载。

  • 检查闪屏配置
    验证安卓闪屏资源是否正确配置,避免闪屏加载耗时导致卡顿。查看Platforms/Android/Resources/drawable/splash_screen.xml,确保配置简洁无冗余:

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@color/splash_background" />
        <item>
            <bitmap
                android:gravity="center"
                android:src="@mipmap/appicon" />
        </item>
    </layer-list>
    
  • 排查后台资源占用
    在MainActivity的OnResume方法中添加日志,追踪应用唤醒时的执行流程,排查是否有未释放的资源或阻塞的后台任务:

    protected override void OnResume()
    {
        base.OnResume();
        Log.Debug("MAUI_APP", "应用从后台唤醒");
        // 检查并释放不必要的后台资源
    }
    

    通过日志定位卡顿的具体环节,比如BlazorWebView初始化是否出现阻塞。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 17:37:09