I am making an android app. We use JSON link to get data from the server to the android app. Eg: when a product is to be delivered ,changes are made on the server end but those changes are known by the app only when a GET request is made by the app. Since this GET request needs to be in a background service , I am using Alarm Manager . In the Alarm Manager , I have to set a specific time to check the changes in the json url like I have kept interval of a Day. How i can make it dynamic so that app would fetch a GET request when changes happen on the server end. My question is how will a background service know changes have happened at the server end in real-time. Please help.Thank you for reading.
Asked
Active
Viewed 304 times
2 Answers
1
Have you checked out FCM
Firebase cloud messaging allows a server to send async JSON messages to the app. Your app can receive the message by implementing the FCM Client and do anything.
Your options could be :
- As soon as changes made on the server, Send an FCM message with data body containing the JSON of changes. Might be inefficient since large data will need to be sent in FCM message.
- Send a minimal FCM message to app whenever server data changes. When the app gets this message, it can create a GET request to the server. So your GET request would only be needed whenever data has changed and the app has been notified about the change.

Siddharth Garg
- 1,560
- 14
- 19
-
im not using firebase , so i using GCM , i came to know about GCM after posting this question , my data is on a PHP server . – Divyang Shah Sep 25 '17 at 11:19
-
Well if you are integrating cloud messaging service fresh in your application, i would recommend FCM rather than GCM. https://medium.com/glucosio-project/whats-new-in-firebase-cloud-messaging-and-how-to-migrate-from-gcm-578019c2167d https://stackoverflow.com/questions/37311188/migration-from-gcm-to-fcm-needed – Siddharth Garg Sep 25 '17 at 11:33
1
You can use Foreground Service to run all the time and ping the server after 10 seconds. By using this you can achieve this. Or you can Use MQTT for push and publish messages between your server and App. You can refere to this link for How to Use MQTT in android Basic Steps for Using MQTT in android
For Step By Step Explanation https://www.survivingwithandroid.com/2016/10/mqtt-protocol-tutorial.html
Here is the link for MQTT Service to use in Android. https://github.com/eclipse/paho.mqtt.android

Sardar Khan
- 845
- 6
- 16
-
will try Foreground service but isnt it too labour intensive for the app and i guess it cant be used for push notifications , so MQTT might be useful as u have said , but i am using GCM , anyways thanks for the help – Divyang Shah Sep 25 '17 at 11:20
-
ya surely it was helpful ! I thought MQTT is only used for I.O.T projects , i learnt that it can be used for android also – Divyang Shah Sep 26 '17 at 06:56
-
So kindly accept my answer so that others can benefit from it . Thanks @DivyangShah – Sardar Khan Sep 26 '17 at 07:20
-
-
ok understood but cant accept it since its not completely relevant MQTT is more related to I.O.T than android anyways thanks – Divyang Shah Sep 26 '17 at 08:03
-
ok.You can use MQTT other than IOT as well we are also using this thats why i refer to you to use this.Anyways Thanks @DivyangShah – Sardar Khan Sep 26 '17 at 08:06
-
and for the answer to be accepted you should have given atleast little detail or a link to be followed in order to get it done rather than just a name of a technology , – Divyang Shah Sep 26 '17 at 08:13
-
-
I have Edit my answer to fullfil your needs to accept the answer if any thing is not understandable let me know. – Sardar Khan Sep 26 '17 at 08:17
-
1whats the big thing about an acceptance for an answer ? do u get too many credits ? – Divyang Shah Sep 26 '17 at 09:57