名称: Firebase移动端 描述: 为移动应用提供Firebase后端服务集成 版本: 1.0.0 类别: 后端服务 标识符: firebase-mobile 状态: 活跃
Firebase移动端技能
概述
此技能为移动应用程序提供Firebase后端服务集成。它支持配置身份验证、Firestore数据库、云存储、云函数和其他Firebase服务。
允许使用的工具
bash- 执行Firebase CLI命令read- 分析Firebase配置write- 生成安全规则和配置edit- 更新Firebase实现glob- 搜索Firebase文件grep- 搜索模式
能力
Firebase身份验证
-
身份验证方法
- 电子邮件/密码身份验证
- OAuth提供商(Google、Apple、Facebook)
- 手机身份验证
- 匿名身份验证
- 自定义令牌身份验证
-
身份验证状态
- 处理身份验证状态变更
- 令牌刷新
- 会话持久化
- 多因素身份验证
Cloud Firestore
-
数据库操作
- 文档增删改查操作
- 集合查询
- 实时监听器
- 批量操作
- 事务
-
安全规则
- 编写Firestore规则
- 使用模拟器测试规则
- 处理基于角色的访问
- 验证数据结构
Firebase云存储
- 文件操作
- 带进度上传文件
- 下载文件
- 生成下载URL
- 处理元数据
- 配置安全规则
云函数
- 函数集成
- 调用HTTPS函数
- 调用可调用函数
- 处理函数响应
- 配置超时
远程配置
- 功能开关
- 获取远程配置
- 配置默认值
- 处理获取间隔
- A/B测试设置
性能监控
- 性能追踪
- 自动追踪
- 自定义追踪
- 网络请求监控
- 屏幕渲染指标
目标流程
firebase-backend-integration.js- Firebase集成firebase-cloud-messaging.js- 推送通知mobile-analytics-setup.js- 分析
依赖项
- Firebase SDK
- Firebase CLI
- Google Cloud账户
使用示例
Firebase设置(iOS)
// AppDelegate.swift
import FirebaseCore
import FirebaseAuth
import FirebaseFirestore
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
return true
}
}
// AuthService.swift
class AuthService: ObservableObject {
@Published var user: User?
private var handle: AuthStateDidChangeListenerHandle?
init() {
handle = Auth.auth().addStateDidChangeListener { [weak self] _, user in
self?.user = user
}
}
func signIn(email: String, password: String) async throws {
try await Auth.auth().signIn(withEmail: email, password: password)
}
func signUp(email: String, password: String) async throws {
try await Auth.auth().createUser(withEmail: email, password: password)
}
func signOut() throws {
try Auth.auth().signOut()
}
}
Firestore仓库(Android)
// data/repository/PostRepository.kt
class PostRepository @Inject constructor(
private val firestore: FirebaseFirestore
) {
private val postsCollection = firestore.collection("posts")
fun observePosts(): Flow<List<Post>> = callbackFlow {
val listener = postsCollection
.orderBy("createdAt", Query.Direction.DESCENDING)
.addSnapshotListener { snapshot, error ->
if (error != null) {
close(error)
return@addSnapshotListener
}
val posts = snapshot?.documents?.mapNotNull { it.toObject<Post>() } ?: emptyList()
trySend(posts)
}
awaitClose { listener.remove() }
}
suspend fun createPost(post: Post): String {
val docRef = postsCollection.add(post).await()
return docRef.id
}
suspend fun updatePost(postId: String, updates: Map<String, Any>) {
postsCollection.document(postId).update(updates).await()
}
suspend fun deletePost(postId: String) {
postsCollection.document(postId).delete().await()
}
}
安全规则
// firestore.rules
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
function isAuthenticated() {
return request.auth != null;
}
function isOwner(userId) {
return isAuthenticated() && request.auth.uid == userId;
}
match /users/{userId} {
allow read: if isAuthenticated();
allow write: if isOwner(userId);
}
match /posts/{postId} {
allow read: if true;
allow create: if isAuthenticated()
&& request.resource.data.authorId == request.auth.uid;
allow update, delete: if isOwner(resource.data.authorId);
}
}
}
Firebase模拟器
# 启动模拟器
firebase emulators:start
# 在代码中使用模拟器
if ProcessInfo.processInfo.environment["USE_FIREBASE_EMULATOR"] == "YES" {
Auth.auth().useEmulator(withHost: "localhost", port: 9099)
Firestore.firestore().useEmulator(withHost: "localhost", port: 8080)
Storage.storage().useEmulator(withHost: "localhost", port: 9199)
}
质量门限
- 使用模拟器测试安全规则
- 验证身份验证流程
- 测试离线持久化
- 全面的错误处理
相关技能
push-notifications- FCM集成mobile-analytics- Firebase分析mobile-security- 安全模式
版本历史
- 1.0.0 - 初始版本