How to send FCM messages to a different user - node.js

I am able to send the notification to a single user by getting its fcm token.
Consider this simple case of something like Instagram:
A user(X) sends a friend request to some different user(Y)
Now I want to send a notification to user Y, how should I do that?
The only way I can think of is to store the fcm token into the user data base and send on basis of that, Is there a better method?

Firebase Cloud Messaging has three ways to target messages:
You send a message to a FCM token/device ID.
An FCM token identifies a specific app on a specific device, so you're targeting a single app instance.
You send a message to a topic.
An app instance can subscribe to any topic, so you're targeting everyone who has subscribed to the topic here.
You send a message to a device group.
You can group FCM tokens into groups of up to 20, which you can then target with a single message.
Of these options, the first and third are by far the most common.
You'll note that there's no options to send a message to a user, as FCM doesn't have the concept of a user. A user can be signed in to the same app on multiple devices, in which case they'd have multiple FCM tokens. And (less common, but still common enough to think about) multiple users can sign into the same app on the same device.
You'll want to set up a database somewhere (sometimes referred to as a token registry) where you map the FCM tokens to how you want to send messages. So in your case, storing the tokens for each user makes sense, and is in fact quite idiomatic.

Related

FCM, send multiple devices without tokens?

I want to send FCM to everyone who installed the app. Is it essential to get everyone's tokens from the database every time?
My app is using firebase firestore overall. If there are 100,000 users,
do I have to read 100,000 from database to send fcm each time? (I think it`s little heavy stuff isn`t it?)
another workroad exists?
I wonder Is the only way to send it by putting it in the registration ID?
And can you send it on time? All apps on the market send push messages on time, but if you read 100,000 and send fcm separately, shouldn't it arrive like this at 9:01 or 9:02? But why do I always get messages at 9 o'clock?
What are the methods, logic, algorithms they use (the way companies usually use)
I still have no clue at all.
There is no "send to all users" operation in FCM. You either will have to send to each token (that's not a heave operation for FCM, which handles billions of such calls every second), or you have to subscribe all instances to a specific topic and then send to that topics (which ends up the same behind the scenes, just with Firebase loading the tokens for the topic for you).
This has been covered a few times before, so I recommend checking:
How do you send a Firebase Notification to all devices via CURL?
How to send notifications to all devices using Firebase Cloud Messaging
Firebase Cloud Messaging - Send message to all users
The notifications panel in the Firebase console has an option to deliver messages at a specific time, but no such option exists in the Firebase Cloud Messaging API. You'll have to either implement your own mechanism to schedule the delivery, or you can deliver a data message right away and then only display the notification on the device when it's time.
This also has been covered a few times before, so check:
Firebase Messaging FCM Distribution over configurable time interval
How can scheduled Firebase Cloud Messaging notifications be made outside of the Firebase Console?
Flutter Firebase Messaging: How to send push notifications to users at specified time

Send notification to one user with firebase cloud function node

I am trying to send a notification to a specific user using FCM, but I haven't found a way. I have a mobile app and a node server running.
I want to be able to send a notification when the shipment status changes. I have already a function for it in my server I just have to send the notification to the user. Is it possible to achieve this using nodejs or is there a way to implement it in flutter?
I found this code
    var FCM = require('fcm-node')
var serverKey = require('path/to/privatekey.json') //put the generated private key path here            
var fcm = new FCM(serverKey)     
var message = { //this may vary according to the message type (single recipient, multicast, topic, et cetera)        
to: 'registration_token',         
collapse_key: 'your_collapse_key',                
notification: {            
title: 'Title of your push notification',             
body: 'Body of your push notification'         
},                
data: {  //you can send only notification or only data(or include both)            
my_key: 'my value',            
my_another_key: 'my another value'        
}    
}        
fcm.send(message, function(err, response){        
if (err) {            
console.log("Something has gone wrong!")        
} else {            
console.log("Successfully sent with response: ", response)        
this is a npm package taht lets send a notification but it asks for a registration token.
I hope you can help me. Thanks in advance!
Firebase Cloud Messaging has no concept of a user. Instead it can send messages to:
Specific app instances (so your specific app as installed on one specific device), as identified by an FCM/device token.
A group of app instances/tokens, as defined by your application logic.
A specific topic, to which app instances can then subscribe.
It's up to your application logic to decide how to map from your user ID to one of these options. The most common are to:
Store the device token(s) and your user ID in your own database, then look up the device token(s) for a user when needed and fill them in to the API call you already have.
Use a specific topic for each user ID, subscribe the application to that when it starts, and then send a message to that topic when needed. Note that anyone can subscribe to any topic though, so this approach would allow folks to receive message for anyone whose user ID they know.

Push Notifications for multiple users in multiple events

I am building an app in which users can register themselves in events.
I'd like to send push notifications to the users registered in a particular event when, for example, the organizer makes a modification to the event.
Stack :
Server : NodeJS, Graphql (Apollo), Firebase for authentication.
Client : React-Native, Graphql (Apollo), Firebase for authentication.
For now I send in-app alerts using subscriptions, but I'd like to use push notifications so the users have the notification event if the app isn't launched.
I have read the docs of Firebase Cloud Messaging, and it uses topics to send notifications to users.
I could create a topic for each particular event, and have each user subscribe to each "event-topic" that corresponds to each event they are in, but this sounds like a misuse of FCM topics. To my understanding topics are made for a more general behavior, in the docs they only use topics like "weather", "highscores" ...
I'm not sure which approach I should use. Is the "one topic per event" wrong ? What are your thoughts about it ?
Topics actually sound like a great mapping for your use-case, as you could subscribe the users to the FCM topic (similar to how you already subscribe to to database ) to receive updates about the event.
The only thing to be aware of is that topics are public: any user can subscribe to any topic they know off. So in your case if you use the event ID as the topic, then anyone can subscribe to any event they know (or can guess) the ID of.
If that is a concern, you should not use topics and instead manage your own tokens and fan out from event ID to the FCM tokens of the folks who receive updates.

Retrieve Teams User ID based on AAD ID

I am trying to start a scheduled proactive conversation (the bot initiates the conversation on scheduled time).
I managed to get the User's AAD ID based on Graph API, but it doesn't match the Teams user ID. Tried for over 2 hours to obtain the right id, but I can't figure it out how. What would be the best approach I should take?
Have a look at the Microsoft Graph api to get the chat thread ID.
When the app is installed for the user, the bot will get receive a conversationUpdate event that will contain the necessary information for it to send the proactive message. For more information, see Bot events.
If you lose the chatThreadId, you can find it again by calling:
GET /users/{user-id}/chats?$filter=installedApps/any(a:a/teamsApp/id eq '{teamsAppid}')
However, this will only for for the personal scope! My advice would be to make sure you catch the conversationUpdate which is triggered after an install and persist the user details in a database.
I'm curious how you tried to "match" these? In any case, I don't think they're intended to match up in any way (the aadObjectId Guid and the "29:..." user id). As a result, you should store a mapping on your side (database or similar). You need to store ServiceUrl and ConversationId anyway to do proactive messaging, so just tack userid on as well.

Sending Firebase Notification with nodejs

I have an android app on which I want to send notification via my web app.
How do I send Firebase Cloud Messaging notification with nodejs? I have found a lot of examples and posts on this subject, but in all of them you are supposed to just paste device Token and send notification to that device. If I wanted to use it that way, I would be able to simply implement this from official documentation.
The thing is, I need to receive user id from database so I know which user I want to send notification. After I have user id I can then retrieve device Token from the same database. I get both user id and device token on client side. And documentation refers to server side.
This all happen after button click. So, I don't understand how to send notification on server side, meaning, I don't have there all those informations I need - user id, device token and message info that contain some other database info. Do I pass those arguments from client side to server side somehow, or is there a way of using modules like "require()" on client side?
What is the best approach here?
You will need to implement a device token registry to fit with your use case. So if you want to send messages to users, then you will need a registry (really just a fancy word for a database) of the token(s) for each user. Then when you want to send a specific user a message, you look up their token(s) in the registry, and call the FCM API to send messages to them. Since you mention Node.js, you can likely do this using the Firebase Admin SDK: https://firebase.google.com/docs/cloud-messaging/admin/send-messages#send_to_individual_devices.
An alternative I have once written article about using a topic for each user, which saves having to have a registry of their tokens. While this is less secure (since anyone who knows a topic, can subscribe to it), it is definitely easier to implement.

Resources