Firebase functions -get fcm token from user id - firebase

I want to send push notification to specific user,
but i dont know his fcm token.
Is it possible to get the fcm token of user from his user id?

A user may be using multiple devices. You should expect this to be the case. You'll need to store each device token for all devices that a particular user may be using in order to notify them. Typically, Firebase apps will use Realtime Database to record the token at the time of login.

Related

Export Historical user data from Firebase

I have setup a firebase project to track users events and for supporting push notification in my IOS and Android apps. In my apps I was registering for notifications when user completes sign up. However I have a number of users who just opened the app and didn't complete registration. I am planning to send notifications to such users to give them a nudge.
However I don't have the notification tokens for such users in my database. So is there any option to export the details of such users from firebase where I can grab their notification tokens?
I tried using big query and audiences but that wont work with historical data.
It sounds like you're using FCM tokens to send messages. In that case you will always needs an FCM token to send a message to a specific app/device.
FCM doesn't know anything about users of your app; all it knows about are the tokens.
So if you want to send a message to an app instance where the user didn't sign in, you'll need to store the token from the app in your database as soon as the app starts - even before the user signs in. Then when the user signs in, you can associate the token with their UID.
With those steps out of the way, you can query for database for tokens without an associated UID and send them a reminder to complete the registration.

Does firebase cloud messaging allow targeting of push notifications to a specific device/user etc

I am trying out firebase cloud messaging (React-native-firebase) and it seems that I am only able to push notifications to all android/ios users, not specific users. Is there a way to target a specific user. Like if a user invites another user only send notification to that one specific user?
The flow can be this one:
User allow notifications on their device(s)
Grab the generated token and store it in the user document inside a tokens array.
(If the same user connects to your app in another device, add the new token in the tokens array)
If you want to target a specific user, get his UID and send a notification to their devices.
You can also subscribe a user to a topic and send a notification to users subscribed to that topic.
two way:
init
your client-side subscribe the topic.
subscribeToTopic("weather")
your client-side report the user property
setUserProperty("name","TOM");
Firebase Cloud Message Console
crate a notification item like this way
The flow can be this as follows:
User allow notifications on their device(s)
Grab the generated token and store it in the user document inside a tokens array.
(If the same user connects to your app in another device, add the new token in the tokens array)
If you want to target a specific user, get his UID and send a notification to their devices.
You can also subscribe a user to a topic and send a notification to users subscribed to that topic.

Web Push Notification

I'm having some series of questions about implementing push notifications. The things are,
I am using Angular for my frontend where am sending my subscription value which is of an object type with key keys like endpoint, auth, etc (is this will be same for every device like mobile, or some other browsers).
Does all the data in that subscription object is mandatory? or only endpoint.
If a user is logged in with more than 10 devices, do I need to store subscription values for each device? is it how it works? Or should I store the last logged in device's subscription value? If so then the rest of 9 won't get any notifications.
If you are storing all the loggedin Device's subscription value, then is a user logged in more than one browser will he get the notification in each browser? Is it a standard practice?
Suggestions are welcome, any standard practices will be helpful.
Thanks in advance
subscription object has the same format for every web device/browser
yes, for more, see the documentation
When we want to send a push message to a user with a payload, there are three inputs we need:The payload itself.The auth secret from the PushSubscription.The p256dh key from the PushSubscription.We've seen the auth and p256dh values being retrieved from a PushSubscription but for a quick reminder, given a subscription we'd need these values:
each device has it's own endpoint and auth keys, so if you want to send push to every device user logged in, you need to store all of them
yes, user gets notifications for all browsers, if you call them.( you're making a post request to device's endpoint )

retrieve registered token from firebase console

I understand that client app send request for registration and firebase return with some token which we need to store on our app server so that later can be used for sending notification to particular device. I want to know is there any way to get the same token on firebase server using it's console?
Thanks
The Firebase console has no feature to show what FCM Instance ID tokens have been handed out. If you want to have listing of all the tokens in your app, you will have to store it yourself.

Firebase Auth + FCM

My usecase it to send a push-notification to a user using the Firebase Auth UID to all the devices he is signed-in. Since firebase Auth is managing the user sessions,
Is there a way to directly send a FCM notification just using the uid of the user? Or
Is there a way to fetch the registration ids of all signed-in devices for a given uid?
This looks like standard requirement, for example to update the 'Order Status' to a user. If available this could be pretty powerful, and could greatly simplify direct messaging requirements (e-commerce, chat etc) where the user gets notified on web/android/ios.
If this is not possible, any suggestions on the standard way to acquire/manage the registrations_ids of a given user is appreciated.
Thanks all,
This is not supported out of the box but you can build the mechanism.
You need to maintain a uid specific deviceGroup and all the registration ID per device that belongs to it.
Each time a user signs in with Firebase, you get the device registration ID and the user's ID token and send it to your backend. You verify the ID token and get the uid from it, you then add that registration ID to that user's device group.
Each time a user signs out, you remove their registration ID from the signed out user's device group.
When you want to send a push notification to a specified user, you can send it to the user's device group ID.
This is the simplified version but there are a bunch of edge cases you have to deal with to keep the device group for a user in sync so no notification is sent to a signed out user.

Resources