Home > database >  Firebase push notifications with node.js and flutter
Firebase push notifications with node.js and flutter

Time:07-31

I am creating the push notifications functionality with firebase using node.js backend and flutter for front-end.

Till now I have learned about push notifications, register the application with firebase, and then successfully tested the notifications with the firebase console.

But I am having the confusion that how to handle the FCM token. as the firebase will send a notification to specific devices using the FCM token how can I get the FCM token from the front-end??

The solution that I have figured out is that when the user logged in to the mobile app the FCM token will be generated and will be sent to the node server from there I will store that FCM token in the database. So sending notifications will be easy to handle

is it a good approach?

or there is any good approach for that?

CodePudding user response:

What you're describing is indeed the correct approach, and is described in the Firebase documentation on accessing the registration token (this link is for Java, but the same exists on all supported platforms). From there comes this code sample:

/**
 * There are two scenarios when onNewToken is called:
 * 1) When a new token is generated on initial app startup
 * 2) Whenever an existing token is changed
 * Under #2, there are three scenarios when the existing token is changed:
 * A) App is restored to a new device
 * B) User uninstalls/reinstalls the app
 * C) User clears app data
 */
@Override
public void onNewToken(@NonNull String token) {
    Log.d(TAG, "Refreshed token: "   token);

    // If you want to send messages to this application instance or
    // manage this apps subscriptions on the server side, send the
    // FCM registration token to your app server.
    sendRegistrationToServer(token);
}

The onNewToken here is part of the FCM API, but the sendRegistrationToServer in this code is what you are describing and something you will have to implement yourself.

  • Related