How to get messege from server with less data usage in Android? - php

I am writing a Android program that get message from server. I can refresh my program every second to get message from server, but I don't want to do this work.
Is it possible to send message from server to client IP and then get message with my Android program (that installed in client device)?.
If it is not possible, please say me way to get message from server.

You can use this
1) Use push Notification in your app
2) When user click notification refresh your app (make a http request to your server fetch the update info).
OR
If you don't want to show notification in mobile which is no harm (almost every news app does this). Just don't create notification refresh you app.
for more information refer this
GCM
pushnotification in android

Related

Show Message on Android App in Real time

I am developing an application, where a random number is being generated in an android app, and after that, I am inserting that code in Web Base app.
What I am looking for is, when I am updating the value from web app to MySql database, I want at the same time, to show a message on android app that number has been saved, without doing any action in Android app.
signage-e36e0: null
I have tried Firebase, but I don't think Firebase would be needed.
Any help would be appreciated.
You can show a message in the app once the Webcall for sending rendom number is finished from app side you can know that event in HTTP Client you are using.
If it's not possible for you to listen to HTTP Response then You can utilize Firebase Cloud messaging(FCM) for this.
When you change the value in the database you can send the message to the app via FCM and app will show a message on the screen based on that message.
https://firebase.google.com/docs/cloud-messaging/

Getting "InvalidTokenFormat, Error 400" from Firebase Cloud Messaging

I am using Firebase Cloud Messaging for my android development with PHP as server side language. Right now, i'm able to :
Receive push notification sending using Firebase Console either both real apps and emulator.
Got it working when testing it out from terminal(curl)
So then, i tried sending from apps server(using php curl) instead of Firebase Console. And right now, encountered this weird message InvalidTokenFormat, Error 400 every time i'm request the POST request. Any thought?
Here is the link for Firebase Api were made : FirebaseApi Class
Feel free to ask for any inputs from meif above use case didn't clear enough. Thanks.
My mistake. The url should be(without slash at the end) :
https://fcm.googleapis.com/fcm/send
you need to send request to the firebase from your server with message of the notification and the token of the users then firebase will accept the request and broadcast the message to all the users (tokens that we provide) in form of a notification .
Firebase and device works on poll model, hence only firebase can send notification on your device , you can setup your poll with the device but google does not allow that because if each and every app starts to make a poll tunnel with the device , it will take up whole lot of resources.
Can you Post the raw request curl sends to the server? Normally curl does provide a printout for that, not sure about the php version.
CURLOPT_POSTFIELDS does not want a JSON. if you want to send a application/x-www-form-urlencoded request, use http_build_query($message) , or if you want to send a multipart/form-data request, give it the $message array directly.

Get data from a server with Objective-C

I'm trying to make a notification system between a PHP server and an iOS device, just like in the Facebook App.
I already know how to make a PHP request via GET or POST and wait for a response. But I would like to receive server's responses without any request, is it possible? Or should I make a request every x seconds?
Look into Apple Push Notification Service (APNS). You send a message to the APNS server, and the server sends a notification to the device(s) you specified. This is standard practice for apps like Facebook.
When the device receives a notification, it sends it to the app (if the app is running) or alerts the user (if it's not). The user can choose to start the app, at which point the notification is delivered. The app can then respond by contacting your server to update its information or whatever.

Sending a notification to the server when the android app if closed

I have created an application which is used to locate other people and a server... Now if anyone closes an app by Advanced Task Killer (This app is used to kill un-necessary application) how do i send a notification or a message to the server saying this person has closed the app.
example:- I have an app which detects other peoples location. And 2 more friends of mine have it. There is a server which examines our location. Now if one of my friend closes the app as said above how do i send a message or notification to the server created.
Before callling finish(); (if ur calling) or in onDestory();
send "closed" as request parameter to your server url like
"http:YOUR_SERVER_URL/?status=closed" using HttpClient post request.
at server catch it as request.getparamer("status");//Its JSP code
if you want inform ur frends about this use C2DM as push notification.
how do i send a notification or a message to the server saying this person has closed the app
You don't. If the user force-stops your app, you do not get control.
Take a look at the activity life cycle. I think what you are looking for, is to call the server in the onDestory() in the current activity...

C2DM and PHP Server

I'm trying to write the server side of my android app that uses C2DM. I'm going to write it in php. I basically am just doing tests right now. I copied the chrome to phone example for the actual android app and modified it to my liking. Right now my biggest question is the registration.
Let me get everything straightened out.
When registering the device for the first time, the app talks directly to my server. The server grabs the deviceid and responds back with a 200. Right?
So, on the php side of things, I grab the device id like $deviceid = $_POST['deviceId'] Right?
add it to my database. then respond back to the app.. How exactly do I send a particular response back? I assume in the header? I'm unsure how to do that.
Please confirm or deny that I'm on the right track and clear up any confusion.
Thanks for the help.
You don't respond back to the app. You must register your server with google's servers to get a server authentication token. You then use that server token and the device token in a post request to google's C2DM service to have google's servers send a push notification to the phone. Your app and your server never directly communicate when a push notification is sent in C2DM, it is all done through google, yet you need to implement a mechanism for your server to know the device id of the device it wants to send a message to.
This is a pretty detailed guide, though the server code is in Java:
www.vogella.de/articles/AndroidCloudToDeviceMessaging/article.html
Make sure you have really gotten the IDs before sending to the server. If you are using an emulator for testing, you could do an echo over at the php script so the message will appear in your logcat.
Google refreshes expired registration IDs so your application should be able to pick up new notifications/ids from google and handle the message by sending to the database.

Categories