Android: GCM server side & sending backwards - android

I am trying to learn GCM and is currently reading the documentation:
connection servers take messages from a 3rd-party application server (written by you) and send them to a GCM-enabled Android application (the "client app," also written by you) running on a device.
Now in my other applications that needs a "Larger" amount of data i have done the following steps:
Send data to server (HTTP post)
Recieve data from server (JSON) (using GSON)
Now as far as i can read from the documentation what this will do is add a middle step which is only usefull if the device i am trying to send to is offline.
my question is:
I am trying to create an instant messaging application, what should i use on my server side? is PHP enough to just instantly send the data to GCM or should i use another server software that can handle these huge chuncks of request, such as Message queue?

Related

One to One Android Chat using Firebase

I need some clarifications.
I'm using Firebase realtime database.
Now I've to implement a realtime chat between two android devices. I need also push notifications.
So, the solution based on realtime database is to drop because if the app isn't running, it will not handle the onChildAdded event.
I read on the documentation that I can achieve my aim, using the upstream message from the device to the server.
Now, it's written also that we need an app server.
I just want to be clarified the need of the app server (XMPP or HTTP), and which is the entire flow of message m1 sent from the device A, and the notification on the device B.
In my opinion the app server should act as a man in the middle, so:
A ---> XMPP SERVER ---> FCM
FCM ---> XMPP SERVER ---> B
Is this the flow?
On the documentation I can't understand if I'm right or not.
If so, how do I send a message from the FCM back to the XMPP Server?
The notification message is sent from FCM to the client o from XMPP Server?
Please someone clarify me.
Other solution thought by me:
Since I find really powerful firebase realtime database, I thought these solution to walk around the problem:
Create an app server the is listening for onChildAdded and when it changes send a notification to client device.
Creating a background service on the client app listening to onChildAdded and create notification when necessary.
Even all, I don't like these solutions.
Just want to understand the standard and correct way to achieve my aim.
Firebaser and author of the article debated in the question comments here
To send messages to a device with Firebase Cloud Messaging, you need to specify your project's FCM Server Key. As its name suggests, this key should only be used in code that runs in a trusted environment: i.e. a server that you control.
To send device-to-device messages (such as in a 1:1 chat application where the receiving user is not necessarily online) you need two steps:
one user send an upstream message
the other user receives the downstream message
Using Firebase Cloud Messaging and your custom app server code, you can handle step 2. But that still leaves step 1: the user needs to send a message that somehow triggers your code on the app server. There are quite a few ways to do this.
For example you could implement an HTTP endpoint on your app server and have the chat application send the messages there too (in addition to sending to the database).
You could also implement an XMPP endpoint and have the chat application send the message there (again in addition to sending it to the database).
My article suggest yet another way, one that doesn't require implementing an endpoint at all. It uses a node.js script that runs on your app server and is essentially just a client to the database. Whenever this script detects a relevant chat message, it calls the FCM API and sends the downstream message.
This pattern of using the Firebase Database as your endpoint and then using server-side scripts is quite common when using Firebase. We documented it in our classic blog post Where does Firebase fit in your app? (as option 2) and in this article in the Google Cloud documentation.

Implementing a chat application server for clients on Android/Windows Phone

I need to implement a group chat server which can support more than 50 users at a time.
The users will be only on mobile clients which include Android/Windows Phone.
Sending push notifs to the apps will not be difficult, as the server needs to do a basic curl request. So "polling" is not an issue.
My current skill set includes.
Flask
Google App Engine( I intent to use it or a web hosting
server.)
Basic php
So from what I know, I can make http requests to the server with user specific information+chat msg.
The server processes this and sends a broadcast using push notifs to all the devices stored in database.The server identifies the device from the data in the http request.
How efficient will this be?
If this is not the correct approach, what all do I need learn(sockets programming?) and any framework which can make my work easier (it will help if it is also supported on GAE).
I think you need XMPP messaging. It's support by App Engine but not in PHP : https://developers.google.com/appengine/features/
XMPP permit to send and receive chat messages.

How does GCM work ? (google cloud messaging for Android)

