【问题标题】:How to disable push notifications prompt when running Flutter Driver tests运行 Flutter Driver 测试时如何禁用推送通知提示
【发布时间】:2020-09-09 12:17:30
【问题描述】:
我使用 Flutter Driver 运行我的 e2e 测试。我使用的典型命令是:
flutter drive --flavor=development --target=e2e/instrumented_app.dart --driver=e2e/scenarios/smoke_scenario.dart -d "iPhone 11"
但是,在添加推送通知支持后,我的测试在启动时会超时,因为会显示推送通知提示。运行 Flutter Driver 测试时如何授予访问权限或跳过提示?
【问题讨论】:
标签:
ios
flutter
flutterdriver
【解决方案1】:
我想出的是使用 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)
}
我在项目的 Build Settings 的 Other Swift Flags 部分中为 Debug-development 风格设置了这个标志,在运行 Flutter Driver 测试时默认使用: