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.
How to get device tokens from external server? I'm using Firebase admin SDK now.
Is the device token only generated when the app is connected fcm server?
Should I save token to another database when user first run the app and register FCM server?
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.
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).
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With