Can we send notifications in android using Firebase's UID instead of registration token?

CR Sohan :

I have two android applications in my single firebase project. That project belongs to the connection of vehicles. One app is for the driver and other app is for the passenger. So whenever passenger requests for a ride the driver needs to be notified of that request. So is there any way to send notification to the driver using firebase UID instead of FCM registration token.

I want to know whether registration token will be fix for a particular user or it will refresh/change over time.

Yash Krishan :

Yes you can, use this to register the UID as a topic:

FirebaseMessaging.getInstance()
                .subscribeToTopic(appUser.getUid());

Now, when sending the notification, you can use this topic to send the notif. to the particular user.

Tokens keep on refreshing at instances, topics defined by you will not change. Every device for which the topic is defined will be notified.

NOTE:

Registering too many topics will raise messaging/too-many-topics error. Details here. Hence, token registration in the preferred way.

Fetching and keeping track of tokens:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

@Override
public void onNewToken(String s) {
    super.onNewToken(s);
    // Save the new token here in a place from where you want to fetch it and send notification
    Log.e("NEW_TOKEN",s);
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=161741&siteId=1