首页 > 解决方案 > 运行 Flutter Driver 测试时如何禁用推送通知提示

问题描述

我使用 Flutter Driver 运行我的 e2e 测试。我使用的典型命令是:

flutter drive --flavor=development --target=e2e/instrumented_app.dart --driver=e2e/scenarios/smoke_scenario.dart -d "iPhone 11"

但是,在添加推送通知支持后,我的测试在启动时超时,因为显示了推送通知提示。运行 Flutter Driver 测试时如何授予访问权限或跳过提示?

标签: iosflutterflutterdriver

解决方案


我想出的是使用 Swift 自定义标志

在我的 AppDelegate 中,didFinishLaunchingWithOptions我使用了条件标志SKIP_NOTIFICATIONS_PROMPT

    override func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {
        GeneratedPluginRegistrant.register(with: self)
        
        let brazeApiKey = ""
        Appboy.start(withApiKey: brazeApiKey as! String, in:application, withLaunchOptions:launchOptions)
        
        if #available(iOS 10, *) {
            let center = UNUserNotificationCenter.current()
            center.delegate = self as UNUserNotificationCenterDelegate
            let options: UNAuthorizationOptions = [.alert, .sound, .badge]
            #if SKIP_NOTIFICATIONS_PROMPT
            // skipping permission request for uat test builds
            #else
            center.requestAuthorization(options: options) { (granted, error) in
                Appboy.sharedInstance()?.pushAuthorization(fromUserNotificationCenter: granted)
            }
            #endif
            UIApplication.shared.registerForRemoteNotifications()
        } else {
            let types : UIUserNotificationType = [.alert, .badge, .sound]
            let setting : UIUserNotificationSettings = UIUserNotificationSettings(types:types, categories:nil)
            UIApplication.shared.registerUserNotificationSettings(setting)
            UIApplication.shared.registerForRemoteNotifications()
        }
        
        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
    }

我在项目的 Build Settings 的Other Swift Flags部分设置了这个标志,Debug-development用于运行 Flutter Driver 测试时默认使用的风格:

在此处输入图像描述


推荐阅读