简体   繁体   中英

Android Service - SocketIO - GCM - Communicate with Activity

Hopefully this question is not too broad or too much to ask for...

I am building an Android app for my office. The app is used to send message alerts (as a custom "message" object class) to all users with the app (my office only). Once the message is received, all the names of the recipients are listed at the bottom with a status icon showing their reply to the message (thumbs up or down basically).

I have a Node/Express server that is used to track, store and distribute the messages and log their history. When a user sends an alert message, it will go to the Node server which will handle the distribution.

Requirements of the app

  1. It is for emergency responders. The app must ALWAYS be able to receive an alert. If they have no service, that's ok, an SMS will be sent as backup. But all the Android phones must ALWAYS be listening to my Node server for updates.
  2. The app MUST start on boot, or at least a service or something to listen to the server. This is based off the last requirement that it must always be listening to the server.
  3. Once the message is received, the user will view it on a specific activity. This activity lists all the recipients and their status (thumbs up or down). This screen will need to be connected to the websocket at all times and update status real-time as the other users are submitting their status.

Here is my question

There are a few ways that I have seen recommended, but I don't know which to use.

  1. Create a background service (service or intentservice... no clue?) that will start on boot and connect to the Node server.

    This raises a few questions:

    a. Is it ok to have a TCP socket connection like this on the phone 24/7?

    b. It will only be exchanging data a few times a day. Will the open connection kill the battery?

    c. Can I use this 'always running' service to start my application activity up in the event that the service receives a message from the server? Also, can I easily send the message from the background service to the application? (I don't know threading too well.... thats the hard part for me here).

  2. Use GCM to "trigger" the phone to start the app, and once the app is started up, use the main UI thread to create the connection to the server and pull down the message that was sent.

    a. I have never used GCM and only just learned about it yesterday. Is this the sort of thing it is used for?

    b. If I use GCM, do I still need to create an 'always running' background service that starts on boot anyways?? Or does GCM have some built in way of just always listening?

I do not entirely understand how to use threads, my Java knowledge is below average (I wouldn't say beginner though), and this is my first Android application ever. The app is looking beautiful and really coming along great, but I just don't know how I should handle this network being always connected .

Here is a drawing to help understand what I need: 需要数据流

Thank you for your replies and taking the time to read this behemoth of a question.

a. Is it ok to have a TCP socket connection like this on the phone 24/7?
b. It will only be exchanging data a few times a day. Will the open connection kill the battery?

A persistent server connection has several challenges attached to it. You definitely need to send keep-alive packets every few minutes or lazy routers will assume your connection is dead and drop packets when you actually need them (A problem that google themselves had ). And yes, this will definitely drain the battery because it needs to listen all the time. That is, if you manage to keep the connection open at all because Android has powersaving features that simply disable networking for background apps.

c. Can I use this 'always running' service to start my application activity up in the event that the service receives a message from the server? Also, can I easily send the message from the background service to the application? (I don't know threading too well.... thats the hard part for me here).

Yes you can. It's fairly easy to start Activities from a service as well and you can pass data like the message in the Intent . Wrt Threads, background services aren't actually running in threads but in the main ui thread. You can start threads in them though. In case of networking you even have to because the service would otherwise block the UI thread. The same and only ui thread that activities of that app use as well.


a. I have never used GCM and only just learned about it yesterday. Is this the sort of thing it is used for?

That is exactly what it is made for. It also solves essentially all above challenges for you.

b. If I use GCM, do I still need to create an 'always running' background service that starts on boot anyways?? Or does GCM have some built in way of just always listening?

No. You define a <receiver> / BroadcastReceiver which gets triggered even if the app isn't running. The system's GCM service (part of the Google Play services app that you need to have installed) does all the waiting for messages for you. From there you can start activities / services or (the probably best thing to do) show a notification that when clicked opens the app. Opening activities out of nowhere from background events is very disruptive. You can btw even put the message itself inside the GCM payload if it isn't too large so you could potentially save 1 web request and speed up the initial display.


GCM usually does a good job at delivering messages quickly but as you can see from eg http://support.whispersystems.org/hc/en-us/articles/213190487-Why-is-there-a-delay-in-receiving-messages- or https://threema.ch/en/faq/push_andr there are circumstances that result in delays. Unless you have very special requirements and you don't care about battery, don't implements your own as in 1) - use GCM.

There is btw no way to know whether phones did receive the GCM message built into GCM. So you could send backup SMS after a timeout, when devices didn't connect to your server)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM