diff --git a/Assets/FirebaseManager.cs b/Assets/FirebaseManager.cs new file mode 100644 index 00000000..21537cf8 --- /dev/null +++ b/Assets/FirebaseManager.cs @@ -0,0 +1,86 @@ +using UnityEngine; +using Firebase; +using Firebase.Messaging; +using System.Threading.Tasks; + +public class FirebaseManager : MonoBehaviour +{ + private static FirebaseManager _instance; + private static bool _isInitialized = false; + + public static FirebaseManager Instance => _instance; + + void Awake() + { + // Singleton: keep only one instance across scenes + if (_instance != null) + { + Destroy(gameObject); + return; + } + + _instance = this; + DontDestroyOnLoad(gameObject); + } + + async void Start() + { + if (_isInitialized) return; + + _isInitialized = true; + + Debug.Log("Initializing Firebase..."); + await InitializeFirebaseAsync(); + } + + private async Task InitializeFirebaseAsync() + { + var dependencyStatus = await FirebaseApp.CheckAndFixDependenciesAsync(); + if (dependencyStatus != DependencyStatus.Available) + { + Debug.LogError($"Firebase dependency error: {dependencyStatus}"); + return; + } + + Debug.Log("✅ Firebase initialized successfully"); + + // Register for token and message events + FirebaseMessaging.TokenReceived += OnTokenReceived; + FirebaseMessaging.MessageReceived += OnMessageReceived; + + // Get the FCM token manually + var tokenTask = FirebaseMessaging.GetTokenAsync(); + await tokenTask; + + if (tokenTask.IsCompletedSuccessfully) + { + string token = tokenTask.Result; + Debug.Log("🔥 FCM Token: " + token); + + // TODO: Save token to PlayFab here if needed + } + else + { + Debug.LogWarning("❌ Failed to retrieve FCM token"); + } + } + + private void OnTokenReceived(object sender, TokenReceivedEventArgs token) + { + Debug.Log("📱 Token received from Firebase: " + token.Token); + // Optional: save to PlayFab + } + + private void OnMessageReceived(object sender, MessageReceivedEventArgs e) + { + if (e.Message.Notification != null) + { + Debug.Log($"📨 Push Notification Received: {e.Message.Notification.Title} - {e.Message.Notification.Body}"); + // Optional: Show in-game popup or route to leaderboard + } + else + { + Debug.Log("📨 Data-only message received."); + } + } +} diff --git a/Assets/FirebaseManager.cs.meta b/Assets/FirebaseManager.cs.meta new file mode 100644 index 00000000..62bb6980 --- /dev/null +++ b/Assets/FirebaseManager.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: f92bf5d5cdc3e074e8b67ddb00176fee +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/google-services.json b/Assets/google-services.json index 7a0722a8..bd134edb 100644 --- a/Assets/google-services.json +++ b/Assets/google-services.json @@ -13,14 +13,6 @@ } }, "oauth_client": [ - { - "client_id": "627393855302-c24ki5dspbaoetlldhtq118pdufcm0ku.apps.googleusercontent.com", - "client_type": 1, - "android_info": { - "package_name": "com.rizze.pipuzzle", - "certificate_hash": "c15e8bb32af9d00af84b991c7063196b01eee2be" - } - }, { "client_id": "627393855302-ie99j5loumrkt3gqi5mopa1c1avu7o86.apps.googleusercontent.com", "client_type": 3