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)
Related
I have two version from my app one for the customer and one for the client I need to send push notification to the specific client(in client app) when the customer take an specific action (in customer app).
I know that I should use Firebase Cloud Messaging as it's a new replacement to GCM, but I can't figure out how I can do this in both server side and client side.
you have a lot of options here!
let's start with the server side:
you have two options HTTP or XMPP, HTTP is a one way connection XMPP is two way.
for your app you will need XMPP for upstream and down stream messages
check there Guides it's really great: Server guide
there is also a Java and Node.JS Admin SDKs but i don't know much about.
but there is a great Tutorial which describes the whole proccess if you go for Node.js
So what happens is:
when a device sends an upstream message
Firebase will forward it to your server
Your server should handle the message and send it to the other device through Firebase cloud messaging
you can implement a server-less app using real time databse but you will not receive message in background, but when the receiver app is running it listen to databse changes and send notification to user
you can also make a server listens to databse changes "instead of receiver" and take actions upon it"send a message to the other device"
Edit:
with the new cloud function feature you can now use it to send notifications to users based on database events,
so you can add your message to database in a class
and make a cloud function to listen to this class write events
and whenever a message saved to this class, it sends it to desired
receiver
so now you don't need an external server to send notifications to
devices
check this cloud function sample, it's an example of sending notifications based on database event.
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.
looking for links/resources and approach on connecting to server through web service API periodically in background and check for availability of updated data on the server instead of parsing server response every time.
I have a server which has some data and will be updated in a month time. I exposed a service on top of it and it is used by android app. So, to reduce server load & improve battery, I would like to call the service periodically to check latest data is available or not.
I really appreciate if anyone can help me out on this.
Your best chance would be to use GCM messages from the server, in such case a logic that could be used is the following:
Android devices registers with the GCM service and requests initial data
Server logs the last time a specific Android device has updated each data
On each data update, the server sends a message to the devices registered
Devices request new data
Server uses the last update log to send only new data and updates the update log
Another nice option, would be to use the Android Sync Adapter with longer refresh intervals.
On each response from the server, send the date of the last update
Devices will send this data along with the request for updated data i.e. /request/data?last_update=[epoch_here]
Server searches for data that are more recent than the date specified and if it doesn't find any it responds with a 304 Not Modified
You could combine the above options, for faster updates using GCM and very long intervals for cases where the device does receive the GVM message, i.e. if the device is not used for a certain period of time.
Polling periodically can cause phone to drain battery fast. Google Cloud Messaging (GCM) allows server to push notification on devices. So when the update is ready, you just tell the phone and it connect to server to grab the update.
Quoted from Android Developer Page about GCM:
This could be a lightweight message telling your app there is new data
to be fetched from the server (for instance, a movie uploaded by a
friend), or it could be a message containing up to 4kb of payload data
(so apps like instant messaging can consume the message directly).
Our organization has an Android app and an iOS app.
We want to start pushing notification to these apps.
Android has GCM.
Apple has APNS.
But we want to create an API which will work on both android and iOS.
What is the easiest way to setup a server so that when a push notification needs to be sent, it knows exactly which server to send the message to?
I use a service called Parse to do my notification pushes to both Android and iOS. They have great documentation and libraries available. You can get some details here: https://parse.com/products/push
As a little background this is for a university setting where multiple colleges apps as well as distance education may be using the service. Here is the approach that we are using in our organization. If you look at the way APNS works it can be used by just sending a web call to the APNS service with the token id. GCM is very close to the same type of system. Basically create a JSON package and send it to the desired service.
Here is our steps we used to create this service.
Server admins created a server and database that can be called that will collect the tokens from both android and ios devices. When the device registers we also send what type of device it is. This is possible since we are just sending data to the database that is has been created.
From here we then created a couple of python scripts that send the data do the desired service whether it is ios or android. These scripts gather the appropriate data from the database and sends the packaged data (JSON package) to APNS for ios message and GCM for google cloud.
We also created a web interface so that those who need to send messages to the devices can.
The rest of the implementation is up to you to decide the best way to utilize the service. For example when to check for invalid devices,
Because we are planning on using this same server for multiple applications we can send the type of device, token, application, or whatever else is needed for an application to distinguish it from others we produce so that each application that wants to use the service can. I hope this helps and gives you some idea on how to accomplish this.
For APNS, Maybe you may consider this forked version of PyAPNS that has enhanced message support.
https://github.com/jimhorng/PyAPNs
which means it will catch error response for failure messages and resent the message which are discarded by APNS while sending between failure messages and receiving error response.
Solution:
Non-blocking ssl socket connection to send notification without waiting for response.
A separate thread for constantly checking error-response from read connection.
A sent notification buffer used for re-sending notification that were sent after failed notification, or arbitrary connection close by apns.
(Reference to non-blocking apns pull request by minorblend, enhanced message by hagino3000)
Result:
Send notification at throughput of 1000/secs
In worse case of when 1st notification sent failed, error-response respond after 1 secs and 999 notification sent are discarded by APNS at the mean time, all discarded 999 notifications will be resent without loosing any of them. With the same logic, if notification resent failed, it will resent rest of resent notification after the failed one.
For GCM, you may consider https://github.com/geeknam/python-gcm
For generic wrapper that support both or more mobile provider:
https://github.com/Redth/PushSharp
Server authenticates against google client login to use app engine's C2DM server. I understand that. I will use .net here. When data changes, I will send push notification to client and client service will refresh the data in their device db and UI will be updated. I am expecting this way the whole thing will work
All clients needs to register to server for receiving C2DM calls. Here there are interesting scenarios.
In my app there is a login in order to manage authentication and authorization to filter out the data that is relevant to him only. so at the same time I have to register to C2DM server as well. Is that correct?
Let's take an example of task which is assigned to some user. If the
task is updated by user, it show send C2DM message to it's owner, and
it owner creates task it should send C2DM message to user who is
responsible for the task.
Is this practically possible? Is anything that I am missing or understanding wrong?
Yes this is practically possible, but remember
you can send only limited data in your payload.
you have to write all logics inside your app to change UI.
Payload message data can only pin the decision making process.