21

We adjusted our ongoing notification for Oreo and it worked great. Now, on Pie only (not happening on Oreo devices), we're getting the titled error. Has something changed in foreground services in Pie that I'm missing?

Here's the onCreate code for the foreground service ->

override fun onCreate() {
    super.onCreate()

    val notification: Notification = NotificationCompat.Builder(this, packageName)
            .setSmallIcon(R.drawable.status_notification_icon)
            .setContentTitle(getString(R.string.ongoing_notify_temp_title))
            .setContentText(getString(R.string.ongoing_notify_temp_message))
            .setGroup(AppConstants.NOTIFICATION_GROUP_ONGOING)
            .setColor(ContextCompat.getColor(this, R.color.custom_blue))
            .build()

    startForeground(ONGOING_NOTIFY_ID, notification)

    appSettings = AppSettings(this)

    weatherLookUpHelper = WeatherLookUpHelper()
    MyRoomDatabase.getInstance().invalidationTracker.addObserver(onChange)

    retrieveCurrentLocation()
    createAlarmManager()
}

as you can see, we're just creating the notification and then calling startForeground. Any ideas on why this code would generate the titled error?

Side Note: Fabric Crashlytics shows this crash only happening on Pixel devices (pixel, pixel xl, pixel 2, pixel 2 xl) running Pie

EDIT: We do have the foreground permission in our manifest

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
Psest328
  • 6,575
  • 11
  • 55
  • 90

4 Answers4

15

As mentioned here Background Service Limitations, the app/service has five seconds to call startForeground(), If the app does not call startForeground() within the time limit, the system stops the service and declares the app to be ANR.

There are some possibilities:

  1. Either your foreground service gets destroyed/finished before calling the startForeground() method.
  2. Or if the foreground service is already instantiated and its getting called again, then the onCreate method will not be called, instead onStartCommand will be called. Then move your logic to onStartCommand to call startForeground() method.
  3. Your notification id in startForeground must not be 0, otherwise it will also cause the crash.
Arshad Mehmood
  • 409
  • 3
  • 14
  • 1
    would putting startForeground in onStartCommand risk anything with duplicate calls? – Psest328 Oct 29 '18 at 16:17
  • 1
    No, `onStartCommand` would get called after `onCreate` only once when you start the service. But, if you start the service multiple times, then of course `onStartCommand` would be called multiple times. – Arshad Mehmood Oct 30 '18 at 13:59
2

Has something changed in foreground services in Pie that I'm missing?

YES Have a look here migration notes of Android 9 / Pie

Change

Foreground service permission

Summary

Apps wanting to use foreground services must now request the FOREGROUND_SERVICE permission first. This is a normal permission, so the system automatically grants it to the requesting app. Starting a foreground service without the permission throws a SecurityException.

UPDATE

related issue in Google Issue Tracker Context.startForegroundService() did not then call Service.startForeground

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
1
  1. I revert back to startService() instead of using startForeground() solved my issue.
Liya
  • 568
  • 7
  • 28
  • 1
    Same issue here...after adding the API > Oreo check and using ```startForegroundService(intent)``` this is causing the crash even though I use ```startForeground(0, builder.build());``` in the service at ```onStartCommand``` – DIRTY DAVE Nov 10 '21 at 10:03
0

I stopped to get the error after implementing below code in ONCREATE

Intent intent1 = new Intent(this, YourActivity.class);
          try {
                startService(intent1);
            }catch ( Exception e1){
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    this.startForegroundService(intent1);
                }else {
                    Crashlytics.log("crash for first time, trying another.");
                    this.startService(intent1);
                }
            }

For me sounds strange but is working fine without crash.

  • 1
    It will not crash, but just service (not foreground) will not work, when application is closed from API > Android oreo – Kostya M Dec 02 '19 at 07:32