Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved Firebase initializes, but no token received [Firebase SDK 8.0.0]

Discussion in 'Android' started by realragnvaldr, Jul 12, 2021.

  1. realragnvaldr

    realragnvaldr

    Joined:
    Jul 21, 2019
    Posts:
    40
    EDIT: I solved it by downgrading Unity Firebase SDK 8.0.0 back to 7.2.0

    ----

    I have a problem with Firebase Cloud Messaging (FCM). Everything used to work fine, but since recently my app doesn't receive tokens anymore.

    Initialization of the APIs seems to go fine:

    upload_2021-7-12_13-19-35.png

    ... and the initialization code from the FCM tutorial also seems to be executed normally:

    upload_2021-7-12_13-11-19.png

    upload_2021-7-12_13-19-51.png

    ... but the OnTokenReceived routine (see code above) never gets triggered anymore. I can still send messages to my devices that I managed to register before this problem appeared, though (i'm saving the tokens in a database).

    So, in summary: everything seems fine, except that my app stopped receiving FCM tokens

    I tried a whole bunch of things (updating/reinstalling Firebase SDKs, reimport all assets, android resolver, etc), but no luck so far. I think that the problems started after updating the Google Mobile Ads SDK, but that may be a coincidence.

    I'd be happy to hear any suggestions about:
    1) what could be the problem
    2) how i can further debug this (e.g., is there any way to see in Firebase console when/why it refuses to send a token to an app?)
     
    Last edited: Jul 12, 2021
  2. realragnvaldr

    realragnvaldr

    Joined:
    Jul 21, 2019
    Posts:
    40
    PS. One thing that i did notice is that Firebase Cloud Messaging API gets initialized several times (or at least there are multiple messages about it). Can that be a problem and, if so, how can i avoid this? (This initialization is something that happens automatically - i don't do this anywhere in the code AFAIK)

    upload_2021-7-12_13-46-20.png

    PPS. I also see that I somehow ended up with two installations of some of the Firebase SDKs (7.2.0 and 8.0.0) - can that be a problem (and explain multiple initializations)?

    upload_2021-7-12_13-50-51.png
     
  3. Evst

    Evst

    Joined:
    Jul 28, 2015
    Posts:
    4
    I have the same issue.
    Callback method not work after updated the Firebase SDK from 7.2 to 8.1.
    It works well with iOS at 8.1, but Android not.
     
  4. CengizKuscu

    CengizKuscu

    Joined:
    Oct 24, 2014
    Posts:
    3
    Try FirebaseMessaging.GetTokenAsync();
    Code (CSharp):
    1. public void InitializeFCM()
    2. {
    3. ....
    4.    GetTokenAsync();
    5. }
    6.  
    7. private async void GetTokenAsync()
    8. {
    9.     var task = FirebaseMessaging.GetTokenAsync();
    10.  
    11.     await task;
    12.  
    13.     if (task.IsCompleted)
    14.     {
    15.        Debug.Log("GET TOKEN ASYNC "+ task.Result);
    16.     }
    17. }
    18.  
    or

    Code (CSharp):
    1. public void InitializeFCM()
    2. {
    3. ....
    4.    StartCoroutine(GetTokenAsync());
    5. }
    6.  
    7. private IEnumerator void GetTokenAsync()
    8. {
    9.     var task = FirebaseMessaging.GetTokenAsync();
    10.  
    11.     while(!task.IsCompleted)
    12.          yield return new WaitForEndOfFrame();
    13.  
    14.     Debug.Log("GET TOKEN ASYNC "+ task.Result);
    15. }
     
  5. unity_jlN4F5yeBd5hDg

    unity_jlN4F5yeBd5hDg

    Joined:
    Jan 14, 2021
    Posts:
    3
    Thanks man, it worked!
     
    itsnottme and CengizKuscu like this.
  6. arssarath23

    arssarath23

    Joined:
    Nov 26, 2021
    Posts:
    10
    Hi,
    Firebase Cloud Messaging API Initialized is printing but can any one say how to get token.
    Thanks in advance.
     
  7. arssarath23

    arssarath23

    Joined:
    Nov 26, 2021
    Posts:
    10
    upload_2022-7-28_23-59-59.png
     
  8. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    Did the code just above to get the token not work for you?
     
  9. arssarath23

    arssarath23

    Joined:
    Nov 26, 2021
    Posts:
    10
    No it was not working, please see the below code what i have used....

    public void Start() {
    Firebase.Messaging.FirebaseMessaging.TokenReceived += OnTokenReceived;
    Firebase.Messaging.FirebaseMessaging.MessageReceived += OnMessageReceived;
    }

    public void OnTokenReceived(object sender, Firebase.Messaging.TokenReceivedEventArgs token) {
    UnityEngine.Debug.Log("Received Registration Token: " + token.Token);
    }

    public void OnMessageReceived(object sender, Firebase.Messaging.MessageReceivedEventArgs e) {
    UnityEngine.Debug.Log("Received a new message from: " + e.Message.From);
    }
     
  10. arssarath23

    arssarath23

    Joined:
    Nov 26, 2021
    Posts:
    10
    I created one dummy project only with one button and script mentioned below at that time token generated...

     
  11. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    So did you get it working? It's not clear. So it's working in one project, and the same code is not working in another? Are you getting an error? What is the Debug.Log output?
     
  12. arssarath23

    arssarath23

    Joined:
    Nov 26, 2021
    Posts:
    10
    1. public void InitializeFCM()
    2. {
    3. ....
    4. StartCoroutine(GetTokenAsync());
    5. }

    6. private IEnumerator void GetTokenAsync()
    7. {
    8. var task = FirebaseMessaging.GetTokenAsync();

    9. while(!task.IsCompleted)
    10. yield return new WaitForEndOfFrame();

    11. Debug.Log("GET TOKEN ASYNC "+ task.Result);
    12. }
    I used the above script and the error was showing to me was mentioned below please check the pic
    upload_2022-7-29_12-11-46.png
     
  13. arssarath23

    arssarath23

    Joined:
    Nov 26, 2021
    Posts:
    10

    When I import the google-services.json file to my project assets i error was showing in console like
    TypeLoadException: Could not resolve type with token 01000074 (from typeref, class/assembly Google.EditorInitializer, Google.VersionHandlerImpl, Version=1.2.0.0, Culture=neutral, PublicKeyToken=null)
    Rethrow as TypeInitializationException: The type initializer for 'Firebase.Editor.XcodeProjectPatcher' threw an exception.
     
  14. arssarath23

    arssarath23

    Joined:
    Nov 26, 2021
    Posts:
    10
    The issue was solved and the token was generating now

    Error is because i have old External Dependency Manager.
    Updated the External Dependency Manager then downloaded the google-services.json file again and imported the unity and finally got token.

    Update External Dependency Manager package link : https://forum.unity.com/threads/firebase-stopped-working-after-unity-2020-upgrade.986790/
    check the conversation and will get the download link directly.
     
  15. Fangh

    Fangh

    Joined:
    Apr 19, 2013
    Posts:
    251
    Is it normal to get a StubToken in the editor ? upload_2023-3-16_18-0-44.png
     
    CoOz likes this.
  16. Visuallization

    Visuallization

    Joined:
    Mar 14, 2015
    Posts:
    8
    I was wondering. Should `Firebase.Messaging.FirebaseMessaging.TokenReceived += OnTokenReceived;` also work in the unity editor or only on mobile builds? And is there a way to easily test FCM in the Unity Editor without having to build the project for mobile?
     
  17. namhhuynh

    namhhuynh

    Joined:
    Apr 28, 2021
    Posts:
    1
    Using this code to try and fetch the token but firebase throws this exception:

    FirebaseException: The operation couldn’t be completed. No APNS token specified before fetching FCM Token

    Does anyone know how to fix this issue?
     
    Saaskun likes this.
  18. Saaskun

    Saaskun

    Joined:
    Nov 29, 2019
    Posts:
    50
    did you find the solution. I have the same problem
     
  19. Don-Tako

    Don-Tako

    Joined:
    Jan 31, 2013
    Posts:
    7
    Maybe you should upload your .p8 certificate to your Firebase Project Configuration (The cog at the top left menu)

    Certificate can be generated inside apple developer account, note that you can only generate few of these, but they works for all your apps

    Also add the Capabilities "Background Modes" (check background fetch & Remote notifications) and "Push Notifications" in XCode