简体   繁体   English

Android服务-SocketIO-GCM-与活动进行通信

[英]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. 我正在为我的办公室构建一个Android应用程序。 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. 我有一台Node / Express服务器,该服务器用于跟踪,存储和分发消息并记录其历史记录。 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. 如果他们没有服务,没关系,一条SMS将作为备份发送。 But all the Android phones must ALWAYS be listening to my Node server for updates. 但是所有Android手机都必须始终在监听我的Node服务器进行更新。
  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. 创建一个后台服务(服务或intentservice ...不知道吗?),该服务将在启动时启动并连接到Node服务器。

    This raises a few questions: 这引起了一些问题:

    a. 一种。 Is it ok to have a TCP socket connection like this on the phone 24/7? 可以在电话24/7上建立这样的TCP套接字连接吗?

    b. b。 It will only be exchanging data a few times a day. 一天只交换几次数据。 Will the open connection kill the battery? 打开的连接会杀死电池吗?

    c. 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. 使用GCM来“触发”手机以启动该应用程序,一旦启动该应用程序,请使用主UI线程创建与服务器的连接并拉下已发送的消息。

    a. 一种。 I have never used GCM and only just learned about it yesterday. 我从未使用过GCM,只是昨天才了解到它。 Is this the sort of thing it is used for? 这是用来做什么的吗?

    b. b。 If I use GCM, do I still need to create an 'always running' background service that starts on boot anyways?? 如果我使用GCM,是否需要创建一个始终在启动时启动的“始终运行”后台服务? Or does GCM have some built in way of just always listening? 还是GCM有一些始终听的方式?

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. 我并不完全理解如何使用线程,我的Java知识还不到平均水平(不过我不会说初学者),这是我有史以来第一个Android应用程序。 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? 可以在电话24/7上建立这样的TCP套接字连接吗?
b. 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 ). 您绝对需要每隔几分钟发送一次保持活动的数据包,否则,懒惰的路由器会假设您的连接已死,并在实际需要它们时丢弃数据包(这是google本身的问题)。 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. 也就是说,如果您设法使连接完全保持打开状态,因为Android具有省电功能,这些功能仅禁用了后台应用程序的联网功能。

c. 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 . 同样, 从服务中启动活动也很容易,您可以在Intent传递诸如消息之类的数据。 Wrt Threads, background services aren't actually running in threads but in the main ui thread. Wrt线程,后台服务实际上不在线程中运行,而是在主ui线程中运行。 You can start threads in them though. 您可以在其中启动线程。 In case of networking you even have to because the service would otherwise block the UI thread. 在联网的情况下,您甚至必须这样做,因为该服务否则会阻塞UI线程。 The same and only ui thread that activities of that app use as well. 该应用程序的活动也使用相同且唯一的ui线程。


a. 一种。 I have never used GCM and only just learned about it yesterday. 我从未使用过GCM,只是昨天才了解到它。 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. b。 If I use GCM, do I still need to create an 'always running' background service that starts on boot anyways?? 如果我使用GCM,是否仍需要创建一个始终在启动时启动的“始终运行”后台服务? Or does GCM have some built in way of just always listening? 还是GCM有一些始终听的方式?

No. You define a <receiver> / BroadcastReceiver which gets triggered even if the app isn't running. 否。您定义了一个<receiver> / BroadcastReceiver ,即使该应用程序未运行,它也会被触发。 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. 系统的GCM服务(您需要安装的Google Play服务应用的一部分)会为您完成所有等待消息。 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有效负载中(如果它不太大),这样就可以潜在地保存1个Web请求并加快初始显示速度。


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. GCM通常在快速传递消息方面做得很好,但是您可以从http://support.whispersystems.org/hc/en-us/articles/213190487-Why-is-there-a-delay-in-receiving中看到-messages-https://threema.ch/zh-CN/faq/push_andr,在某些情况下会导致延迟。 Unless you have very special requirements and you don't care about battery, don't implements your own as in 1) - use GCM. 除非您有非常特殊的要求并且不关心电池,否则不要像1)中那样实现自己的电池-使用GCM。

There is btw no way to know whether phones did receive the GCM message built into GCM. 顺便说一句,没有办法知道手机是否收到了内置在GCM中的GCM消息。 So you could send backup SMS after a timeout, when devices didn't connect to your server) 因此,当设备未连接到服务器时,您可以在超时后发送备用SMS)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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