Can gcm be used to send and receive messages between android devices? - android

I have two android devices that runs a gcm client app. I get the Reg IDs of both the devices. I also have a server API key. Now the thing is, Using a simple php code using cUrl or Zend framework, I'm able to send message from a web server to one of my droids (Of course I can broadcast too). But, with this in hand, is there any way to send a string from one device to another?

Quoting myself from my book:
You might be tempted to use GCM for peer-to-peer messaging, without a server of
your own. In effect, each Android app is its own server, using the same JAR you
might use in a Web app inside your Android app to send messages to some other
party. For example, you could implement a chat system without having a dedicated
chat server.
The danger here is that this would require your API key to be embedded within
your Android application. Anyone with that API key is perfectly capable of forging
messages from you. The IP address restrictions you could place on that API key are
unlikely to help, since your legitimate uses might come from any IP address, not
just some single server. Since finding magic strings in APK files is not that difficult
for those with the inclination, putting your API key in your APK file is a dangerous
move.
Hence, you will want some server of your own as a middleman.

One thing you could do is to make a POST request to your web server with the message and the registration ID of the other Android device, but that would be a little cumbersome as you'd have to figure out a way to retrieve the registration IDs of the other Android devices from your web server.
I would recommend looking into XMPP as this protocol has been built from the ground up to be an extensive messaging protocol. I'm pretty sure you'll find good XMPP server frameworks to implement your functionality.

Related

Device to device messaging using GCM without registering an app server

Is it possible for a device to send message to other devices using Google cloud messaging without an app server at all?
I have a centralized database using Google Cloud Datastore. The app will get required registration ids from the centralized database and the database is updated by all the devices. So, getting registration ids is not a problem.
Can this be done using upstream messaging? I am not sure because i have searched a lot but never saw an example where app server is not used for this purpose.
This question is not duplicate of another question, because here i have central database to store registration ids which is mentioned as a problem in another question.
In the most basic sense no, not at this time. You cannot send an upstream message from one device to another without an app server. You can create an app server on your device application but that is not recommended. The current recommendation would be to use an app server to facilitate your device to device messages.
To answer is it possible? Yes it is.
Sometime back I have created a POC to send GCM message to self, so I know its very much possible. I configured my GCM to work with any ip in developer console i.e. 0.0.0.0.
just use HTTP post to send your message from android as you would have done from Server.
Like (most) everything else, there is an API for that.
Google hosts its GCM service on GCM Connection Servers. The official document requires you to create an App Server to issue the API requests to the Connection Servers.
Alternatively you could setup the Connection Server to accept requests from any IP around the world, and then issue the requests via your client app. The API is here and it's quite simple.
Seurity issues
Bundling your API key with your app, and setting the Connection Server to unrestricted connections is an obvious major security issue.
Why bother?
Why bother doing all of this? Instead I would use a free service like Parse.com, which takes care of the users database and offers up to 1000000 messages a month free.
Has anyone actually done this. I would like to see example android project.

Where and how to save Android GCM project ID and api key?

I am working on Android gcm project. The docs recommend to save the project ID and api key in a server for security purposes. But all of the GCM examples in the web don't do that. How efficient would that be to save them in a server and grab them when needed in the app? Has anyone done that in any project? Can someone give me a snippet of efficient code?
Thank you
GCM implementations require you to have server side part that will be sending data to your devices. This acts as middleware between devices and GCM servers. Server side can be PHP/Python etc scripts, whatever. And to these scripts need these data to be able to send anything to your devices via Google servers. So your app is NOT fetching anything. Like this:
.

Sending data from ASP.NET to android

