I am trying to implement push notification using Firebase Cloud Messaging but it seems as if you have to register the device to receive push notifications. I am trying to make it so when there is action in the database, such as an inbox reply, send a push notification to the user who received the message.
Is there a way to send the push notification with Firebase to a particular user, not a particular device?
You don't send push notifications to a user, you send them to a device.
Somewhere in your code (maybe when your user signs up/ logs in to your app), you have to execute the code to get a GCM registration token. This token is unique to your Android app and that specific Android device.
With that token, you can send push notifications to that device using any method you want (from an app, from a website, etc. If you have a GCM reg token you can even send pushes from this handy website: http://1-dot-sigma-freedom-752.appspot.com/).
It is now your responsibility to keep a database of your users and their GCM registration tokens. When a user wants to send a push to another user, you would find the corresponding registration token in your database and then execute the code to send them the push using that token.
FCM provides several ways for you to send notifications to your users. You can send messages directly to devices via an Instance ID token, you can send messages to a topic that your user has subscribed to, and you can also send messages to groups of users eg: country/language etc.
Have a look at this sample which demonstrates sending to a device and topic. Also have a look at the docs for more details on sending and receiving messages.
Related
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.
I want to know that is there any way to send push notification programmatically on particular FCM registered token (Device token)? i had implemented group chat but stuck in one-one chat.
I don't want to use any web-service or any back-end at all, because i am using Firebase database and storage as a Back-end in my application.
i want to send upstream message to particular device token.
Any idea's how to do that ? thanks.
I have two questions related to Android Push Notification System:
What is the working principle of this system? The client sends its own IP to the Google Cloud Messaging Service (for example when it switches its own IP)? So it a sort of pooling?.
How do you know that Google Cloud Messaging Service "looks into" the content of the notification message (created in the server and dispatched to the client)?
Answering the question about how GCM service contacts the client, the GCM client contacts GCM to create the connection. You are correct that device addresses change as the device disconnects and reconnects so GCM cannot initiate the connection from the server side.
This connection is maintained as much as possible and is not created for specific messages.
The registration id identifies the device and app and allows GCM to route the message to the device, if it is connected. If it is not connected, GCM needs to wait until the device reconnects.
What is the working principle of this system? The client sends its own
IP to the Google Cloud Messaging Service (for example when it switches
its own IP)? So it a sort of pooling?
I'm adding an image here describing how GCM works.
This is a step by step presentation. You need to get the push registration ID first when your application launches. So if you've a backend server to send some push notification in your application, you need to pass the registration ID to your backend server. So when you need to send a push notification, your backend server will send the push directly to GCM with the targeted registration ID. GCM manages to push the notification in your device when your device comes online.
So this is not any kind of pooling. The only thing GCM needs to know is the registration ID of your device when it comes online and tries to communicate with GCM. Once your device is registered, GCM sends the push notification using that registration ID.
How do you know that Google Cloud Messaging Service "looks into" the
content of the notification message (created in the server and
dispatched to the client)?
This question is not very clear to me. As far as I have understood, you wanted to know how GCM understands to whom it needs to send the push notification when the notification is coming from your backend server. If this is your question, then I think I have answered it already in the previous section of my answer.
GCM doesn't need to look into your notification content to know the destination of the push notification. As I said earlier, when your application launches, it requests for a push registration ID from GCM and when it receives an registration ID, you might have to pass the registration ID by calling a service of your backend server. The server then knows to whom it might send some notification.
So, when its time to send a notification to your client application, the backend server sends the notification to GCM with the registration ID you sent to your backend server earlier. GCM then handles sending the push notification to the client when the application comes online.
Hope that helps!
I was wondering how android push framework is able to distinguish data recieved via GCM and forward it to the appropriate android application for which it was intended ?
Can anyone let me know how it is done ?
You question : how android push framework is able to distinguish data recieved via GCM and forward it to the appropriate android application.
From your question it looks like you willing to know about data
workflow & Client Server architecture that exists in applications
which are using the GCM service.
As per google’s documentation “Google Cloud Messaging for Android (GCM) is a service that helps developers send data from servers to their Android applications on Android devices”. GCM is a service provided by Google for developer that helps developer to send data from server to any number of Android devices.
Simplified Application Specific Work-flow:
The push notification can be broadcasted either to the mass audience
or a select set of users. Mass audience is targeted when the
notification has to be sent about a marketing campaign. A subset of
users are targeted when a personalized information has to be sent.
The below steps explains how push notification works on android devices:
First android device sends sender id, application id to GCM server
for registration.
Upon successful registration, GCM server issues registration id to android device.
After receiving registration id, device will send registration id to our server.
Our server will store registration id in the database for further use.
Whenever push notification is needed, our server sends a message to GCM server along with device registration id (which is
stored earlier in the database).
GCM server will deliver that message to respected mobile device using device registration id.
This can also be understand using following figure
An Example Workflow:
So, from above images it easy to understand that whenever the android
application is first installed by the user, then it registers itself
to GCM server, and obtains unique GCM ID, then it's our Host servers
responsibility to keep this newly registered Registration ID of the
android user into Database, and then it will be used whenever server
side application willing to send the message to that particular
android user.
So, let us consider one case; suppose an Server wants to send Some data to Android User, which has already registered it's GCM ID 1234567 when it's first time installed, and as it's in the server's database the server application will fetch it from DB, and simply make a HTTP POST request to the GCM server in JSON format, which will have registered user's GCM ID along with the data to send , in same way the GCM Server has the record of all the Registered GCM/Android Clients, it directly forwards that message to the intended android user, and android app in user's phone will raise and Notification alert, to indicate an push notification has arrived.
Hope This answers an question!
GCM stands for Google Cloud Messaging.
Every push notification receive on any Android device is sent by the GCM only.
sender -> GCM -> Android Device
when sender sends an push notification then it goes to GCM. GCM receives that push and forward it to particular Android Device by its Unique device id.
GCM can't deliver Push without Unique Device ID.
while implementing push notification there are two important things, application key and server key ... these are unique Keys.. using these keys GCM identifies the application to whom push notification is related
I'm looking to create for my app some authentication. This will basically consist of a user registering (preferably with their google account, although not necessary) and providing a username that is then sent to my server and then having the ability to log in. I then from there would like to be able for the client app to communicate with my server by sending data and receiving push notifications via GCM (google cloud messaging). It is important that the server is able to send a push notification directly to a certain user not to all registered devices.
Would someone be able to point me in the right direction for what I must do? I've had a look at a few tutorials but what I've seen has always sent messages to all registered devices not to a specific user.
Some extra information about the app:
-The client app (user) will need to send a piece of information to the server. This information will also include the user that it needs to be sent to. Once the server receives the piece of information it should send a push notification through GCM to the specified user's client app.
I'm clueless as to how I need to go about setting up the authentication system, any help would be very appreciated!
Thank you very much,
Daniel
Offcourse you can send push notifications to selected users. This process would roughly comprise the following steps:
1) send the User's GCM registration id to your own server along with a unique user id (what ever id you have assigned to that user; could be email or sim id or something like that). Save it in your database.
2) To send Push notifications to selected users, choose users from the database based on some criteria, retrieve their GCM registration ids from the database and forward them the GCM server.
but there is one important thing you must handle. GCM registration ids could change e.g if user uninstalls and re-installs the application. You must update you corresponding database entry in that case.