---
title: "Firebase apps"
description: "Tích hợp Firebase với Adapty để nâng cao phân tích người dùng và theo dõi gói đăng ký cho ứng dụng di động của bạn."
---

Trang này hướng dẫn cách tích hợp Adapty vào ứng dụng của bạn nếu ứng dụng đang chạy trên Firebase.

:::note
Bắt đầu

Đây không phải là tất cả các bước cần thiết để Adapty hoạt động, chỉ là một số mẹo hữu ích cho việc tích hợp với Firebase. Nếu bạn muốn tích hợp Adapty vào ứng dụng của mình, hãy đọc [Hướng dẫn Quickstart](quickstart) trước.
:::

## Xác định người dùng \{#user-identification\}

Nếu bạn đang dùng Firebase auth, đoạn code này có thể giúp bạn đồng bộ người dùng giữa Firebase và Adapty. Lưu ý rằng đây chỉ là ví dụ, và bạn cần cân nhắc đặc thù xác thực của ứng dụng mình.

<Tabs groupId="current-os" queryString>
<TabItem value="swift" label="iOS with Firebase" default>
```swift showLineNumbers
 
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
 
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Configure Adapty before Firebase
        Adapty.activate("YOUR_API_KEY")
        Adapty.delegate = self
        
        // Configure Firebase
        FirebaseApp.configure()
        
        // Add state change listener for Firebase Authentication
        Auth.auth().addStateDidChangeListener { (auth, user) in
            if let uid = user?.uid {
                // identify Adapty SDK with new Firebase user
                Adapty.identify(uid) { error in
                    if let e = error {
                        print("Sign in error: \(e.localizedDescription)")
                    } else {
                        print("User \(uid) signed in")
                    }
                }
            }
        }
 
        return true
    }
 
}
 
extension AppDelegate: AdaptyDelegate {
 
    // MARK: - Adapty delegate
    func didReceiveUpdatedPurchaserInfo(_ purchaserInfo: PurchaserInfoModel) {
        // You can optionally post to the notification center whenever
        // purchaser info changes.
 
        // You can subscribe to this notification throughout your app
        // to refresh tableViews or change the UI based on the user's
        // subscription status
        
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "com.Adapty.PurchaserInfoUpdatedNotification"), object: purchaserInfo)
    }
 
}
```
</TabItem>
<TabItem value="kotlin" label="Android with Firebase" default>
```kotlin showLineNumbers
class App : Application() {

    override fun onCreate() {
        super.onCreate()
        // Configure Adapty
        Adapty.activate(this, "YOUR_API_KEY")

        Adapty.setOnPurchaserInfoUpdatedListener(object : OnPurchaserInfoUpdatedListener {
            override fun onPurchaserInfoReceived(purchaserInfo: PurchaserInfoModel) {
                // handle any changes to subscription state
            }
        })

        // Add state change listener for Firebase Authentication
        FirebaseAuth.getInstance().addAuthStateListener { auth ->
            val currentUserId = auth.currentUser?.uid

            if (currentUserId != null) {
                // identify Adapty SDK with new Firebase user
                Adapty.identify(currentUserId) { error ->
                    if (error == null) {
                        //success
                    }
                }
            } else {
                Adapty.logout { }
            }
        }
    }
}
```
</TabItem>
</Tabs>