I created a messenger application which works with sockets. So I want to add it in background. In background the socket will hear income messages in background and show notifications. So I want to do it with WorkManager
. Is it the best way and will this work? Or there are another ways to do this? Thanks. If you interested I'm using SocketCluster
.
Asked
Active
Viewed 352 times
0

Hayk Mkrtchyan
- 2,835
- 3
- 19
- 61
-
We do something similar in an app my team has developed. We ended up using a foreground service to keep our socket alive: https://developer.android.com/guide/components/services – Bradford2000 Feb 21 '19 at 20:31
-
1Ok I can use foreground service. But what if I don't want to show a notification always? I only want to show notification when someone sends me a message. – Hayk Mkrtchyan Feb 22 '19 at 09:13
-
Hey, sorry for the delay, haven't been on here in awhile. Unfortunately, if you make your service a foreground service, it has to always show the notification, there is no way to dismiss it. This works good for the voice app my team maintains, but I can see how that might not be ideal for a messenger app. If the foreground notification is a deal-breaker, you could leave your service as non-foreground and use GCM/FCM for when the app is not in the foreground. We do this for the messenger application my team maintains. – Bradford2000 Mar 21 '19 at 19:51
1 Answers
0
I recommend using FCM
for this. Using Sockets for maintaining persistent TCP connections can be bad from a battery perspective. If you must do this, use a foreground Service
.

Rahul
- 19,744
- 1
- 25
- 29
-
1Ok I can use foreground service. But what if I don't want to show a notification always? I only want to show notification when someone sends me a message. – Hayk Mkrtchyan Feb 22 '19 at 09:11