I have an android app which i connect to my server using REST API (django rest framework)
here is a scenario(and maybe my plan):
data is sent back and forth as json
I have a user model and a task model where users are owners of some task.
Users typicaly sends a task to another user (with json similar to this: {"owner": "exampleuser", "from":"otheruser", "content":"example" ...} using POST method)
-The tasks has a boolean field "completed" and is deleted once the task is completed (using PUT or PATCH method: completed = true,)
once a new task gets created using POST method, the only way users can see any activities concerning their tasks is through an android activity that uses GET method to get a list of all the tasks owned by the user, by looking up all objects owned by the user
So my questions are:
Instead of having the user to check the app everytime. How can I use GCM to push notify the user?
How will it tell which user or device to send the push notification to?
How does GCM knows when there has been a change to tasks or when a user POST a task?
Android keeps one active connection to Google's servers, but it doesn't use much power or data, because no traffic is sent along it until something sends a GCM message to an app on your phone. There's only one connection on the phone, used by all apps: installing a new app that uses GCM doesn't add any extra load.
The first step in GCM is that a third-party server (such as an email server) sends a request to Google's GCM server. This server then sends the message to your device, through that open connection. The Android system looks at the message to determine which app it's for, and starts that app. The app must have registered with Android to use GCM, and it must have the relevant permission. When the app starts, it might create a notification straight away with the data from the message. GCM messages are very limited in size, so the app might instead open a normal connection to the third-party server to get more information (for example, downloading the headers of new emails).
The advantage of using push notifications is that apps don't have to run at regular intervals to check for new data, saving both power and data. The advantage of having a centralized mechanism like GCM is that the device only needs one open network connection and the Android GCM system is the only thing that needs to keep running, rather than each app having to stay running in the background to keep its own network connection to its own server.
As per the GCM implementation, it requires that you implement a remote server which will manage all the requests, both incoming and outgoing. You can do this simply with a web server acting as a webservice, which will get (for instance) the requests from the clients with a HTTP POST request, and process act consequently.
Instead of having the user to check the app everytime. How can I use GCM to push notify the user?
This will be managed by the server that I just described. It will know who is subscribed and who should receive the notifications.
How will it tell which user or device to send the push notification to?
Same goes here. The server, upon a subscription, should store the users in some kind of storage (a SQL database, for instance), and this way it will know who to send notifications. This implies you'll have to implement some timeout mechanism. In my case, I make clients send a dummy HTTP POST every 30 seconds. If I don't get this request from a reasonable amount of time, I consider the client timed-out and therefore I remove them from the database.
How does GCM knows when there has been a change to tasks or when a user POST a task?
Same story, it's all handled by the server. You implement the logic of what should happen upon any request.
You might want to read this: How to send location of the device on server when needed
And also this references:
Reference on Google Cloud Messaging
Android Push Notifications using Google Cloud Messaging GCM - Android Example
Google Cloud Messaging using PHP
Connection between PHP (server) and Android (client) Using HTTP and JSON
Notificaciones Push Android: Google Cloud Messaging (GCM). ImplementaciĆ³n Cliente (Nueva VersiĆ³n) (spanish)

Google Cloud Messaging for Android works from server to client, but also viceversa(from client to server)?

stackoverflow fellows,
Since I am very new in Cloud Computing matters, I would like to ask you a question: Google Cloud Messaging for Android works from server to client, but also viceversa(from client to server)?
I am asking you this, because I am currently developing an Android application. The structure, in general lines, it's something like: I would like to use my laptop as a server - and perform all the computations here. Then, from my server (laptop), to send everything in the cloud, and from the cloud, directly to my Android running device. I want to perform the computing and database storage on my local server (because, since I am a student, I won't have enough money to actually pay for a server from google (let's say), so I am focusing on what's free and suitable to my needs) and just communicate with my Android device using Google Cloud Messaging for Android. (I want my application to be independent from the hardware of my mobile device, and also, I want to write the program that will perform the computations on my server in Java).
I know that I can send messages from the server to my device(through cloud), but the question is: can I also send messages from my device to the server ? I am asking this, because the computations will be made after I submit something from my device (basically, my Android application will be an interface, from where I can send the data to my server (through cloud) and then receive (also through cloud) the results of the computation on my device)
According to what I've been currently reading (http://developer.android.com/google/gcm/index.html) it seems that I can only send messages from the server to my device, but I decided to ask, since, so far, was the only free messaging framework(using cloud) I have found.
Thank you.
You can use GCM Cloud Connection Server to send messages from device to server.
Paragraph from this page:
http://developer.android.com/google/gcm/ccs.html
The GCM Cloud Connection Server (CCS) allows third party servers to communicate with Android devices by establishing a persistent TCP connection with Google servers using the XMPP protocol. This communication is asynchronous and bidirectional.
Google Cloud Messaging for Android works from server to client, but
also viceversa(from client to server)?
Sorry but GCM(Google Cloud Messaging) is only made up for sending Push Notification from server to device. Not for Viceversa.
I know that I can send messages from the server to my device(through
cloud), but the question is: can I also send messages from my device
to the server ?
For sending the message or anything(For Example: Photo,files..) to Server why Don't you Use Web services for it. I think it will Be suitable according to your Needs.
It is correct what KashifB wrote:
You can use GCM Cloud Connection Server to send messages from device
to server.
But keep in mind that Messages with Payload have a size limit of 4kb per message.

Client(android) receive messages from server(java)

I am trying to create a chat application. The communication is based on RESTful web services(Jersey). The client communicates with the server sending HTTP requests. My problem is how to send messages from server to client without the client send a request first. I read about C2DM notifications, I suppose using this way, when the client receives the notification from the server then (the client) has to send a request to the server in order to receive the data. I also was thinking about using sockets but I don't know if it is possible as I am using Rest. I mean in the server side can I also use sockets(sockets run in lower level) with the REST code? The only thing I have though that could work using sockets is to create another server with socket(two servers RESTful and socket), but is this a good solution? Also could I put some server code in the client side and some client code in the server side. Would it be correct and effective? What do you think? Which solution would be the best?
C2DM has been officially deprecated as of June 26, 2012 by google. Existing C2DM developers are encouraged to migrate to the new version of C2DM, called Google Cloud Messaging for Android (GCM).
Check out this link for more info
http://developer.android.com/guide/google/gcm/index.html
The scenario you describe sounds ideal for C2DM. If you build your own sockets set up you will drain battery and CPU resources doing something the phone is already doing - maintaining an open socket to receive messages.
C2DM is relatively straightforward to set up, and in my experience is pretty reliable.
This is the best tutorial I have seen on it:
http://www.vogella.de/articles/AndroidCloudToDeviceMessaging/article.html
But you also need to read the official docs:
http://code.google.com/android/c2dm/
Your server-side code will need to send an HTTP request to the C2DM servers to send a message to a device. To tell it where to send the message, your server needs to specify the Registration ID (which represents your app running on one specific device) and so when your app sets up on the Android device, it registers with C2DM, and then needs to send its C2DM Registration ID to your server to be stored, ready to be used to send messages to.

Categories

Resources