Vipul Tyagi
Vipul Tyagi

Reputation: 667

Sending push notification to a specific user in cloud function FCM

Please note that this question is not about getting device token on client side.

I am very new to react-native and firebase and I want to implement push notication service of FCM. What I am planning to do is to send a notification to particular device using its device token on my nodejs app server or cloud function( not through console). But as I have found, FCM doesn't provides any API to accrss token by username. Suppose I want to send notification to user X( for the time being, suppose that one user signs only on single device). Now using the function sendToDevice(), I can send the message to a specific device. But how would I know the device token of user X. Do I need to store tokens in the firebase database by myself? Or can I get along without storing FCM tokens?

Please guide me because firebase docs aren't clear about this.

Upvotes: 1

Views: 1717

Answers (2)

Rohit K Choudhary
Rohit K Choudhary

Reputation: 79

Save the device token for a particular user in DB and then you can use fcm-node npm in which there is a simple function to send a push to a particular device token.

You also need to use the FCM server key which you will get easily on the firebase console.

Upvotes: 1

Doug Stevenson
Doug Stevenson

Reputation: 317828

FCM doesn't offer a way to associate device tokens to individual users who might be using your app from multiple devices. You will need to write code to associate a device token to a user account by sending that token to your backend, along with the user's ID. Only then can you collect FCM tokens for that user, and message them on any of their devices.

Upvotes: 0

Related Questions