firebasemanager script

dev-ali-pushnotifications
Ali Sharoz 3 weeks ago
parent 8d71a90eb6
commit 7c1ca8eff7

@ -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.");
}
}
}

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f92bf5d5cdc3e074e8b67ddb00176fee
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

@ -13,14 +13,6 @@
} }
}, },
"oauth_client": [ "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_id": "627393855302-ie99j5loumrkt3gqi5mopa1c1avu7o86.apps.googleusercontent.com",
"client_type": 3 "client_type": 3

Loading…
Cancel
Save