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.
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.
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.
Related
I have an application which is already built. Now I want to use FCM to send messages between server and users.
As I understand each device has its own token which I can use to send message, and this token can be retrieved from
FirebaseInstanceId.getInstance().getInstanceId()
Correct me If I am wrong.
The application and backed server works fine and bellow my workflow
1- User open the android app.
2- Android app take the user token ID for authentication and device token for
messageing and send them to the server.
3- The back end server verify the user via
decodedToken = FirebaseAuth.getInstance().verifyIdToken(login.getToken());
then it save the token for sending the message.
Sending message working fine until I stuck with the following scenario.
The application allows multiple devices to login with same user, which means for 1 USER can have multiple tokens for messaging.
In this case I want to send message to all devices logged in with this user.
So how I can do this without changing the current design of my application
You need to change your DB and add ability to save all device tokens from one user. Then you need to call this Firebase api https://fcm.googleapis.com/fcm/send as many times as the tokens user have. Here the official documentation.
https://firebase.google.com/docs/cloud-messaging/http-server-ref
To update device specific token when it is changed on client side, you also have to bound that token with the deviceID, so to have that you need to pass the DeviceId to the server when passing the Firebase token. As deviceID can serve Android unique deviceID, or the id that provides Firebase in client side FirebaseInstanceId.getInstance().id. And here how to get Android deviceID
Is there a unique Android device ID?
If you use access_token for login the user, as another alternative, you can bound firebase token with user access_token, as it is unique for any device the user logged in.
I got two mobile apps for service provider and customer, i need to send push notifications for service provider user once the customer requests a specific service type. So it needs to be filtered and send only for those who can provide such a service. Therefore web api needs to send the notification only for specific users, how can i do that with firebase should i use, device mac or registration id generated by firebase.
Any help
register a new project in FCM console.
Make android/ios and web(for configuration) application.
The first-time app will be loaded it gives device token and store to the database.
by using device token you can send a notification to a particular device.
Reference link:- https://www.djamware.com/post/5c6ccd1f80aca754f7a9d1ec/push-notification-using-ionic-4-and-firebase-cloud-messaging
First you have to get the fire base token from android app.
Register your mobile app in Firebase console.
If you have any user login api and database, send the fire base token while user
login your app and save the token against the user.
Whenever the user request for service you can get the token of the particular user
and send a message to fire base api with that token from server side.
It will automatically send the message to that particular device whoever login your
app.
Make sure you update the token while if other user login your app in same device.
How would i send a push notification automatically, instead of doing it through the firebase console manually. Like when user invites another user send a notification to the user that they have been invited. I am getting the user device token by doing firebase.messaging().getToken();
You would need to develop your own server where you need to save device token and send push notifications based on various triggers.
Alternate way - if you are using other Firebase services like Firestore - you can go with Firebase Functions. Here is quick overview: https://firebase.google.com/docs/functions/use-cases
Do it through your backend, First you need to register users' token on your backend and when userA send invitation to userB send that request to the backend and your backend should have userB token, it will send userB notification.
If you use firebase DB and want to send push notification by some actions (for example, new record in your db ), you can try to use firebase functions.
See here for more information
In any other ways - you should create your own server app that will be connected with fcm or apn services and your db. Of course, you should configure your server app correctly.
I am developping a mobile application that must implement push notifications. Documents are stored on a SQL database, and people should be able to edit these docs after downloading them offline, then the modifications should be saved in the SQL Database once the phone is online again. People should be able to receive notifications when the app is closed.
I heard that I need push notifications, and for android there is Firebase Cloud Messaging (FCM). But is it possible to use FCM with my SQL database ?
All tutorials in the docs only speak about connecting the app with Firebase
For your project you need a server with your API, SQL Database... and a push notification server that ask FCM services to notify your registered devices.
FCM works like that :
You add the FCM plugin in your Android/iOS app, then when you launch the application it will ask FCM for a unique token associated for your device and this app. You will store that unique token in your push notification server, I advise you to associate it with a user identifier or one thing that you will use to identify the device to notify.
When your first server (with the APIs and the Database) did some action and you want to notify a device :
The server will ask your push notification server to notify a list of devices that you will probably determine with the user identifier like I said earlier, then the push notification server will find the associated tokens and send all of the pre stored tokens to FCM with the notification content you want to send, you can also send parameters that will be used to do special actions in your mobile app.
Finally FCM will notify your devices :)
Hope it helps.
I'm building an app that allows the user to create events and add people to those events. It stores the event and certain user data (email and display name) in a Firebase database.
What I'm trying to do is send a notification to a specific user when another user adds them to an event. I've tried looking at the documentation for both Firebase Notifications/Cloud Messaging and Batch, but I can't find a way to send the notification to a single user, rather than an entire group.
How do I go about doing this? I'm hoping to avoid storing the notification in the Firebase DB and setting up a servlet backend that queries the DB repeatedly.
Edit: If I do have to make a Java Servlet backend for this, can someone tell me how I would code it?
I think you can do that by specifying the device token when sending the notification using console or using post request to FCM server. But unfortunately you have to handle every single device token manually by yourself (get it from the device when it register to FCM, and save it to your own DB somewhere).
If you use console, you can put the token on "FCM registration token" field under target -> single device.
If you use post request, you can make a request like this
{
"data": {"my_custom_key" : "my_custom_value"},
"registration_ids": ["your-device-token","your-device2-token"]
}
Batch.com provides a simpler API named Transactional API to send a notification to one or more users identified by their User ID, Firebase UID etc. as you probably don't want to deal with the complexity of APNS push tokens or GCM registration ids yourself. Contrary to FCM, you will get a complete analytics view (eg: open-rate) on those notifications.
You can trigger push notifications directly from your app code by using our Swift client for the Transactional API or by coding your own HTTP client (it's a standard POST request with a JSON body). We even have a client for Node.js if you decide to trigger the notifications from a server.
And if you are using Firebase, we have a specific documentation to plug Firebase with Batch.