0

I need some advice for my application structure. It's about messaging using GCM. So I get onMessageReceived call in GcmListenerService (with message data). Now there are two cases:

a) There already is a chat activity in Activity Stack and I should send there an Intent with information about new message so that the activity updates its views

b) There's no opened chat activity at the moment, so I'm just gonna show a Notification.
The important thing is, I do NOT wanna show the notification if there's an activity on top of the stack.

How do I detect whether there's a chat activity in activity stack?

T.Vert
  • 279
  • 2
  • 13
  • just use "bound local service" pattern and your service will know if there is any client bound to it – pskink Aug 16 '16 at 10:36
  • @pskink, so I need to make an instance of Service (usual one) and bind Chat activity to it? I mean, we can't bind to GcmListenerService, right? – T.Vert Aug 16 '16 at 10:44
  • `GcmListenerService` extends `Service` so you can call `bindService` on it – pskink Aug 16 '16 at 10:45
  • @pskink, but isn't GcmListenerService created only to process a message and then to be automatically destroyed? – T.Vert Aug 16 '16 at 10:47
  • read [this](https://developer.android.com/guide/components/bound-services.html) to find out bound / started service lifecycle – pskink Aug 16 '16 at 10:51
  • @pskink, well, we can't override onBind method in GcmListenerService, it's final and it returns null – T.Vert Aug 16 '16 at 11:06
  • ooops, too bad... so you probably need some other service to bind to... or just maybe some singleton flag could be used (set and unset by your activity) – pskink Aug 16 '16 at 12:03
  • @pskink, singleton flag sounds interesting, I never thought about it... Would that be a good practice? I probably gonna use it, cause it's much simpler than creating another service and binding to it. Thanks :) – T.Vert Aug 17 '16 at 05:12
  • if you dont like "normal" singletons you could use a custom `android.app.Application` but its java docs say: `"There is normally no need to subclass Application. In most situation, static singletons can provide the same functionality in a more modular way. If your singleton needs a global context (for example to register broadcast receivers), the function to retrieve it can be given a Context which internally uses Context.getApplicationContext() when first constructing the singleton."` – pskink Aug 17 '16 at 05:26

0 Answers0