3

Our app has some services and intent-services that starts running when the user starts the app. These components needs to be started to continue the process running even if the user minimize the app.

On Android Oreo our app started crashing due the missing initialization of these services using startForegroundService(:intent) and startForeground(:id, :notification). We've fixed it attaching these notifications as requerested but we discovered a strange behaviour. These notifications appear even if the app is in foreground, since it doesn't make sense because our services are really short-running almost all the time, but it needs to be guaranteed to run 100% of the time. That's why the process runs on a service and not in the activity's context. I would like to show these notifications only when the user minimize the app and the service is up. Is there any way to do it?

enter image description here

I can't change this message, very annoying!

Here is some code used to create the channel:

const val SERVICE_CHANNEL_ID = "com.myFunnyApp.notifications"
//.....
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

  val notificationService = context.getSystemService(Context.NOTIFICATION_SERVICE)
  (notificationService as? NotificationManager)?.let { notificationManager ->

    if (notificationManager.getNotificationChannel(SERVICE_CHANNEL_ID) == null) {
      val channel = NotificationChannel(
          SERVICE_CHANNEL_ID,
          context.resources.getString(R.string.test),
          NotificationManager.IMPORTANCE_MIN)
      channel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
      notificationManager.createNotificationChannel(channel)
      return
    }

  }

}

On the service, the code runs:

val notification = Notification.Builder(context, SERVICE_CHANNEL_ID)
        .setSubText("subText") //Not working
        .setContentText("contentText") //Not working
        .setSettingsText("settingsText") //Not working
        .setContentTitle("title") //Not working
        .build()
startForeground(1, notification)

Currently I am testing on the emulator, I don't have any Android Oreo device.

Thanks!

EDIT 1: The text only changes if you set the small icon.

Pedro Paulo Amorim
  • 1,838
  • 2
  • 27
  • 50
  • have you defined a different "process" for the service in manifest? how long your service takes to complete? – Arnav M. Nov 01 '17 at 06:21

1 Answers1

1

In Android 8.0(Android Oreo) and onwards, we have to bring the service to the foreground before we trigger a Notification.

After which once you have triggered your Notification - you will have to stop the service running in the Foreground for which execute the following code after -

val notification = Notification.Builder(context, SERVICE_CHANNEL_ID)
        .setSubText("subText") //Not working
        .setContentText("contentText") //Not working
        .setSettingsText("settingsText") //Not working
        .setContentTitle("title") //Not working
        .build()
startForeground(1, notification)

stopForeground(true);
stopSelf();
susheel
  • 36
  • 6