Sign In Register

How can we help you today?

Start a new topic

Problems with FCM

 I have a Unity mobile game project, I want to have a leaderboard among facebook friends. And I want push notifications to be sent to a players friends when they beat their score.

I'm currently trying to get this working in Android.


Ive followed this tutorial of yours


And this firebase tutorial that you link to in the above tutorial


Heres my test code for setting this all up


public class LoginManager : MonoBehaviour
{
    string firebasetoken = null;

    // Awake function from Unity's MonoBehavior
    void Start()
    {
        if (Application.platform == RuntimePlatform.WindowsEditor) return;
        BeginLogin();
    }

    void BeginLogin()
    {
        VisualDebuggerRef.SetTxt("");
        if (!FB.IsInitialized)
        {
            // Initialize the Facebook SDK
            FB.Init(FBInitialiseDone);
            VisualDebuggerRef.AppendTxt("initialising FB");
        }
        else
        {
            FBInitialiseDone();
            VisualDebuggerRef.AppendTxt("FB was already initialised");
        }
    }

    void FBInitialiseDone()
    {
        //VisualDebuggerRef.AppendTxt("Initialised FB");       
        if (FB.IsLoggedIn)
        {
            /*
            FB.LogOut();
            Login();
            */
            VisualDebuggerRef.AppendTxt("was already logged in to FB");
            LoginToGameSparks();
        }
        else
            LoginToFB();
    }

    void LoginToFB()
    {
        var perms = new List<string>() { "public_profile", /*"email",*/ "user_friends" };
        FB.LogInWithReadPermissions(perms, AuthCallback);
    }

    private void AuthCallback(ILoginResult result)
    {
        if (FB.IsLoggedIn)
        {
            VisualDebuggerRef.SetTxt("logged in");
            LoginToGameSparks();
        }
        else
        {
            VisualDebuggerRef.SetTxt("Didnt log in");
        }
    }

    void LoginToGameSparks()
    {
        if (!GameSparks.Core.GS.Authenticated) AuthenticateWithGameSparks(AccessToken.CurrentAccessToken.TokenString);
        else
        {
            VisualDebuggerRef.AppendTxt("GS already authenticated");
            LoginComplete();
        }
    }

    void AuthenticateWithGameSparks(string fbtoken)
    {
        VisualDebuggerRef.AppendTxt("making GS auth request");

        new GameSparks.Api.Requests.FacebookConnectRequest()
            .SetAccessToken(fbtoken)
            .SetSwitchIfPossible(true)
            .Send((response) =>
            {
                if (!response.HasErrors)
                {
                    VisualDebuggerRef.AppendTxt("GS login OK " + response.DisplayName);
                    LoginComplete();
                    //GetInvitefriends();
                }
                else
                {
                    VisualDebuggerRef.AppendTxt("GS LOGIN ERRORS||" + response.Errors.ToString());
                }
            });
    }

    void LoginComplete()
    {
        VisualDebuggerRef.AppendTxt("Login Complete");

        SetupFirebase();
    }

    void SetupFirebase()
    {
        Firebase.Messaging.FirebaseMessaging.TokenReceived += OnFIRETokenReceived;
        Firebase.Messaging.FirebaseMessaging.MessageReceived += OnFIREMessageReceived;
        VisualDebuggerRef.AppendTxt("Firebase Setup");
    }

    public void OnFIRETokenReceived(object sender, Firebase.Messaging.TokenReceivedEventArgs token)
    {
        VisualDebuggerRef.AppendTxt("Received FIREB Registration Token: " /*+ token.Token*/);
        firebasetoken = token.Token;
        SetupPushNotificationWithGameSparks();
    }

    public void OnFIREMessageReceived(object sender, Firebase.Messaging.MessageReceivedEventArgs e)
    {
        VisualDebuggerRef.AppendTxt("FIREB Received a new message from: " + e.Message.From);
    }

    void SetupPushNotificationWithGameSparks()
    {
        new GameSparks.Api.Requests.PushRegistrationRequest()
            .SetDeviceOS("FCM")
            .SetPushId(firebasetoken)
            .Send((response) =>
            {
                if (!response.HasErrors)
                {
                    VisualDebuggerRef.AppendTxt("GS SetupPushNotification OK ");
                }
                else
                {
                    VisualDebuggerRef.AppendTxt("GS SetupPushNotification BAD: " + response.Errors.ToString());
                }
            });
    }
}

 

So First I check if we are logged into facebook and if not do so.

Then I authenticate with Gamesparks using the facebook token.


It works fine up to here.


Then once I've authenticated with Gamesparks I Setup firebase.

Heres where things get really weird,


When "SetupFirebase();" is called this debug message never appears VisualDebuggerRef.AppendTxt("Firebase Setup"); instead the method "AuthenticateWithGameSparks" gets called over and over again... I have no idea ho this is happening unless something gets fundamentally messed up when the program is being compiled.


Also If I call SetupFirebase() by itself in Start and nothing else, nothing happens, I dont get the VisualDebuggerRef.AppendTxt("Firebase Setup"); debug message.


This definitely seems like a problem with firebase. I will ask them also, but I would really appreciate any help / advice ye could offer.


Thanks




1 person has this question
Login to post a comment