15

How to continue background service in android Oreo without showing notification dot? i continues my background service using notification but i don't want to show notification for running service.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
faiyaz meghreji
  • 271
  • 1
  • 2
  • 9

3 Answers3

35

If you would have read the Android Oreo 8.0 Documentation properly somewhere in here, you might not have posted this question here.

Step 1: Make sure you start a service as a foreground Service as given in below code

ContextCompat.startForegroundService(mainActivity, new Intent(getContext(), GpsServices.class));
ContextCompat.startForegroundService(mainActivity, new Intent(getContext(), BluetoothService.class));
ContextCompat.startForegroundService(mainActivity, new Intent(getContext(), BackgroundApiService.class));

Step 2: Use notification to show that your service is running. Add below line of code in onCreate method of Service.

@Override
public void onCreate() {
    ...
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        startForeground(NOTIFICATION_ID, notification);
    }
    ...
}

Step 3: Remove the notification when the service is stopped or destroyed.

@Override
public void onDestroy() {
    ...
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
          stopForeground(true); //true will remove notification
    }
    ...
}

One problem with this solution is that it will keep showing the notification until your Service is running on all devices running on Android Oreo 8.0.

I'm sure that this solution will work even when the app is in the background or in kill state.

IMPORTANT NOTE: SHOWING A NOTIFICATION FOR RUNNING A SERVICE IN BACKGROUND (APP IN BACKGROUND OR KILLED STATE) IS MANDATORY IN ANDROID OREO 8.0. YOU CANNOT RUN AWAY WITH IT. SO IT IS RECOMMENDED THAT YOU MAKE NECESSARY CHANGES IN YOUR APP TO MAKE IT WORK PROPERLY AS PER THE BEST PRACTICES FOLLOWED OR ASKED BY ANDROID.

I hope this might help to solve your problem.

Saran Sankaran
  • 2,335
  • 2
  • 19
  • 34
Amrut Bidri
  • 6,276
  • 6
  • 38
  • 80
  • But how to handle received boot compled listener in orieo? – Feroz Siddiqui Nov 05 '18 at 07:19
  • it is giving me error again and again check https://stackoverflow.com/questions/54550439/how-to-make-foreground-service-for-music-player-for-android-oreo/54558052#54558052 – Vipul Chauhan Feb 07 '19 at 08:22
  • 8
    But How Fb and whatsapp still working without that notification? – Cecil Paul Mar 19 '19 at 05:01
  • @CecilPaul WhatsApp shows notification for backing up messages. I think Facebook app may not be running foreground service. – Malwinder Singh Apr 01 '19 at 06:43
  • But in whatsapp how they receive message without using foreground service? – Cecil Paul Apr 11 '19 at 07:47
  • 3
    @CecilPaul Because the app is temporarily whitelisted for "Handling a high-priority Firebase Cloud Messaging (FCM) message.". Read more on this page https://developer.android.com/about/versions/oreo/background.html#services – vovahost May 23 '19 at 15:46
  • 2
    Currently there are many applications that have a service running in the background without a notification icon using Oreo and higher. To start, most of the security monitoring applications, cctv. Even the best known as facebook, instagram, whatsapp, etc. etc. etc.; I doubt that the only way to use services in Oreo and higher, is in foreground. – nnyerges Nov 06 '19 at 14:38
  • 1
    Faebook, instagram, whatsapp and other big names get special permissions from Google and Apple to access system services which we commoners don't have access to. – zeeshan Nov 20 '19 at 17:10
  • @zeeshan do you have information on that? how can you sign up as a provider? we want to use SignalR to collect information from a server and provide notifications, it seems crazy that only a select few people can provide real time notifications with no information on what to do to authorise, review, approve real time notification providers. – Simon Oct 12 '21 at 21:45
  • 1
    @simon unfortunately I don't have that information. You'll have to contact directly to Google and Apple to ask them. However, this year, i.e. 2021 they both have further increases restrictions for background processes and access to potential private information. In our last project, for the Government of Canada, we had to put a message asking users to keep the app open until certain operations are completed because there was no hope dealing with background operations. – zeeshan Oct 14 '21 at 17:10
6

This isn't possible in this API version (26) and higher. Android OS automatically close your service if you run it without showing a notification to the user.

If you are targeting API >= 26 the system will impose a restriction on your service to run in background unless your activity in foreground. As soon as your activity goes to background, the service will be terminates when system finds it running in background (See Background service limitations).

By using startForegroundService() method you grant that permission to run the service in background even when the activity isn't running. Must also once the service has been created, the service must call its startForeground() method within five seconds.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Abdur Rahman
  • 894
  • 1
  • 11
  • 27
  • Currently there are many applications that have a service running in the background without a notification icon using Oreo and higher. To start, most of the security monitoring applications, cctv. Even the best known as facebook, instagram, whatsapp, etc. etc. etc.; I doubt that the only way to use services in Oreo and higher, is in foreground. – nnyerges Nov 06 '19 at 14:40
  • 1
    Faebook, instagram, whatsapp and other big names get special permissions from Google and Apple to access system services which we commoners don't have access to. – zeeshan Nov 20 '19 at 17:05
1

This code worked for me. First you make sure :

1- Define the service in Manifest

2- Define the permission in Manifest

  <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

3- Make sure you have started the service before.

Your service should have a notification. You should be careful that your notification must have a ChannelID !

this page can help you:

How to create a Custom Notification Layout in android?

Android how to show notification on screen

Now paste the following code into your service

@Override
    public void onCreate() {
        super.onCreate();
            try {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    startForeground(" your Notification ID ", notification);
                }
            } catch (Exception e) {
                Log.e(" Error --->> ", e.getMessage());
            }
    }

after that add this code:

@Override
public ComponentName startForegroundService(Intent service) {
    return super.startForegroundService(service);
}

and this:

@Override
public void onDestroy() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        stopForeground(true);
    }
}
Masoud Siahkali
  • 5,100
  • 1
  • 29
  • 18