How to get all of device tokens from Firebase?

23,531

Solution 1

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.

Solution 2

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.

Solution 3

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

Share:
23,531
ton1
Author by

ton1

Hi there.

Updated on July 15, 2022

Comments

  • ton1
    ton1 almost 2 years

    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?

  • Vishal Singh
    Vishal Singh over 6 years
    Cant we just use google instance-id API to generate fcm token from APNS token and use it to send push notification to iOS devices without integrating firebase SDK? I tried it, but without sdk integration, push does not work. Once the SDK is integrated, the same API starts returning correct fcm token, which works fine for push. And then you can actually remove firebase SDK and still receive push notifications using firebase. Cant firebase simply take raw device token and send push notification using correct certs or auth keys. Why the SDK has to be included?
  • Vishal Singh
    Vishal Singh over 6 years
    The API to convert APNS token to fcm registration token is describe here: developers.google.com/instance-id/reference/…
  • Joao Vitor Marques
    Joao Vitor Marques over 4 years
    @RaviSharma when you enter the url i posted with the params does it returns anything?
  • Ravi Sharma
    Ravi Sharma over 4 years
    it returns null.
  • Volodymyr Bobyr
    Volodymyr Bobyr over 3 years
    The problem with this is that there's no way to make the messages sent to topics private. I.E. if some actor guesses the topic path, they can subscribe to it as well.
  • Greg Ellis
    Greg Ellis over 3 years
    fair point. you can secure this a little better by generating your own token for each user such as (n) digit random string of letters, numbers, special characters store it in the db and use that instead of the user id.