运行Flutter Driver测试时如何禁用推送通知提示

问题描述

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

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

但是,添加推送通知后,由于显示了“推送通知提示,因此启动时我的测试超时。运行Flutter驱动程序测试时如何授予访问权限或跳过提示

解决方法

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

didFinishLaunchingWithOptions的AppDelegate中,我使用了条件标志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)
    }

然后,我在项目的“构建设置”的“ Swift标记”部分中为Debug-development风格设置了此标记,在运行Flutter Driver测试时默认使用该标记:

enter image description here