I would like to implement GCM in my Android app:
One Android app
Registered- U_user and another M_user.
I have Google Sign In in my app thats works fine with JSON and GSON and local MySQL database server with tomcat-server restutil to keep storing Gmail login users.
So, I want to send a notification from U_user to M_user.
U_user does a registration that has been pushed to M_user.
How can I do this?
I am assuming you mean the same Android app installed on different phones (U_user's phone and a separate phone for M_user).
To quote a section in the docs:
To make sure that messages go to the intended user:
Your app server can maintain a mapping between the current user and the registration ID.
The app can then check to ensure that messages it receives match the logged in user.
What this means is that you can send messages to another user through GCM. To let the app server know what to send to M_user, let U_user send an upstream message to your GCM app server (only possible in app servers using XMPP - see here). Alternatively, you have the option to do POST requests to your app server via HttpUrlConnection (can work on both XMPP- and HTTP-based GCM app servers).
Regardless of which method you use, specify in these messages to the server that you would like to send a message to M_user.
Related
I just switched my app over to firebase 2.0 from parse. Basically I have an android app (main app) built for the general public to use. Then I also built a second app (manager app) that only I and a few others have access to which is used to update the content of the main app. Now I am adding in notifications which work fine when I send them from the firebase console, however, is it possible to send them from the manager app to the main app? If not, what would I need to do in order to send them from somewhere other than the console (I don't want anyone else to have access to the console but would like them to be able to send notifications.) Thanks!
Seems like you are looking for device to device messaging. This is not currently supported by FCM, so you will need some type of server.
The server could implement XMPP in which case it can be a relay, upstream messages from admin client will be converted to downstream messages to non-admin clients.
The server could watch the Firebase Relatime Database and then the admin client could write something to the database and the server watching the database could then take action and send notifications to non-admin clients.
I am trying to make a simple messaging app that has a mysql and php server with an android app. My back end revolves around mysql to store and php to communicate from the database to the device and vise versa.
Now, what I am trying to accomplish is this: sending device->GCM->target device.
What I had in mind was that the database I created stores the gcm Id for all user. That way, when a user wants to send a message, their app sends a message to my database to be stored and retrieves the targets gcm Id and then sends the targets gcm id to the gcm servers to create the push notificiation. When the target receives the gcm message, it sends a response to the database to receive the actual sent message.
Is this possible, and how would I go about doing this?
I already have a gcm receiver implemented from here:
https://github.com/codepath/android_guides/wiki/Google-Cloud-Messaging
No. Don't do it.
For any internet connected device (server or mobile) to request a push to a device, it needs to send a POST request to the GCM address (https://gcm-http.googleapis.com/gcm/send) passing the server API key and the device GCM reg id which should receive the message. And having any of those values available on a device is a security risk for your application.
Having your registration ID, means ppl could easily "copy" your ID and start sending messages from their servers on your behalf.
The GCM device registraion ID, means anyone could start spaming your users, and you certainly don't want that.
You could look into GCM Upstream (https://developers.google.com/cloud-messaging/upstream) but that only means your client code will be easier, as it's as simple as calling gcm.send(String);, but you still have to handle that on your server application.
The correct way is to have on your server a table that maps userId with gcmRegId and have devices send to your server messages to their desired userId. Your server should process one device "send" message and create a push to the other device. That logic should be fairly simple on the server side, after you already have a whole chat application developed.
Can anyone help me with a way to send requests as a notification through my android app to all the users who have installed my application and then their responses are sent back to the user who sent the request.
I have read about GCM but I dont understand how can I register all the users to get their GCM registration ID and how can i simulataneously send a notification to all users..I may sound naive but I am completely new to this GCM concept and I dont think that it is the exact thing what I am looking for..
So,somebody please tell me how to send notifications(simultaneously on click of a button or something..) to all the users who have registered in my android app .
You will need to build a server component that keeps track of all registered users. This component will be an app that you write and expose in the cloud. There are many app-hosting services to choose from. Amazon EC2 is one example.
So the app flow would be something like this:
User launches your Android app.
Android app registers itself with GMC. GCM will respond with a token that represents that device.
Android app POSTs that device token to your cloud application.
Cloud application saves that token. The app should now have a list of tokens that represents all active devices running your app. (of course you may want to have an expiration policy - i.e. remove all tokens corresponding to devices you have not heard from in say 30 days).
One of your app users posts a message that s/he wants to broadcast.
Your Android app responds by sending a request to your cloud application.
Your cloud application responds to this request, by making a request to GCM. In this request (or series of requests), the app will include all device tokens and the user-entered message.
GCM responds by pushing the message to all devices with your app (i.e. all of those that have register with GCM and received a token - see step 2).
If using GCM alone you would need to write a server component. I get the impression you don't want to do this. You could use Urban Airship push messaging, which will allow you to send out a message to all registered apps from the Urban Airship web portal. Urban Airship integrates with GCM (which is easy to setup). You would still need to add code to your app to handle the push notification the app receives.
http://docs.urbanairship.com/build/android.html
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)
I m planning to build an android app
the specification of this app is:
1) it should have this ability to send data suach as (new record in database) to the server
2) server also should able to send data or some information to the specific or all android devices
i dont know should all the android-side user register to GCM or not?
if all the user should register to the gcm it is maybe difficult for some people!!!
please give me step by step process about gcm
from client side and server side
You should implement GCM if you need the ability to push information to device and it would be too costly to implement polling. The guide for GCM integration is quite good: http://developer.android.com/google/gcm/index.html and also all the boiler code can be created for you by saying New GCM enabled Android app.
I have done something similar:
Yes all users should have GCM implemented (but this does not require user interaction)
The GCM framework gives each device a unique registration id. Store this id together with other user information on server database.
When server receives a message from say user A with registration id 12345, who wishes to send the message to user B and C.
Now it is easy for the server to look up user B and C and their registration I'd forward the message, possibly adding the information that it came from A.