I'm running the Slack app on an Android 11 device. If the app is placed in the background by tapping on the device's home button and a Slack chat message arrives, it displays a notification on the Android notification bar. If you tap on the notification icon to open the notification and then tap on the message, the notification is removed.
What puzzles me is whether an app like Slack even uses a foreground service when receiving chat messages. From my experiencee, to have a continously running background service when no UI is shown, you must display a notification icon on Android's notification bar to indicate to the user that the app is running a background service. While this is considered a background service, it is in fact referred to as a foreground service.
But Slack does not display an icon on the Notification bar to indicate any foreground service. So if it's not using a foreground service but can receive messages while the UI is not shown and post a notification, how is this being done? Has something changed in Android that you don't need a foreground service in order to run a long running process in the background?
The only possible explanation I can think of is that instead of a service, a class that inherits from Application is used and the process for receiving messages is done within a coroutine that is launched from this class and remains active as long as the app remains alive. But if that is true, it means that apps CAN run endless background processes without any UI showing and that means that something changed in the policy required by Android to run endless background services.
What puzzles me is whether an app like Slack even uses a foreground service when receiving chat messages
Most likely, they do not.
So if it's not using a foreground service but can receive messages while the UI is not shown and post a notification, how is this being done?
Most likely, on Google Play ecosystem devices, they are using Firebase Cloud Messaging (FCM) as a trigger to find out about messages that require user notification. FCM and its predecessors (C2DM and GCM) have been around for over a decade and are designed for this sort of problem.
if you look at the firebase messaging documentation you will be understood notifications that arrived to a client are not handled by the application, there is an Android transport layer (ATL) that is responsible for getting messages even if application is not running!
Related
I am using react-native-firebase for the push notifications and it works fine. I am also using CallKeep for handling VoIP calls. I want to display an incoming call when the app gets a notification while it is killed or in background (ex. like WhatsApp).
Regarding to the documenation if a message is received "the device silently starts your application in a background state. At this point, your background handler" code is triggered. But this does not happen. The handler is trigger only if I tap the notification.
I've also added some code in AppDelegate.mm like mentioned in the description but it not works.
BTW: I am currently on iOS and don't know how the effect is on an Android device.
So how can I achieve this, or it is even possible ?
So I found the solution. The problem was that I used firebase console to send a notification which did not contain "ContentAvailable: true" property. When I send a notification using the C# sdk including "ContentAvailable = true" then the background handler is called, even if the app is in background or killed.
I have a situation on my React Native app, where user can start a timer and isRunning and startTime states are stored in the app so that it can display current running time when the app is in foreground even though the user quits the application at some point and opens it again.
Is there a way to show notification icon on status bar when the timer is running, but user has quit the application to indicate that the timer is currently "running" (actually it is not doing any operations on background) on background?
I have encountered some apps that display a silent notification that is not directly closable after I have quit the app, but I haven't seen a situation where status bar notification is present (together with notification) until some condition is met after the app is quit by the user.
Is there a way to achieve this? I am using react-native-push-notification and Firebase to push notifications in my app overall.
React-Native manages only Active and Background/Inactive/Foreground state. When user kill the app. JS engine shuts down.
All you can use is Local Notification and Scheduled Notification. I also have one app in which user set a reminder time. And notification invoke at that specified time.
Using Firebase I guess you need to call api after some specific interval for push notification from server side.
I am not an expert in background services or android development but here is my thought... I believe you could dig in and write some java android code for a background unstoppable service (persistent on app close or background states). You can do this by creating a bridge and using native modules to manage your background services. I came across a great resource on medium that details the process of creating a background service, a broadcast receiver and maintaining Headless instance even when the app is closed or the device restarts.Hope that helps you achieve your goal.
Edit
This ready made package will help you with better. Check it out. You actually don't have to write native android java code at all.
I am working on the voip app. I do all the logic regarding the signaling on background service. When user exists the app I am stoping the service. In this case, I am not connected to the server. When I am in this state, server is sending the push notification (FCM), I am starting the service and creating notification for incoming call or message. This is all good so far:)
I have read about the limitations for Oreo for backgrounding services and realize that now I need to start service in foreground while showing notification. Then I wanted to check how the Hangouts(google) does it, and of course they are running the service in the background when message is received. Am I missing something?
Thank you
Visible activity = foreground
When user exists the app I am stoping the service
If this means that the service is running only when your app is visible to the user, then the process and the service is actually in the foreground and you shouldn't experience any problems with system killing your service:
An app is considered to be in the foreground if any of the following is true:
It has a visible activity, whether the activity is started or paused.
FCM received = foreground
Also, receiving high-priority FCM messages will put your app on temporary whitelist for background processing, and you will have more than enough time to do your processing at that time, or you can always start a foreground service after FCM is received - like a service that handles the call and should display a notification while running:
Under certain circumstances, a background app is placed on a temporary whitelist for several minutes. While an app is on the whitelist, it can launch services without limitation, and its background services are permitted to run. An app is placed on the whitelist when it handles a task that's visible to the user, such as:
Handling a high-priority Firebase Cloud Messaging (FCM) message.
Your app design - handling incoming calls by receiving priority FCM and then creating a foreground call service - seems reasonable.
Refer to the documentation which is really clear on these subjects.
I am trying to implement a functionality wherein users get notification messages even when the app is not running (neither in foreground nor in background). Companies like Amazon do send notifications and they show up in the notification tray - when tapped, the app opens or whatever intent the message carried with it.
I have been able to implement notification handling when the app is in foreground and background... but when the app is not running, there are no notification messages received at all!
From what I searched around, I think there needs to be a Service running in the background that keeps listening to notification messages - because a service is destroyed when the app is closed. Am I going in the right direction?
Can someone point me to some code that implements or highlights the same.
FIXED... actually.. the problem was MIUI.. Its security app by default disabled autostart for apps.. which blocks notifications from being shown in the system tray when the app is not running.
References:-
http://en.miui.com/thread-37783-1-1.html
https://github.com/firebase/quickstart-android/issues/89#issuecomment-233558184
I you have to see the implementation of FCM for android from https://firebase.google.com/docs/cloud-messaging/
I shows complete understanding of instant notification.
I have an intent service which is continously polling to check if data has been modified in the server. The problem is if the app remains idle for some time , it stops receiving notification. We are suspecting the service has been killed by android.
I think even GCMINTENTSERVICE is also killed by android in some time.
My question is how come gmail / what's app/yahoo mail always sync without foreground notification. Which sync mechanism they are using.
Are you aware of any other app which is always receiving notification even when in background, so that I can check in Google why it is not killed in some time.
Does Google kill gcmintentservice if it has been running for some time in the backgroud.
EDIT: GCMBASEINTENTSERVICE is the service we are planning to implement for push notifications, because with the older polling service the app did not receive notification after some time. This is may be because it was getting killed by android in some time. So with push notifications, the app will always receive notification? Isn't Gcmbaseintentservice an intent service and that will also be killed by android in some time.
Android will kill services after some time, to resolve memory issues. It's not a good thing to always run your application in background, since it will consume memory and processing power.
You have got the wrong idea. GMail and Yahoo are not polling for new mail, they are using Push Notification mechanism, more precisely Google Cloud Messaging or GCM. Push Notifications can be received in the background, and once they receive a push notification, they sync the app.
BBM (BlackBerry messenger) didn't use GCM, as they relied on BlackBerry servers. So they ran the application in the background without getting killed. This is done by always showing non swipeable notification. Someway with notification, you could run a service in background without getting it killed.
According to AndroidHeadlines
You may want to leave it persistent if you want to use BBM. In Android
4.3, Google has begun fighting apps that suck down battery and stay alive without you knowing. It now requires any app that wants to run
in the background to place a persistent icon in your notification.
Hence the BBM icon. Doing this allows that particular app to never be
closed.
Surely google will kill any service if the device runs out of memory or new services are running in background.