DEV Community

Rodrigo Garcia Trejo
Rodrigo Garcia Trejo

Posted on

3 2

Tutorial Android Studio: Obtener token de mi dispositivo

Bienvenidos a un nuevo tutorial en esta ocasión les enseñare o obtener el token de su dispositivo en android studio utilizando firebase.

¡¡¡Vamos al código!!!

En nuestro activity creamos un método llamado obtener Token y dentro agregamos este código.

    private void obtenerToken() {
        FirebaseMessaging.getInstance().getToken()
        .addOnCompleteListener(task -> {
            if (!task.isSuccessful()) {
                Toast.makeText(RegistrarToken.this, "Ocurrio un error", Toast.LENGTH_SHORT).show();
                Log.w(TAG, "Fetching FCM registration token failed", task.getException());
                return;
            }
            String token = task.getResult();

           //  AQUI PODEMOS MANDAR LLAMAR A UNA FUNCION PARA QUE LO GUARDEMOS EN NUESTRO BACKEND
        });
    }


Enter fullscreen mode Exit fullscreen mode

El cual nos retorna el token del dispositivo ahora lo podemos utilizar donde queramos. Se recomienda guardar este token en nuestro backend para poder utilizarlo cuando queramos enviar una notificación a un dispositivo en concreto o a un conjunto de dispositivos.

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more