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

.NET MAUI Android Firebase推送通知后台/关闭状态处理问题

.NET MAUI Android: 后台/关闭状态下推送通知处理及OnNewIntent未触发问题

一、OnNewIntent未触发的核心解决步骤

1. 配置MainActivity启动模式

确保MainActivity的启动模式设置为SingleTop,否则点击通知会创建新的Activity实例,不会触发OnNewIntent。修改MainActivity.cs的特性:

[Activity(Theme = "@style/Maui.SplashTheme", 
          MainLauncher = true, 
          ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density,
          LaunchMode = LaunchMode.SingleTop)]
public class MainActivity : MauiAppCompatActivity

2. 正确构建推送通知的PendingIntent

在自定义的FirebaseMessagingService中,构建通知时必须将推送数据附加到Intent,并设置正确的PendingIntent标记(Android 12+需添加Immutable):

public class MyFirebaseMessagingService : FirebaseMessagingService
{
    private const string CHANNEL_ID = "MyPushChannel";

    public override void OnMessageReceived(RemoteMessage remoteMessage)
    {
        // 处理Data数据(仅Data负载会在后台/关闭状态触发此方法)
        if (remoteMessage.Data.Count > 0)
        {
            // 可在此提前处理数据逻辑
        }

        // 构建跳转Intent并附加数据
        Intent intent = new Intent(this, typeof(MainActivity));
        intent.AddFlags(ActivityFlags.ClearTop);
        foreach (var key in remoteMessage.Data.Keys)
        {
            intent.PutExtra(key, remoteMessage.Data[key]);
        }

        // 设置PendingIntent标记,确保Intent数据被更新
        PendingIntent pendingIntent = PendingIntent.GetActivity(
            this, 
            0, 
            intent, 
            PendingIntentFlags.UpdateCurrent | PendingIntentFlags.Immutable);

        // 构建并显示通知
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
            .SetContentTitle(remoteMessage.GetNotification()?.Title ?? "新通知")
            .SetContentText(remoteMessage.GetNotification()?.Body ?? "您有一条新消息")
            .SetSmallIcon(Resource.Drawable.ic_notification)
            .SetContentIntent(pendingIntent)
            .SetAutoCancel(true);

        NotificationManagerCompat.From(this).Notify(new Random().Next(), builder.Build());
    }
}

3. 正确重写OnNewIntent方法

在MainActivity中重写OnNewIntent,必须调用base.OnNewIntent(intent),再提取Intent中的数据:

protected override void OnNewIntent(Intent intent)
{
    base.OnNewIntent(intent);
    // 处理推送携带的数据
    if (intent.Extras != null)
    {
        foreach (string key in intent.Extras.KeySet())
        {
            string value = intent.Extras.GetString(key);
            // 这里添加自定义数据处理逻辑
        }
    }
}

二、后台/关闭状态下推送接收不稳定问题

1. 优先使用仅Data负载

当应用处于后台或完全关闭时,Firebase的Notification负载由系统托盘直接处理,不会触发OnMessageReceived。若要确保数据被处理,需发送仅Data负载,此时系统会将消息传递给FirebaseMessagingService,无论应用状态如何。

2. 检查权限与Manifest配置

确保AndroidManifest.xml中包含必要权限并正确注册FirebaseMessagingService:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

<service
    android:name=".MyFirebaseMessagingService"
    android:exported="false">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>

注意:Android 13+需在运行时请求POST_NOTIFICATIONS权限,否则无法显示通知。

3. 排除厂商电池优化限制

部分Android厂商的电池优化会限制后台服务运行,导致推送延迟或丢失。需引导用户将应用加入电池优化白名单。

4. Azure通知中心模板配置

若使用Azure通知中心,确保推送模板配置为Data格式,示例模板:

{"data":{"title":"$(title)","content":"$(content)","customData":"$(customData)"}}

内容的提问来源于stack exchange,提问作者Dimitri van Zyl

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 00:13:12