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

如何在Flutter中实现从系统设置跳转至应用内通知设置?

可以在Flutter中实现该功能

下面分Android和iOS平台说明具体实现方案,同时结合你正在使用的AwesomeNotifications和FCM做适配:

Android 端实现

1. 配置AndroidManifest.xml

在主MainActivity的配置中添加两个intent-filter,分别接收系统通知设置的跳转请求,以及自定义的跳转触发:

<activity
    android:name=".MainActivity"
    android:exported="true">
    <!-- 原有配置保留 -->
    <!-- 接收系统APP通知设置的跳转意图 -->
    <intent-filter>
        <action android:name="android.settings.APP_NOTIFICATION_SETTINGS" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:package="${applicationId}" />
    </intent-filter>
    <!-- 自定义action,用于精准触发APP内设置跳转 -->
    <intent-filter>
        <action android:name="com.your_package_name.NOTIFICATION_IN_APP_SETTINGS" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

注意替换com.your_package_name为你的APP实际包名。

2. 在Android原生层捕获跳转意图

修改MainActivity.kt,在onNewIntent方法中捕获上述Intent,并通过MethodChannel通知Flutter端跳转:

class MainActivity : FlutterActivity() {
    private val CHANNEL = "com.your_package_name/notification_settings"

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        intent?.let {
            if (it.action == "android.settings.APP_NOTIFICATION_SETTINGS" ||
                it.action == "com.your_package_name.NOTIFICATION_IN_APP_SETTINGS") {
                MethodChannel(flutterEngine?.dartExecutor?.binaryMessenger, CHANNEL)
                    .invokeMethod("openInAppSettings", null)
            }
        }
    }
}

3. Flutter端处理跳转

在你的主页面或初始化逻辑中,监听MethodChannel的调用,跳转到APP内的通知设置页面:

final MethodChannel _notificationChannel = MethodChannel('com.your_package_name/notification_settings');

@override
void initState() {
    super.initState();
    _notificationChannel.setMethodCallHandler((call) async {
        if (call.method == 'openInAppSettings') {
            // 跳转到你的自定义通知设置页面
            Navigator.push(
                context,
                MaterialPageRoute(builder: (_) => const NotificationSettingsPage()),
            );
        }
    });
}

4. 适配AwesomeNotifications

初始化AwesomeNotifications时,确保通知渠道配置正确,系统会自动关联到你的APP通知设置入口:

await AwesomeNotifications().initialize(
    null, // 图标路径,按需设置
    [
        NotificationChannel(
            channelKey: 'default_channel',
            channelName: '默认通知',
            channelDescription: '基础通知渠道',
            importance: NotificationImportance.High,
            ledColor: Colors.white,
        )
    ],
);

iOS 端实现

1. 配置Info.plist

添加自定义URL Scheme,用于系统跳转回APP:

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>your_app_scheme</string>
        </array>
    </dict>
</array>

替换your_app_scheme为你自定义的Scheme(比如myapp-notifications)。

2. 原生层处理URL跳转

修改AppDelegate.swift,捕获指定Scheme的URL请求,通知Flutter端跳转:

import UIKit
import Flutter
import UserNotifications

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
    private let CHANNEL = "com.your_package_name/notification_settings"

    override func application(
        _ app: UIApplication,
        open url: URL,
        options: [UIApplication.OpenURLOptionsKey : Any] = [:]
    ) -> Bool {
        if url.scheme == "your_app_scheme" && url.host == "notifications" {
            guard let controller = window?.rootViewController as? FlutterViewController else { return false }
            let channel = FlutterMethodChannel(name: CHANNEL, binaryMessenger: controller.binaryMessenger)
            channel.invokeMethod("openInAppSettings", arguments: nil)
            return true
        }
        return super.application(app, open: url, options: options)
    }

    override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // 请求通知权限并设置代理
        UNUserNotificationCenter.current().delegate = self
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { _, _ in }
        application.registerForRemoteNotifications()
        GeneratedPluginRegistrant.register(with: self)
        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
    }
}

3. Flutter端同Android逻辑

复用Android端的MethodChannel监听代码,接收到调用后跳转到内部通知设置页面即可。

FCM适配说明

FCM的集成不会干扰该跳转逻辑,只要确保你的APP正常注册FCM并接收通知,系统通知设置入口会正常显示,点击后会触发上述的跳转逻辑。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 05:38:14