How to send push notification to specific devices - android

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.

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.

send cloud message (FCM) multiple devices single user

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.

Send Push Notification to users who haven't opened the app yet

Is there any way to send push notifications to the users who have installed the app but not opened yet. I am using firebase to send push notifications.
In order for Firebase Cloud Messaging to send messages to an app on a specific device, the app must have collected a device ID token. This requires that the app have been launched at least once, so the app code can get that token and send it to your backend. If the app was never launched, that means it never had a chance to collect that token, and messaging to it is impossible.

How to know if application data is cleared or application is uninstalled?

I am developing application in which is based on Login-Logout mechanism along with REST API integration.
For making REST API request TOKEN is required. Application is saving this TOKEN and other user details to SharedPreferences.
When User logged in application saves all required data and at logout time all data is removed from SharedPreferences and TOKEN entry is deleted from application server
Application also register with GCM server for sending push notification to user. For that application sends Device Register Id to application server.
But if User clears application data or uninstall then application will open login screen and register again with application server and GCM server so old entry remains in application server and new entry is added to server.
So if server send push notification to application then two push notification receives in device.
Is there any way to detect programatically if application data is cleared or application is uninstalled?
Any help will be appreciated.
Finally i came up with following solution.
I have implemented GCM 3.0 with Instance ID API for sending push notification to application using Application and GCM server instead of old GoogleCloudMessaging.register(SENDER_ID);
So when Application server try to send push notification to device then server gets “error”:“NotRegistered” response for old device id so based on that server will remove useless entry.
In GCM 2.0 server gets “error”:“NotRegistered” response sometimes even if application is installed but in GCM 3.0 it works perfectly.
It is always better to save public data in app itself like the login ID,public ID etc and not the secure data. Let your server also have the same public ID. So If your application data is cleared or uninstalled, the Login ID,public ID in the app also gets erased. So the next time he installs the app or tries to login from beginning, if he uses the same Login ID he would be assigned with different public ID. During the time of authentication with server, the server will try to match with already present Login ID but it gets to know that the public ID is different and hence would ignore your old publicID entry and create a corresponding new public ID for your loginID. In this way you can prevent 2 server push notifications for the same login ID.
You could use broadcast receivers for the uninstall and data erased:
ACTION_UNINSTALL_PACKAGE
and
EXTRA_DATA_REMOVED
You could have the program remove the registration in server, or have the server "timeout" registrations that remain idle for too long.
You can use Firebase Analytics by Google to track these app events.

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

Categories

Resources