Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all of device tokens from Firebase?

How to get device tokens (FCM registration tokens) from Firebase?

I'm making mobile app using firebase and its server using node.js.

I want to send push notification message from server, but I don't know how to get devices token.

  1. How to get device tokens from external server? I'm using Firebase admin SDK now.

  2. Is the device token only generated when the app is connected fcm server?

  3. Should I save token to another database when user first run the app and register FCM server?

like image 557
ton1 Avatar asked Mar 28 '17 04:03

ton1


People also ask

How do I get Firebase device token?

You need to call sendRegistrationToServer() method which will update token on server, if you are sending push notifications from server. UPDATE: New Firebase token is generated ( onTokenRefresh() is called) when: The app deletes Instance ID.

How can I get registration token from client FCM SDKs?

This requires you to: Add app logic in your client app to retrieve the current token using the appropriate API call (such as token(completion): for Apple platforms or getToken() for Android) and then send the current token to your app server for storage (with timestamp).


2 Answers

1. How to get device tokens from external server? I'm using Firebase admin SDK now.

There is currently no API to retrieve all the Registration tokens for your app from the Server side. It's the developer's (you) responsibility to send and store the Registration token to your App Server, which is generated from the Client App side.

2. Is the device token only generated when the app is connected FCM server?

The documentation pretty much covered this (see also my answer here):

On initial startup of your app, the FCM SDK generates a registration token for the client app instance.

It doesn't technically get connected to the FCM Servers. It connects to the FirebaseInstanceID service (via the SDK), which in turn generates the Registration Token.

3. Should I save token to another database when user first run the app and register FCM server?

As I mentioned in #1, you should save it where you can easily access it to send a message.

like image 63
AL. Avatar answered Sep 22 '22 01:09

AL.


The device token is generated when the app first connects, this token must then be stored in your database, and replaced if a new token is assigned

public class MyAndroidFirebaseInstanceIdService extends FirebaseInstanceIdService {

    private static final String TAG = "MyAndroidFCMIIDService";

    @Override
    public void onTokenRefresh() {
        //Get hold of the registration token
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        //Log the token
        Log.d(TAG, "Refreshed token: " + refreshedToken);
    }
    private void sendRegistrationToServer(String token) {
        //Implement this method if you want to store the token on your server
    }
}

See the following Tutorial

like image 38
Donald Jansen Avatar answered Sep 22 '22 01:09

Donald Jansen