Sending message to custom user using firebase - firebase

Id like to make a chat between two custom users.
I need to send an message to custom user.
scenario:
User1 get User2 ID from my server
My server get User2 ID from Firebase (probably using Firebase admin)
My server return User2 ID to User1
User1 send message to User2 via firebase
Is it possible to make? Do I have to use Firebase Admin + FCM or something else?

[Giving you a simple guideline and logic here]
One of the simplest ways to implement it is by using Cloud Firestore.
You need to create 2 tables, one for the users and one for the chat. The users will be used to store the users' info including UID, emails, friends (array, to store a list of friends), chats (array, to store a list of chats), etc. While the chat will be used to store all the chats. (You can design your own database, some guideline can be found here)
When the user signed-in with Firebase Auth, you can query the database to get the details from the Firestore. Assuming you put everything in some UI element (recyclerview), once you click on the user's friend (friends are stored in the Cloud Firestore mentioned above), a new chat document will be created in the chat database/table, and under the other user (User 2) document record in the users table/database, update the chat array with the new document ID. For the User 2 UI interface, new chat will be created/updated as Firebase has something like onChangeListener() to update your client.
You will need to design your UI for the chat allowing the user to send messages. The UI will need to fetch all the chat records from the chat document. When any party sends a new message, update the message to the chat document.
So basically that's the rough idea, you can get all those references from Firebase Doc.
For notification, you can use Firebase Cloud Messaging to notify the user!
The actual function of FCM as stated in the doc:
Using FCM, you can notify a client app that new email or other data is
available to sync. You can send notification messages to drive user
re-engagement and retention. For use cases such as instant messaging,
a message can transfer a payload of up to 4KB to a client app.
Therefore, it is not intended user to user but is for the App admin to the user for certain events.
For Admin, it is not encouraged to call Admin all the time for a production consumer app.
P/s: I don't spoon-feed with code but to encourage people to learn from their own research.
Hope it helps!

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.

Send local personalized notifications without login

I have been trying to find an answer for a while now. I am creating a react native App, and want to send personalized notifications to a user. I only want to ask the user for their name and number, and don't want to make them create an account. Is there a way i can save this info in firestore?
Right now for firestore the only way I see to save user data is to first make them sign in.
auth.createUserWithEmailAndPassword(email, password)
I just want to be able to get a users name and save it in a database, to then be able to send notifications like "Good Job John Doe". My app has no need for an email and password auth.
You can use anonymous authentication to create temporary anonymous accounts. This can be linked to a user if they decide to sign up to your app.
You can use firebase firestore to store user information without using authentication.
Just simply ask the user about their name and number, save it in the firestore. Then using Firebase Cloud Messaging subscribe to a topic which will be unique to the user (name+number or something like that). Then using firebase cloud function or manually you can send notifications to each individual user

Flutter push notification using FCM by topic

hello I'm new to flutter and trying to get push notification in flutter , I have successfully subscribed the user to a topic and sent a notification through the firebase console by topic , what I'm trying to do is to have a button for admins in the app and when they post for a particular topic the users with that topic get the notification I have heard about cloud functions but I don't know exactly where to start ! i hope I made my point ! I have seen some videos that talks about type script but is there is any workaround this ?
The Firebase documentation names notifying the user when something interesting happens as an example use-case. From there:
Developers can use Cloud Functions to keep users engaged and up to date with relevant information about an app. Consider, for example, an app that allows users to follow one another's activities in the app. Each time a user adds themselves as a follower of another user, a write occurs in the Realtime Database. Then this write event could trigger a function to create Firebase Cloud Messaging (FCM) notifications to let the appropriate users know that they have gained new followers.
The function triggers on writes to the Realtime Database path where followers are stored.
The function composes a message to send via FCM.
FCM sends the notification message to the user's device.
To review working code, see Send FCM notifications.

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.

Best practice Fetch user's information from the Firebase User UID

I have integrated Firebase's Facebook and Email Login into my iOS application.
Although, I would like to only store the Firebase User ID in my API database.
Therefore from my backend server (say Ruby) how can I query firebase to get say the user's name and user's email address given that I have their Firebase User UID.
Thanks
To directly read user data for arbitrary users of your app, you'd use the Firebase Admin SDK: https://firebase.google.com/docs/admin/setup. This specific page shows how to read user data: https://firebase.google.com/docs/auth/admin/manage-users#retrieve_user_data
Reading user data through the Admin SDK is currently only available for Node.js and Java. Since Ruby isn't on this list, you'll need to keep the user information elsewhere. The Firebase Database is a common place to store such data, so that any platform can access it through its REST API.

Resources