send cloud message (FCM) multiple devices single user - android

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.

Related

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.
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.

How to send push notification to specific devices

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.

Should I generate android GCM registration ID app specific or user specific?

I am developing an app which has login functionality.I am generating only one gcm registration ID per app. If other user sign's in in same app he will start receiving notifications intended for the previous user.
How to handle this situation, so that each user will get notification intended for his/her?
Should I generate gcm reg id for each user?
What is the standard way to handle this situation?
You could try the following things:
When the user logs off, send a request to delete the token on your server, and erase it on your app;
Once the user logs off, you could simply remove the association of "User ID" to "GCM Token" (Or Registration ID) on your server. And when someone logs in again, you make a new association to that Token.
The GCM Token is app specific, but the association you make on your server is totally up to you.
And I can't stress it enough, the token generated by GCM is APP SPECIFIC. If your user logs in on multiple devices, your server should handle that, associating the user ID to multiple Registration ID Tokens.
An ID issued by the GCM servers to the Android application that allows
it to receive messages. Once the Android application has the
registration ID, it sends it to the 3rd-party application server,
which uses it to identify each device that has registered to receive
messages for a given Android application. In other words, a
registration ID is tied to a particular Android application running on
a particular device.
And from the docs, your server should also:
Before you can write client apps that use GCM, you must have an
application server that meets the following criteria:
Able to communicate with your client.
Able to send properly formatted requests to the GCM connection server.
Able to handle requests and resend them using exponential back-off.
Able to store the API key and client registration tokens.
Able to generate message IDs to uniquely identify each message it sends. Message IDs should be unique per sender ID.
EDIT: You can read more about GCM here.
For further reading, you could also read more on Device Group Messaging.
You can also download some code samples here.
You should store regID as per user.
This is because there is test cases that user1 logs out and user2 logs in. In that case if you have stored regID app specific and not binding with user then user2 will also get notification.
So you need to store regID as per user , app and as well as device specific.
Of course yes every time someone downloads your app, your app should register them with the GCM and be given a reg token, this needs to be sent to your app server, on your app server you need to sort out who you are sending the notification too. you should probably send some login info and reg token when they login so you can identify each person, then write some php or Jquery to send to individual users or all. you should also send the reg token every time they login as these can change without warning.
Your app server should be able to handle removing unused reg tokens and adding new ones
Each android device generate a Unique GCM reg id. push notification from server side will send according to the device id. So at Login time you can send GCM reg id to your server. So from server side push notification will send to device.., that is actual functionality.
Hope this will help

Authenticating access to my GCM server

In my android application, I have utilized G+ logins to authenticate the user on the device, and I have implemented a basic GCM server to get a GCM registration ID for that device as well.
Once a device has both authenticated the user and received a GCM registration ID, I'd like it to let my server know that, for example, registration ID XYZ is associated with tim#gmail.com. This part I understand how to do. What I'm not sure of is how I can make sure that it's REALLY tim#gmail.com.
How can I be sure that the email/GCM registration ID pairs coming from devices is authentic?
Google allows you to verify back-end calls from your Android app by utilizing GoogleAuthUtil
to retrieve a string called an “ID Token”. You send the token to your back end and your back end can use it to quickly and cheaply verify which app sent it and who was using the app.
By passing the ID token with your registration ID call to the server, you can verify that the message is authentic and from your Android app.

Do I need different Authentication Token for each user for sending message using C2DM?

I know there is unique registartionId get from users email address for sending message to his device, but also do i need to have different authenctication id for each user?
If I am right we just need registrationId to send message to user, where as AuthenticationId is static, which is use to send message to user with registration id.
but in my case, I got Authentication Token using my developer email address, and if send message to the same email address I receive message. and If I send message to another user using registration id received from his email address and using authentication token got from my developer account email address user didn't receive message.
and again if I use authentication token got from user's email address and password and using his registration id user receive message.
The authenticationToken confirms that you have a valid Google account and thus are in general allowed to send C2DM messages. The registrationToken is directly tied to a user's device and your application. It allows your application to use your authenticationToken to send a message to the user's device.
You need one authenticationToken to send messages to all devices for which you have registrationTokens. Note however, that you should pick up a new authenticationToken every time you start sending messages, as those can be changed by Google at random.
I recall reading that Google advises to use different mail accounts for sending and receiving.
To the best of my knowledge the Authentication token is generated against the c2dm gmail id and the gmail account configured on the device.
That may be the reason why the other user is not receiving messages using your registration id.
So the answer to your question is yes, you need to have different Authentication Token for different user for sending message using C2DM.
You're correct that the authentication token is static, it serves to authenticate the app to the C2DM service. The registration id serves to identify the user you are sending the message to.
Since you are using your developer email account, it might be that the authentication token is invalidated. Did you check that? You'd get a 401 error sending the message. You shouldn't be using or collecting authentication tokens from your users, that shouldn't even work.
Note that C2DM assumes you are using a server component for your app as described here: http://code.google.com/intl/nl/android/c2dm/index.html#arch.
If you are trying to send a message to the C2DM server from the device, you're doing it wrong.

Categories

Resources