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

Flutter应用通过推送通知打开时最近应用列表重复问题求助

Flutter应用通过推送通知打开时最近应用列表出现重复实例的解决方案

问题分析

按返回键关闭应用时,Activity会被销毁,即便进程可能还存活,任务栈也已清空。此时推送通知触发的Intent如果未配置正确的启动标记,会创建新的任务栈,导致最近应用列表出现两个实例;而按Home键只是将应用后台,任务栈保留,因此打开时能正常复用。

解决方案

1. 调整AndroidManifest.xml配置

将MainActivity的启动模式改为singleTask,移除android:taskAffinity=""(该属性会让Activity脱离默认任务栈,反而引发重复),同时添加推送通知对应的Intent Filter:

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <application
        android:label="Жеке Кабинет Дордой"
        android:name="${applicationName}"
        android:icon="@mipmap/ic_launcher"
        android:usesCleartextTraffic="true">
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:launchMode="singleTask"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
            <!-- 推送通知触发的Intent Filter,替换为你的自定义action -->
            <intent-filter>
                <action android:name="com.yourapp.NOTIFICATION_OPEN"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>
    <queries>
        <intent>
            <action android:name="android.intent.action.PROCESS_TEXT"/>
            <data android:mimeType="text/plain"/>
        </intent>
    </queries>
</manifest>

2. 配置推送通知的Intent标记

无论在Flutter端还是原生端创建推送通知的Intent,都必须添加FLAG_ACTIVITY_CLEAR_TOP | FLAG_ACTIVITY_SINGLE_TOP标记,确保复用现有Activity实例,不创建新任务栈:

Flutter端示例(使用android_intent_plus插件):

import 'package:android_intent_plus/android_intent_plus.dart';

void openAppFromNotification() {
  final intent = Intent(
    action: 'com.yourapp.NOTIFICATION_OPEN',
    flags: [IntentFlag.activityClearTop, IntentFlag.activitySingleTop],
  );
  intent.launch();
}

Android原生端示例(Firebase Messaging处理):

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.setAction("com.yourapp.NOTIFICATION_OPEN");
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(
        this,
        0,
        intent,
        PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
    );

    // 构建并显示通知
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(remoteMessage.getNotification().getTitle())
            .setContentText(remoteMessage.getNotification().getBody())
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setContentIntent(pendingIntent)
            .setAutoCancel(true);

    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
    notificationManager.notify(NOTIFICATION_ID, builder.build());
}

3. 验证效果

  • 打开应用后按返回键关闭
  • 触发推送通知并点击打开
  • 查看最近应用列表,应仅显示一个应用实例

关键说明

  • singleTask模式会确保Activity在同一个任务栈中,无论从主屏幕还是推送打开,都会复用现有实例(如果存在),不存在则创建新实例
  • FLAG_ACTIVITY_CLEAR_TOP会清除目标Activity上方的所有Activity,FLAG_ACTIVITY_SINGLE_TOP会避免创建重复的Activity实例
  • 移除taskAffinity是因为该属性会让Activity脱离应用默认任务栈,导致推送打开时创建新任务栈,进而出现重复实例

内容的提问来源于stack exchange,提问作者Таалайбек Утуров

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 04:42:32