I'm creating a viber-like application, i.e. android users can make instant messages in their android-powered devices. I've created client-to-server webservices using ASP.NET (ASMX service with JSON response) and it works like a charm, but the other side, i.e. server-to-client requests is just a little headache. Of course I can make my application so that there is no need to server-to-cliend requests (sending dummy requests from client to server in short periods) but in this way server will be overloaded specially when number of users increases. I've found some technologies regarding instant messaging, including GCM and XMPP. What are my other options? or let me ask what is my best option?
Should I use sockets? If so, how can I always have my clients IP (considering that clients are mobile users which may be using WiFi or GPRS/3G/4G internet). I'm thinking of a system in which my clients send their IP each n seconds (n can be around 30), so server always has a (nearly) up-to-date list of client IPs.
Is it possible or even logical to do so?
You should definitely consider using GCM (Google Cloud Messaging for Android)
You need to setup a few things in your server to be able to use it to send via GCM.
Its the built-in way to send data from a server to an android device, hope this can help
Implementing GCM Server
Using the good old strategy of long-polling is always possible, but there are much better ways to do it nowadays. That's perhaps the fastest to implement of the mentioned (not so if you want to implement it correctly and with min amount of load on your server/DB engine). I've seen some projects that start around that architecture with the idea to move to something better in the future .... and the moment to move never comes of course. If you start browsing your Android phone connected to a proxy like Charles you'll see what I mean :)
WebSockets are an option, but I don't see a reason why I'd use them for a Viber-like app, which is event-based (you've received a message) and not "real-time" (monitoring some stock graphics movements for example).
Your best choice here is a Google Cloud Messaging (GCM)! It's easy to implement it both on your backend and on the client, and it's built specifically with the problematic connectivity in mind (switching between wifi/poor 3g/4g/etc). It has some pretty nice features that other methods don't provide "out of the box" - message resending (the common scenario of a client loosing his connection - GCM it'll send the push notification later, when he comes online), automatic syncing between devices (smartphone, tablet, etc) and others.

Sending and receiving data from an app over the internet

I recently created an app that sends and receives commands in the form of SMS text messages, being that sending a lot of text messages could become quite costly, i'm now looking for a new method to use to send and receive the data securely over the internet. Obviously one solution would be email, although I do not think this would be ideal due to emails stacking up and accounts etc.
So basically, i'd like some advice on what my other options are....
How would sending a command using TCP work in android and how secure would it be?
How would one go about setting up sending data over TCP?
Any feedback would be great, thanks
What your looking for is Google's Cloud Messaging service. GCM is the standard way to push messages to/from the client. It requires several components to implement, however. One being a database to store GCM ids. Another is a back-end to do this.
It essentially works like this:
User downloads and installs and opens the application
Your application sends a request with your application's unique identifier to google for their GCM id (have to register on google's app center site)
Google responds to the device with their GCM id
Your application then sends (via network services or ajax in a webview) their GCM id to your server, which stores it for future use.
All of these components, however, are good things to have for growth and infrastructure support. Postgresql is a good, open-source relational database.
Other options:
1 - You could use websockets (https://github.com/koush/android-websockets)
Websockets, however, could considerably draw down the user's battery, extremely quickly if the app communicates even if it's not open, like... 'whooosh'
The benefits of using websockets is that it detaches your communication from Google.
Depending on your principals and/or beliefs, you may choose to go 100% independent by using soon-to-emerge web standards for detachment. Google (lol) "Extending the web to support personal network services" written by John Lyle, Claes Nilsson, Anders Isberg, and Shamal Faily. It's a good paper on the mobile communication debate.

GCM Server/Sender Setup for Device-to-Device Communication in Android

I've been reading through the GCM developer page and none of it seems too complicated. However, from an overview standpoint, I'm confused on exactly what I want. I have an application on one device that will send some information to another Android device, which then does something with that information. It's just a pair of doubles.
I've never had any experience doing much web communication aside from simple website/router stuff, so I realize this is a very inexperienced question-- but when I set up GCM on my two devices, what device is acting as the "server" and which as the "client"? Are both clients and Google is the server?
Thanks for any help.
I think you Want to Make Two side communication. you can Do One think that Save your RegistrationID of phone on to the Server. what ever the Data you want to communicate you should save it First to server. and whenever the Notification sent by the Server it will give information to perticular Client with using Registration ID.
Both android devices are considered clients. The clients will communicate with a backend server which can be implemented in a variety of ways. The sample that Google provides uses a standard web app written for the Java App Engine. You can find the example here:
Setting Up the Server
You can also write the server using ASP.NET, PHP, etc. What I typically will have on the backend is also a SQL database to manage the devices that are registered with my application. Each device (client) is given a unique ID from Google which the server will use to communicate. The server uses this ID because the messages it will send is not directly to the client but to GCM service. So in essence the Google GCM is a server to your backend server which is a client to GCM.
Hopefully this answers your questions regarding client/server communication. Please look at Googles demo for detailed implementation. Good luck!

Categories

Resources