Skip to main content
Version: 5.15.0

PutDeviceToken

public BackendReturnObject PutDeviceToken(string deviceToken);

Parameter

ValueTypeDescription
deviceTokenstringInformation about the local device.

Description

Turns on the push message setting.
When the push message setting is turned on, push notifications will arrive when BACKND Console uses the push message function.
By default, the push message is set to Disagree to receive nighttime push messages. To change this setting, refer to the AgreeNightPushNotification method.
For more information on push message registration, refer to the Android push registration guide.

Example

Write codes to load device token

using Firebase;
using Firebase.Messaging;


public class PushTest : MonoBehaviour
{

string token = string.Empty;

void Start() {
FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
{
var dependencyStatus = task.Result;
if (dependencyStatus == DependencyStatus.Available)
{
// Initialize Firebase
FirebaseApp app = FirebaseApp.DefaultInstance;
// Retrieve FCM token
GetToken();
}
else
{
Debug.Log($"Could not resolve all Firebase dependencies: {dependencyStatus}");
}
});
}

void GetToken()
{
FirebaseMessaging.TokenReceived += OnTokenReceived;

// The task operates as an external thread.
// When using a Unity method, such as GameObject.Instantiate or UnityEngine.UI, an exception occurs.
FirebaseMessaging.GetTokenAsync().ContinueWith(task =>
{
if (task.IsCompleted)
{
token = task.Result;
Debug.Log($"FCM Token: {token}");
var bro = Backend.Android.PutDeviceToken(token);
Debug.Log("Push message registration status : " + bro);
}
else
{
Debug.Log("Failed to get FCM token");
}
});
}

void OnTokenReceived(object sender, TokenReceivedEventArgs token)
{
Debug.Log($"Received Registration Token: {token.Token}");
}

}

Synchronous

string token = "";

Backend.Android.PutDeviceToken(token);

Asynchronous

Backend.Android.PutDeviceToken(token, (callback) => 
{
// Post-process
});

SendQueue

string token = "";

SendQueue.Enqueue(Backend.Android.PutDeviceToken, token, (callback) =>
{
// Post-process
});

ReturnCase

Success cases

When registration is successful
statusCode : 204
message : Success

Error cases

When the token information about the push message is invalid (e.g., string.Empty, null, etc.)
statusCode : 400
errorCode : UndefinedParameterException
message : undefined deviceToken, deviceToken cannot be checked

An error may occur if Firebase SDK or google-services.json file was not installed properly.
Make sure to check if the value is a randomly-generated string value via Backend.Android.GetDeviceToken().
When string.Empty appears, reinstall Firebase SDK or the JSON file by following the steps below:

  • Re-import FirebaseMessaging.unitypackage
  • Organize plugins (Assets > External Dependency Manager > Android Resolver > Force Resolve)
  • Check if Firebase's package name settings match Unity's package name settings
  • Re-apply the google-services.json file provided by Firebase