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:
.
Related
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.
I have go through several posts in the internet but did not get a straight and clear answer for this.
I am doing one app on android which needs data to be sent/received from a database over internet. So how I can send/receive messages from my remote database on my android device?
Is there any messaging framework available currently in the android to do this type of task?
One method I thought of is to have PHP scripts at my web server and I will call those php scripts from my android httpClient. But its a bit tire some and error checking also a bit problem and retry mechanism also not there.
Please suggest a good way.
Google have a service for that.
Google Cloud Messaging for Android
Google Cloud Messaging may be suitable just for sending push notifications to android devices and things like that. And XMPP could be used to send messages from one android to another. So what you have taken, using a PHP script and calling it using a URLConnection or HTTPConnection is a better way to make transaction with a remote database
I am creating a app wherein user shares some information. This data i am storing it in database through servlets i.e. i am calling my own servlets which will take data through url and store it in database. So i want other users of that same app to get notify that some information is available and in turn they will get the information that other user has updated.
For this to work we can use polling or pusher. But polling will take lots of battery power.
I have tried C2DM but its not working for me. So i am thinking for some other mechanism by which i can implement other than C2DM.
Please suggest some way to work it. and e.g. if u have came across.
C2DM is deprecated, the new version is called Google Cloud Messaging (GCM) and is exactly what you need for your use case. There is even a GCM Demo Application which uses a Java servlet. The source code can be found here, you browse it directly or if you prefer GitHub you can use this repository.
The servlet code can be found in the file
samples/gcm-demo-server/src/com/google/android/gcm/demo/server/HomeServlet.java
If you follow the instructions in the Getting Started guide you will soon have a working application. I would suggest you not trying to copy paste it together, you have to get a clear understanding of how the GCM architecture is working.
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.
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!