1

I have a script on server which sends sms or telegram messages to users. I want to send sms when user is offline and send telegram message when user is online.

Is there any way to detect whether android user is online or not from my server (maybe sending request to already running some app?).

Note, users are willing to share some credentials if necessary.

Android OS version: Api 21+

SpiralDev
  • 7,011
  • 5
  • 28
  • 42
  • What exactly do you mean when you write "device is online"? – Henry Jan 09 '18 at 08:59
  • @Henry Device is connected to the internet, so that it can receive telegram messages – SpiralDev Jan 09 '18 at 09:00
  • As far as I know that is not posibble. You must install client application to achieve that. – HendraWD Jan 09 '18 at 09:01
  • You should have your code running on this device. i.e You need a application which can communicate to server . – ADM Jan 09 '18 at 09:02
  • 1
    What I normally do is to set a timer in Android, to fire every X minutes/hours, to make a POST request to my PHP script. This script just updates a column in a table with the current timestamp, so I know the last time the device was online. If the app timer is set to 10 minutes, and the difference between the current time and the timestamp in the database is longer than that, I consider the device being offline. – Cristian Jan 09 '18 at 09:02
  • Maybe you could make a workaround by using Telegram API https://stackoverflow.com/a/38033267/3940133 to get read/deliver status, then send SMS based on the read/deliver status after x minutes. – HendraWD Jan 09 '18 at 09:10
  • @HendraWD Thank you. Yeah, thought about that way too. But, that answer is not complete like others. – SpiralDev Jan 09 '18 at 09:14

1 Answers1

3

Well, I created my own app.:

  1. I used firebase cloud messaging to send notification (as it is lightweight and based on event-driven model) to devices from my app server. Byw, telegram uses gcm which is younger brother of fcm.
  2. I send only data messages (only payload) to my app from my app server so that I can get my app notified about notifications even if it is in background.

  3. When app receives notification, it sends upstream message so server knows that user has received message.

  4. If server doesn't receive response from user within 5-10 seconds, then user is most likely to be offline.
SpiralDev
  • 7,011
  • 5
  • 28
  • 42