0

I have just started learning react-native and thinking of integrating firebase to it. Now consider my question scenario:

There are two users A & Bwho have the react app running in their device( none of them are admin). Now I have studied that when we connect our react native app to firebase, every instance of the app running on a device gets a unique token and that token is stored in firebase itself.

Now suppose user A wants to send a " notification or message" to user B. Now see the below code I saw on firebase official website:

// This registration token comes from the client FCM SDKs.
var registrationToken = 'YOUR_REGISTRATION_TOKEN';

var message = {
  data: {
    score: '850',
    time: '2:45'
  },
  token: registrationToken
};

// Send a message to the device corresponding to the provided
// registration token.
admin.messaging().send(message)
  .then((response) => {
    // Response is a message ID string.
    console.log('Successfully sent message:', response);
  })
  .catch((error) => {
    console.log('Error sending message:', error);
  });

This method seems quite straightforward, but is there really any method using which user A can know the unique token of user B like this:

const token= firebase.getToken('B');

And then use this token in the above code to send notification to user B. Is it poosible to do it using firebase?

Thank You.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Vipul Tyagi
  • 547
  • 3
  • 11
  • 29

1 Answers1

1

The code you found uses the Firebase Admin SDK to send messages. This SDK grants its users full administrative access to the Firebase project, so can only be used in trusted environments, such as your development machine, a server you control, or Cloud Functions. It cannot be used in the app you send to your users.

You will need a trusted environment to send the messages to the users. For more on this, see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thank God somone respond to the question. Now Consider another scenario: Suppose I am the admin of a react native app who is run by 10 users who are A,B...J. Suppose admin( me) want to send notification to user C, then how would I get the token of user C so that I can send notification to him using the above code that I shared? Is there any function like:``` const token= firebase.getToken('C'); ```. Which can give me token of any particular user. Firebase docs aren't clear about this. – Vipul Tyagi Nov 19 '20 at 14:46
  • I have some misconception about FCM. One of my friend told me that FCM is meant only for push notifications anf by no means using it can one user send notification to other. Only admin can use it. Is it true? – Vipul Tyagi Nov 19 '20 at 14:51
  • You can have one user send a push notifiication to another user by having them call a server-side API that you define. In that server-side code you can then control what messages they can send to what users. – Frank van Puffelen Nov 19 '20 at 14:53
  • Thats fine but suppose I am the admin of the app you are running( I have admin SDK), how will I find your unique token so that I can push notification to your device? That is what I couldn't find anywhere, not even in the docs. I have been searching it for entire day. – Vipul Tyagi Nov 19 '20 at 14:59
  • You'll need to create a token registry (typically a table in the database where you associate the token with either the user or whatever other method of targeting you use). That's what the `sendRegistrationToServer` method in the code snippets here should do: https://firebase.google.com/docs/cloud-messaging/android/client#sample-register – Frank van Puffelen Nov 19 '20 at 15:16
  • I think first part of your answer is what I am looking for but the link you have given is not relevant to the answer. I am running a nodejs server on my local machine with admin SDK and from here I want to push notification. – Vipul Tyagi Nov 19 '20 at 15:20
  • How it will locate my app server? Can I run the app server on my local machine? – Vipul Tyagi Nov 19 '20 at 15:42
  • As said in my answer: it can run on any trusted environment, such as your development machine, a server you control, or Cloud Functions. – Frank van Puffelen Nov 19 '20 at 15:44
  • So how would I register my server so that the function ``sendRegistrationToServer``knows where to send the token. And nothing has been specified in docs how server will receive the token. – Vipul Tyagi Nov 19 '20 at 15:56
  • ```// This registration token comes from the client FCM SDKs. var registrationToken = 'YOUR_REGISTRATION_TOKEN'; ```. I also don't understand where this token would come from, it says it will cone from client FCM SDK. – Vipul Tyagi Nov 19 '20 at 16:10