How to disable push notifications prompt when running Flutter Driver tests

416 Views Asked by At

I run my e2e tests with Flutter Driver. The typical command I use is:

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

However, after adding Push notifications support my tests timeout on launch because the Push Notifications prompt is shown. How to grant access or skip the prompt when running Flutter Driver tests?

1

There are 1 best solutions below

0
On

What I came up with is to use Swift custom flags

In my AppDelegate in didFinishLaunchingWithOptions I used conditional flag 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)
    }

And I set this flag in Other Swift Flags section of Build Settings of the project for the Debug-development flavor which is used by default when running Flutter Driver tests:

enter image description here