「Firebase 服务列表」
名称 内容
A/B Testing 轻松运行并分析产品和营销测试
Analytics 应用分析功能
App Check 为应用程序数据提供保护
App Distribution 将应用程序分发到测试人员
Firebase Authentication 易于建立的用户认证
Cloud Firestore NoSQL 数据库构建无服务器
Cloud Functions for Firebase 无服务器运行后端代码
Firebase Cloud Messaging 发送和接收推送消息
Firebase Crashlytics 跟踪应用稳定性问题
Dynamic Links 提供对本机应用程序链接内容的直接导航
Firebase Extensions Firebase 扩展
Firebase Hosting 网站部署
Firebase In-App Messaging 发送有针对性的上下文消息
Firebase ML 为应用程序提供机器学习功能
Firebase Performance Monitoring 获取性能分析
Firebase Realtime Database 可以保存为 JSON 格式的数据库
Firebase Remote Config 允许功能的动态变化
Cloud Storage for Firebase 保存用户创建的内容
Test lab 在虚拟设备上验证您的应用
将 Firebase 添加到您的 Flutter 应用
官方文档 https://firebase.google.com/docs/flutter/setup?platform=ios
第 1 步:安装所需的命令行工具
- 如果您尚未安装 Firebase CLI,请先安装。
- 运行以下命令,使用您的 Google 帐号登录 Firebase:
$ firebase login
- 从任何目录运行以下命令来安装 FlutterFire CLI:
$ dart pub global activate flutterfire_cli
- 添加到shell的配置文件(.bashrc, (. bash_profile等): Warning: Pub installs
executables into $HOME/.pub-cache/bin, which is not on your path.
export PATH="$PATH":"$HOME/.pub-cache/bin"
第 2 步:将应用配置为使用 Firebase(单个环境)
使用 FlutterFire CLI 将您的 Flutter 应用配置为连接到 Firebase。从 Flutter 项目目录运行以下命令,启动应用配置工作流:
$ flutterfire configure
在命令行中选取或创建firebase控制台中的项目
➜ flutter_app_firebase flutterfire configure
i Found 18 Firebase projects.
✔ Select a Firebase project to configure your Flutter application with · flutter-demo (FLUTTER-DEMO)
? Which platforms should your configuration support (use arrow keys & space to s✔ Which platforms should your configuration support (use arrow keys & space to select)? · android, ios
i Firebase android app com.example.flutter_app_firebase is not registered on Firebase project flutter-demo.
i Registered a new Firebase android app on Firebase project flutter-demo.
i Firebase ios app com.example.flutterAppFirebase is not registered on Firebase project flutter-demo.
i Registered a new Firebase ios app on Firebase project flutter-demo.
? The files android/build.gradle & android/app/build.gradle will be updated to apply Firebase configuration and gradle build plugins. Do you want to continue? (✔ The files android/build.gradle & android/app/build.gradle will be updated to apply Firebase configuration and gradle build plugins. Do you want to continue? · yes
Firebase configuration file lib/firebase_options.dart generated successfully with the following Firebase apps:
Platform Firebase App Id
android 1:203356949376:android:2cd560f57a534d06e54f65
ios 1:203356949376:ios:7f2181a137b3a4fbe54f65
Learn more about using this file and next steps from the documentation:
> https://firebase.google.com/docs/flutter/setup
➜ flutter_app_firebase
第 3 步:在您的应用中初始化 Firebase
在您的 Flutter 项目目录中,运行以下命令以安装核心插件:
$ flutter pub add firebase_core
在 Flutter 项目目录中运行以下命令,以确保您的 Flutter 应用的 Firebase 配置保持最新状态:
$ flutterfire configure
在 lib/main.dart 文件中,导入您之前生成的 Firebase 核心插件和配置文件:
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';
同样在 lib/main.dart 文件中,使用配置文件导出的 DefaultFirebaseOptions 对象初始化 Firebase:
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
runApp(const MyApp());
}
重新构建 Flutter 应用:
$ flutter run
第 4 步:添加 Firebase 插件
您可以通过各种 Firebase Flutter 插件(每个插件对应一种 Firebase 产品,例如 Cloud Firestore、Authentication、Analytics 等)在 Flutter 应用中访问 Firebase。
添加 Firebase Flutter 插件的方法如下:
在您的 Flutter 项目目录中,运行以下命令:
$ flutter pub add PLUGIN_NAME
在您的 Flutter 项目目录中,运行以下命令:
$ flutterfire configure
运行此命令可确保您的 Flutter 应用的 Firebase 配置保持最新状态,并且对于 Android 版 Crashlytics 和 Performance Monitoring,会将所需的 Gradle 插件添加到您的应用中。
完成后,重新构建您的 Flutter 项目:
$ flutter run
通过 Analytics 试用示例应用
官方文档 https://firebase.google.com/docs/analytics/get-started?platform=flutter
Analytics调试事件
https://firebase.google.com/docs/analytics/debugview
安卓:处于调试模式,控制台执行命令
adb shell setprop debug.firebase.analytics.app <软件包名称>
注意事项:
1、adb shell setprop debug.firebase.analytics.app 与 <软件包名称> 中间有空格
2、adb shell setprop debug.firebase.analytics.app .none. 此语句直接直接copy执行就行(".none.“中的”."是需要的,不用有疑虑)
iOS:处于调试模式 (*需要用xcode运行才能连接到设备,可以使用模拟器)
1、在Xcode里Product—>Scheme—>Edit Scheme—>Run—>Arguments中加入配置
打开控制台日志
-FIRDebugEnabled
关闭控制台日志
-FIRDebugDisabled
或者
2、打开DebugView模式(iOS),实时调试
let resourceName = "GoogleService-Info"
let urlScheme = "new-appname-dev"
var newArguments = ProcessInfo.processInfo.arguments
newArguments.append("-FIRDebugEnabled")
ProcessInfo.processInfo.setValue(newArguments, forKey: "arguments")
Firebase Crashlytics试用示例应用
官方文档 https://firebase.google.com/docs/crashlytics/get-started?platform=flutter
启用调试日志记录 https://firebase.google.com/docs/crashlytics/test-implementation?platform=flutter#enable-debug-logging
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
runZonedGuarded<Future<void>>(() async {
FlutterError.onError = FirebaseCrashlytics.instance.recordFlutterFatalError;
runApp(const MyApp());
},(error, stack) =>
FirebaseCrashlytics.instance.recordError(error, stack, fatal: true));
}
写一个崩溃代码
TextButton(
onPressed: () => throw Exception(),
child: const Text("Throw Test Exception"),
),
云消息传递FCM试用示例应用
官方文档 https://firebase.google.com/docs/cloud-messaging/flutter/client?authuser=0&hl=zh
控制台配置
在 Firebase 控制台的项目中,选择齿轮图标,选择Project Settings ,然后选择Cloud Messaging选项卡。
iOS+
xcode添加启用后台获取和远程通知模式 https://developer.apple.com/documentation/xcode/configuring-background-execution-modes
上传您的 APNs 身份验证密钥:KeyID、TeamID、P8文件(文件buildID对应控制台应用包ID)
安卓
发送测试消息
https://firebase.google.com/docs/cloud-messaging/flutter/first-message
Firebase Dynamic Links
控制台创建Dynamic Links:https://firebase.google.com/docs/dynamic-links/flutter/create
1.添加网址前缀
2.创建动态链接
3.添加深层链接
4.定义链接行为,(在您的 Apple 应用中打开深层链接)需要添加您的 App Store ID 和团队 ID。
接收动态链接:https://firebase.google.com/docs/dynamic-links/flutter/receive
1.安卓Firebase 控制台的项目设置页面,并确保您已指定 SHA-1 签名密钥。如果您使用 App Links,请同时指定您的 SHA-256 密钥。
2.在 Apple Developer 网站上,为您的应用创建一个启用了关联域功能的配置文件。
在 Signing & Capabilities 页面上,确保您的 Team 已注册,并且您的 Provisioning Profile 已设置。
flutter中多flavors方案添加firebase
https://www.modb.pro/db/464569
创建flavors对应的应用和firebase_options_stg.dart文件,更改google-service文件
flutterfire config \
--project=bank-dev \
--out=lib/firebase/dev/firebase_options.dart \
--ios-bundle-id=jp.co.bank.minefocus.dev \
--android-app-id=jp.co.bank.minefocus.dev
flutterfire config \
--project=bank-stg \
--out=lib/firebase/stg/firebase_options.dart \
--ios-bundle-id=jp.co.bank.name.stg \
--android-app-id=jp.co.bank.name.stg
创建flavors对应的应用不生成firebase_app_id_file.json文件和GoogleService-Info.plist文件
flutterfire configure -i jp.co.bank.name.dev \
-a jp.co.bank.name.dev \
-o lib/firebase/dev/firebase_options.dart \
--no-apply-gradle-plugins \
--no-app-id-json
flutterfire configure -i jp.co.bank.name.stg \
-a jp.co.bank.name.stg \
-o lib/firebase/stg/firebase_options.dart \
--no-apply-gradle-plugins \
--no-app-id-json
配置多flavors方案(dev、stg、uat、pro)
https://github.com/VeryGoodOpenSource/very_good_cli
优秀的教程
https://sebastien-arbogast.com/2022/05/02/multi-environment-flutter-projects-with-flavors/#Integrating_Firebase
包含Analytics和Crashlytics的实现方案
1、配置dev环境
firebase_core: ^1.24.0
firebase_analytics: ^9.3.8
firebase_messaging: 11.2.5
firebase_crashlytics: ^2.8.13
await Firebase.initializeApp(
name: 'flutter-demo',
options: DefaultFirebaseOptions.currentPlatform,
);
runZonedGuarded<Future<void>>(() async {
FlutterError.onError = FirebaseCrashlytics.instance.recordFlutterFatalError;
runApp(const MyApp());
},(error, stack) =>
FirebaseCrashlytics.instance.recordError(error, stack, fatal: true));
flutterfire config \
--project=flutter-demo \
--out=lib/firebase/dev/firebase_options.dart \
--ios-bundle-id=jp.co.bank.name.dev \
--android-app-id=jp.co.bank.name.dev
2、配置stg环境
firebase_core: ^1.24.0
firebase_analytics: ^9.3.8
firebase_messaging: 11.2.5
firebase_crashlytics: ^2.8.13
await Firebase.initializeApp(
name: 'bank-stg',
options: DefaultFirebaseOptions.currentPlatform,
);
runZonedGuarded<Future<void>>(() async {
FlutterError.onError = FirebaseCrashlytics.instance.recordFlutterFatalError;
runApp(const MyApp());
},(error, stack) =>
FirebaseCrashlytics.instance.recordError(error, stack, fatal: true));
flutterfire config \
--project=bank-stg \
--out=lib/firebase/stg/firebase_options.dart \
--ios-bundle-id=jp.co.bank.name.stg \
--android-app-id=jp.co.bank.name.stg
3、使用cp -f命令,根据环境更改本地文件
cp -f ios/Runner/SupportingFiles/GoogleService-Info-dev.plist ios/Runner/GoogleService-Info.plist
cp -f ios/firebase_app_id_file_dev.json ios/firebase_app_id_file.json
cp -f ios/Runner/SupportingFiles/GoogleService-Info-stg.plist ios/Runner/GoogleService-Info.plist
cp -f ios/firebase_app_id_file_stg.json ios/firebase_app_id_file.json
4、安卓可将google-services.json放到android/app/src/dev环境对应的文件夹下,不用命令切换。
报错1、 FirebaseApp name [DEFAULT] already exists!
问题是由于没有添加应用名称,采用默认 [DEFAULT]名称
可能原因:
1、对应的google-service文件检查是否添加。
解决方法:
加上应用名称即可,如下:
FirebaseApp.initializeApp(options,appName);
2.import ‘firebase_options.dart’; 是否和flavors对应。
3.是否配置好flavors对应的google-service
可能原因:
2、不同环境下添加initializeApp()方法,并没有添加参数name:区分应用名称
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
解决方法:
参数name:区分应用名称
await Firebase.initializeApp(
name: ‘flutter-demo-82860’,
options: DefaultFirebaseOptions.currentPlatform,
);
报错2、
Execution failed for task ‘:app:uploadCrashlyticsMappingFileDebug’
Execution failed for task ‘:app:uploadCrashlyticsMappingFileRelease’
原因:
是因为打release包的时候,Firebase Crashlytics SDK需要将项目混淆后的Mapping等文件上传到Google的服务器,然后出现崩溃后我们可以直接在线上拿到正常的崩溃日志,而不是混淆后的日志。
然而问题就出在上传Mapping文件到Google服务器这一步,这里你就必须要科学上网了。
然而当你开启科学上网的软件后,如果不在项目中设置相应的代理信息,编译还是会出错,所以我们还需要在项目的gradle.properties中进行相应的设置:
// 设置是否要自动上传
firebaseCrashlytics {
mappingFileUploadEnabled true
}
自动上传设置false可以跳过网络问题测试其他功能。
mappingFileUploadEnabled false
报错3、
APNS device token not set before retrieving FCM Token for Sender ID ‘3827790864566’. Notifications to this FCM Token will not be delivered over APNS.Be sure to re-retrieve the FCM token once the APNS device token is set
解决方法
1.模拟器上没有APNS令牌,因此映射失败。真机运行。
2.确认GoogleService-Info.plist 文件与测试环境(dev)是否一致。
3.确认服务端与客户端的device token是否一致。