1

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.

Divyang Shah
  • 3,577
  • 5
  • 14
  • 27

2 Answers2

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 :

  1. 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.
  2. 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