27

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();
}
A. Shevchuk
  • 2,109
  • 14
  • 23
prom85
  • 16,896
  • 17
  • 122
  • 242

3 Answers3

21

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.

class NotificationManager(private val context: Context) {

    companion object {
        private val CHANNEL_ID = "YOUR_CHANNEL_ID"
        private val CHANNEL_NAME = "Your human readable notification channel name"
        private val CHANNEL_DESCRIPTION = "description"
    }

    @RequiresApi(Build.VERSION_CODES.O)
    fun getMainNotificationId(): String {
        return CHANNEL_ID
    }

    @RequiresApi(Build.VERSION_CODES.O)
    fun createMainNotificationChannel() {
            val id = CHANNEL_ID
            val name = CHANNEL_NAME
            val description = CHANNEL_DESCRIPTION
            val importance = android.app.NotificationManager.IMPORTANCE_LOW
            val mChannel = NotificationChannel(id, name, importance)
            mChannel.description = description
            mChannel.enableLights(true)
            mChannel.lightColor = Color.RED
            mChannel.enableVibration(true)
            val mNotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as android.app.NotificationManager
            mNotificationManager.createNotificationChannel(mChannel)
    }
}

Then you can use util like this

fun createNotificationCompatBuilder(context: Context): NotificationCompat.Builder {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        return NotificationCompat.Builder(context, NotificationManager(context).mainNotificationId)
    } else {
        return NotificationCompat.Builder(context)
    }
}
A. Shevchuk
  • 2,109
  • 14
  • 23
  • The link you added is anyways quite good, thanks. One question though: Does this change mean, that I don't necessarily need to show a notification for a foreground service anymore in android oreo? Because people did tell me my app is working on android oreo without the notification as well... – prom85 Sep 08 '17 at 08:48
  • 1
    eghmmm... Actually it can't work without notification. Background work is not allowed in android o without visible components like activity or notification. If you run your service without visible component system will kill it after some time. So you need notification for service if you want to do background job. If you need run something in background with some period - you should check [firebase job dispatcher](https://github.com/firebase/firebase-jobdispatcher-android). It allows you to run scheduled service. – A. Shevchuk Sep 08 '17 at 08:53
  • I've a sidebar app with UI elements on the `WindowManager` and it seems to work with the invisible notification I use until now as well (the invisible notification is one without a channel). Did not get any complaints in a few days now. The emulator does not kill my service within 5 seconds either – prom85 Sep 08 '17 at 08:56
  • @prom85 is your target api set to 26? – A. Shevchuk Sep 08 '17 at 08:56
  • Yes, it is. Visible components are there anyways, even two: one for the overlay I draw and one for the service running in background... – prom85 Sep 08 '17 at 08:57
  • Then it works because of your overlay. But be careful in future with background works limitations – A. Shevchuk Sep 08 '17 at 09:00
1

Code in kotlin

 private fun customNotification(title: String,notificationID: Int) {

                val intent = Intent(this, MainActivity::class.java)
                val pendingIntent = PendingIntent.getActivity(this, 0 /* request code */, intent, PendingIntent.FLAG_UPDATE_CURRENT)


                val builder = NotificationCompat.Builder(this, title)
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .setContentText("Notification description")
                        .setSmallIcon(R.drawable.ic_mark_map)
                        .setContentIntent(pendingIntent)
                        .setOnlyAlertOnce(true)

                val mNotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    val channel = NotificationChannel(title,
                            "Channel human readable title",
                            NotificationManager.IMPORTANCE_DEFAULT)
                    mNotificationManager.createNotificationChannel(channel)
                }

                 val notification = builder.build()
                 startForeground(notificationID, notification)
            }

Use:

 customNotification("Title",101)
code4rox
  • 941
  • 9
  • 34
0
  // Since android Oreo notification channel is needed.
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel channel = new NotificationChannel(channelId,
                        getString(R.string.app_name),
                        NotificationManager.IMPORTANCE_HIGH);

                AudioAttributes attributes = new AudioAttributes.Builder()
                        .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                        .build();


                channel.setDescription(data.get("body"));
                channel.enableLights(true);
                channel.enableVibration(true);
                channel.setSound(SoundUri, attributes); // This is IMPORTANT

                notificationManager.createNotificationChannel(channel);


            }
Teraiya Mayur
  • 1,094
  • 10
  • 18