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

Flutter中应用终止时点击本地通知的处理方法

解决flutter_local_notifications在应用终止后点击通知不触发回调的问题

应用处于终止状态时,点击通知的处理逻辑和后台状态不同——此时Flutter引擎尚未启动,需要原生端先捕获通知点击事件,再将数据传递给Flutter侧的插件回调。针对Android和iOS平台,需要分别补充配置:

Android 端配置

  1. 修改AndroidManifest.xml
    给MainActivity添加singleTop启动模式,并配置通知点击的Intent过滤器,确保通知点击能正确路由到Activity:

    <activity
        android:name=".MainActivity"
        android:launchMode="singleTop"
        android:exported="true"
        ...>
        <intent-filter>
            <action android:name="FLUTTER_NOTIFICATION_CLICK" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    
  2. 在MainActivity中处理通知Intent
    重写onNewIntent方法(Kotlin示例),捕获通知点击的Intent并传递给插件:

    import com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin
    import com.dexterous.flutterlocalnotifications.models.NotificationResponse
    
    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        if (intent.action == "FLUTTER_NOTIFICATION_CLICK") {
            val payload = intent.getStringExtra("payload")
            val actionId = intent.getStringExtra("actionId")
            val input = intent.getStringExtra("input")
            
            FlutterLocalNotificationsPlugin().handleNotificationResponse(
                NotificationResponse(
                    payload = payload,
                    actionId = actionId,
                    input = input
                )
            )
        }
    }
    
  3. 创建通知时配置启动模式
    生成Android通知时,在AndroidNotificationDetails中指定launchMode为AndroidLaunchMode.SingleTop,避免重复创建Activity:

    final AndroidNotificationDetails androidDetails = AndroidNotificationDetails(
      'channel_id',
      'channel_name',
      launchMode: AndroidLaunchMode.SingleTop,
      // 其他配置...
    );
    

iOS 端配置

在AppDelegate.swift中处理应用启动时的通知参数,手动触发插件的回调:

import flutter_local_notifications

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // 检查启动是否来自通知点击
    if let notificationDict = launchOptions?[.remoteNotification] as? [String: Any] {
        let payload = notificationDict["payload"] as? String
        // 构造通知响应对象并传递给插件
        let response = UNNotificationResponse(
            notification: UNNotification(
                request: UNNotificationRequest(
                    identifier: "",
                    content: UNMutableNotificationContent(),
                    trigger: nil
                ),
                date: Date()
            ),
            actionIdentifier: UNNotificationDefaultActionIdentifier,
            userText: nil
        )
        FlutterLocalNotificationsPlugin.instance.handleNotificationResponse(response)
    }
    return true
}

Flutter 端补充说明

你的初始化代码中已经设置了onDidReceiveNotificationResponse和behaviorSubject来接收payload,补充完原生端配置后,应用终止状态下点击通知时,原生会将payload传递给插件,触发该回调,behaviorSubject也能正常接收数据。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 21:22:01