简体   繁体   中英

Firebase OnTokenReceived Doesn't Get Triggered in Unity

I need to set up FCM for push notification in my unity game. For that, I am using initializing code which is mentioned in the Firebase docs which is:

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);
} 

The game uses GameSparks for back-end services. I want to notify user for New High Score in the leaderboard. To do that I have to register device for push notification and according to GameSparks's documentation the following code is used to register push:

  new PushRegistrationRequest()
        .SetDeviceOS(deviceOS)
        .SetPushId(pushId)
        .Send((response) => {
        string registrationId = response.RegistrationId; 
        GSData scriptData = response.ScriptData; 
        });

The pushId is taken from is the registration id returned from the call to FirebaseInstanceID.getToken() as mentioned here . Did I do something wrong here? If there is a better solution please tell me that too. Thank you in advance.

If you're not building and running the Unity Project on an Android Device you will not receive a token. If you're running it on your device and are connected to your computer you can check if the log message is triggered using:

adb logcat -s Unity

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM