How to get all of device tokens from Firebase?

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.


I suggest not storing the tokens at all. Storing the token seems like a good idea until you realize that your users have multiple devices (iOS device, android device, desktop etc.) Now you have to manage storing multiple device tokens per user and that can get messy fast.

Instead, ignore the tokens that are generated and simply subscribe each user to a topic such as when user logs in or creates account subscribe them to a topic made of their userid

NSString* useridchannel = [NSString stringWithFormat:@"user_%ld", (long)userid];
[[FIRMessaging messaging] subscribeToTopic:useridchannel completion:^(NSError * _Nullable error) {
       NSLog(@"Subscribed user to topic %@", useridchannel);
}];

When user logs out make sure to unsubscribe them from that topic as they probably don't want to receive notifications if they are no longer logged into your app

NSString* useridchannel = [NSString stringWithFormat:@"user_%ld", (long)userid];
[[FIRMessaging messaging] unsubscribeFromTopic:useridchannel completion:^(NSError * _Nullable error) {
         NSLog(@"Unsubscribed user from %@", useridchanel);
}];

I realize that Device Groups are supposed to be used for this purpose, but again I don't want to be bothered with storing and managing tokens and also Firebase Admin SDK for PHP does not support sending messages to Device Groups. By using topics there is no need to deal with device tokens and you can send messages to all the users devices from server, the app, or firebase console easily.


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