Send data from a web server to an android app - php

I'm trying to understand how I can send data from a web server to my android app. This isn't a request/receive type question, what I am looking to do is basically have my web server (say for example www.example.com/index.php) send a data (like a string for example) to my android app. Now, this data can be sent anytime, it isn't something where a you click something on the app to receive the data, but rather, when the server is wanting to send data, it sends the data, and my app picks it up. Now I'm not looking for my app to be listening for this data every second, an interval of 5 minutes or so is fine.
The question being, how would I achieve something like this? How can I send a string containing "Hello World" to my app? I know PHP, and I know this has to be done using POST/GET, and of course I know there will probably be some type of listener, service, or something of sort on the android side listening for this data, but I just want to know how I can approach this. What should my setup be?
If someone can provide some link or code to send a basic String message, then that would be more than enough for me to learn from.

There is two best approach you can follow.
from server to app: in this case make corn job(timer service) which send push notification to android app. when app get push notification create one background service which download any data from server.
Note: if you have small text with limit of 100 char then no need to create service just send using push notification
fetch data from server: in this case make one background service(auto start service) in android app. which run within some interval period like 1 hour or more(whatever your requirement). that service fetch data from server.(in this case android app eat battery of user device because of background service)
i will suggest you to use 1st approach because its easy and best way(in case of performance) your server do everything & android app have to just receive data.
how to implement push notification best tutorial using PHP & android(its old but just read official docs)

For this you need implement android push notification.When you send the data from php server then the application get this data automatically.

yes if you want to send data into your app from server side to app without calling web service on click of button you will use cronjob which run on defined time from server side(As i.e : if you want to run it every five seconds or minutes it will possible you don't need to call it from app side).

Related

sending automatic push notification from mysql to android mobile

I have a database that contains some data and i want to get notified automatically on my mobile app if any value in the database is changed without having to check the database every certain time,i have tried many methods and viewed several tutorials but it leads me no where,any help ??
This is not going to be possible with only database. You need to write down an RESTful API which your app needs to poll to look for changes.
Ideal way of doing it is using GSM to push notify your app when ever you change something in your app. This will again require PHP/ASP/NodeJS etc... There must be some way you are updating your database? This should go exactly there.

Storing and displaying data in Cordova app

I'm creating this Cordova (Phonegap) app where I store users personal data (username and scores) every 60 seconds after which I want to display all users data to everybody (like scoreboard). For this I obviously need something to first gather all the data from users and then showing it to everybody.
I tried sending data with ajax to php for database storage. Then I tried sending data with ajax to php file to write the data in file, and finally realized that ajax won't work locally(?) (yes, I'm a novice). How do I create this kind of "global memory" that is accessible by all the users? How this is normally achieved in apps? I'm open for ideas.
Thanks.
You need a backend service, and call it from your application with webservices. You should send the data to your server (via webservice), store it in a database, and generate another call to send the scoreboard to every user.
Google about Rest APIS, choose the one you like.
For storing into the app, using localstorage is OK if the data is not too big. If it is, maybe you need to use SQLite, accesible from the cordova API.

linking android application to the mysql database phone and link the info as well

Hey guys im faced with a challenge of linking my localhost mysql/php when adding something in a form that form should also be linked with my android application.
ex. I have made a form for the admin whereby they could add a new bus route and it will be stored into their database using php mysql in localhost server now that new route should also be linked to my android application it should display the new route (My android application uses sqlite database so two different database here) so when users use the application when they search for new route it should also display as well
How can i go with that? any ideas please im really stuck in this
thank you
Well there are generally 2 approaches to follow when you want to 'Sync' your web database to your mobile database
Push Notifications: You could send a push notification to all your users, whenever a new route is added to the mysql db. When the notification is received by your app, (it's not necessary you show the notification to the user) you could just ping the server for the new route, or you can send the 'route' in the message part of the push notification if you can, and then add this new route to your sqlite database.
AccountManager: Using the AccountManager is the preferred method when you want to Sync your data. The AccountManager uses the SyncAdapter to Sync your apps data with it's cloud. When Android pings the servers of other apps for syncing, it pings your app's server too, and syncs your data. This method also takes care of issues like, if the internet connection was not working when it was intended to sync, so android will schedule the next sync soon.
To provide a good service, you should take a look at a client-agnostic API architecture.
In this architecture, you have a PHP/go/whatever-written API, that can or can not be REST (but it's reccomended), that will provide, given a HTTP petition, the desired result.
Using PHP in the simplest way, you should create different .php files, one for each action ( www.myserver.com/getRoutes.php, www.myserver.com/getRouteDetail?routeId=3), and make those PHP files query the MYSQL database and return (echo) XML / JSON / Raw text.
this should then be parsed by your mobile application, and added/updated to your SQLite database.

Update app data from server (Swift)

I need some help with refreshing data. I have an app in Swift 2.0 and I want to the server send an action and execute a method in the app. I already try with push notification and It works greate but if the user disable the notification the method still execute? and according my knowledge this method is only execute when you press the notification. What is the best way to acomplish this?
I am trying to avoid that the app constantly request data from the server.
My server is in PHP.
Thanks!
I think you can use protocols/delegates.
Delegates are good ways to be notified only when needed and and alternative to notifications.
Go check for more here :
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html
With this scenario I think we have these locations to request for updates:
At appDelegate, overriding applicationDidBecomeActive, called when the app becomes active in foreground.
In the view didAppear from the main view from our app (the first view that appears when we open our app).
Both options make more requests to the server than they should, but maybe the 1st is the least costly because is called just when app becomes active, and this is a good moment to check for updates.

NodeJS with PHP application and user session

I have a PHP application (with Symfony2) and I need to let my users talk, and other tings, in real time (with socket.IO).
Let's focus on the chat mechanism : A logged user can talk with an other logged user (logged with FOSUserBundle in Symfony). When the user sent a message, it must be saved in MySQL and sent in real time to the other user. So the message is linked to two users (a sender and a receiver) in my MySQL database.
So I have two possibilities :
I use PHP to store messages :
I have an event on the click "submit" and call a PHP url with AJAX
If my PHP return "OK" (so he correctly added the message in database), I emit a Socket.IO event -> let doctrine deal with datas and symfony with my user
On NodeJS side, I have a listener on this event and I sent the message through Socket.IO with an other event
I use only NodeJS :
How can I log my user on PHP and NodeJS side ?
I need to use a different database for datas in real time ?
How can I share my users between NodeJS and PHP ?
I don't know what is the cleanest solution, if someone can help me.
Thanks !
Can be done using a common data store like redis to store user sessions
Check this link for reference
http://simplapi.wordpress.com/2012/04/13/php-and-node-js-session-share-redi/
Also check
http://www.slideshare.net/leeboynton/integrating-nodejs-with-php-march-2013-1
Which gives you an overview of how sessions can be shared using a common in-memory data store and some sample memcached code.
WebSockets
If you are using nodejs just for the WebSockets part, I'd suggest not to because then you'd have to replicate your user authentication logic in nodejs as well
You can instead do WebSockets in PHP using some library like http://socketo.me/
As far as storing the messages in MySQL goes
Your first approach of making an AJAX call to store the message in database, waiting for the response and then emitting a SocketIO event would lead to a sluggish chat experience.
I'd suggest you store messages to MySQL through NodeJS (asynchronously)
On a side-note check https://github.com/kyle-dorman/ChitChat.js for adding real-time chat functionality to your website.

Categories