So far, I've adjsuted my code to use ContextCompat.startForegroundService(context, intentService);
to start my service. This way, it works on android < 26 and on android 26 (Oreo) as well.
I still see a difference, in android oreo I don't see my custom foreground notification (I only see the "app is running in the background" notification). Do I have to adjust anything there as well?
My service looks like this:
public class BaseOverlayService extends Service {
@Override
public void onCreate() {
super.onCreate();
moveToForeground();
}
private void moveToForeground() {
Notification notification = ...;
super.startForeground(NOTIFICATION_ID, notification);
}
}
Official example
This example (https://github.com/googlesamples/android-play-location/blob/master/LocationUpdatesForegroundService/app/src/main/java/com/google/android/gms/location/sample/locationupdatesforegroundservice/LocationUpdatesService.java#L200) shows as comment, that I should use following, but startServiceInForeground
does not exist...
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O) {
NotificationManager mNotificationManager = ((NotificationManager) getSystemService(NOTIFICATION_SERVICE));
mNotificationManager.startServiceInForeground(new Intent(this, BaseOverlayService.class), NOTIFICATION_ID, notification);
} else {
startForeground(NOTIFICATION_ID, notification);
}
Edit
My notification is created like this, which is working on thousands of android devices with API < 26 until now:
protected Notification foregroundNotification(int notificationId)
{
boolean paused = MainApp.getPrefs().sidebarServicePaused();
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.icon_not);
builder.setContentIntent(notificationIntent());
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon));
builder.setContentTitle(getString(R.string.derived_app_name));
builder.setContentText(getString(checkStatus() ? (paused ? R.string.service_notification_text_paused : R.string.service_notification_text_running) : R.string.service_notification_text_preparing));
builder.setColor(Color.parseColor("#25baa2"));
if (MainApp.getPrefs().hideNotificationIcon())
builder.setPriority(NotificationCompat.PRIORITY_MIN);
else
builder.setPriority(NotificationCompat.PRIORITY_MAX);
if (paused)
builder.addAction(R.drawable.ic_play_arrow_black_24dp, getString(R.string.resume), resumeIntent(this));
else
builder.addAction(R.drawable.ic_pause_black_24dp, getString(R.string.pause), pauseIntent(this));
return builder.build();
}
Code in kotlin
Use:
You may need to define Notification Channel for your app. Check the log, there should be warning. Check this for introduction
I'll give you some example, it would be in kotin. First, make a class like this. You'll need to call createMainNotificationChannel() once at the start of your application.
Then you can use util like this