How to get all of device tokens from Firebase?
Asked Answered
G

4

15

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?

Glad answered 28/3, 2017 at 4:51 Comment(0)
P
16

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.

Pentastyle answered 28/3, 2017 at 5:15 Comment(2)
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?Grub
The API to convert APNS token to fcm registration token is describe here: developers.google.com/instance-id/reference/…Grub
S
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

Shoreline answered 28/3, 2017 at 5:10 Comment(0)
D
3

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.

Disengagement answered 20/2, 2020 at 14:43 Comment(3)
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.Repel
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.Disengagement
How would you send a message to a single registration then? It's not a practical thing to do.Grane
M
-1

You can make a get request to

https://<<your_project_name>>.firebaseio.com/.json?auth=<<your_database_secret_key>>

The database secret key you get in Firebase console under the tab settings/serviceaccounts/databasesecrets

This returns a JSON file containing all Storage data of your project.

Myrick answered 31/7, 2019 at 17:17 Comment(2)
@RaviSharma when you enter the url i posted with the params does it returns anything?Myrick
it returns null.Kalpak

© 2022 - 2024 — McMap. All rights reserved.