0

I'm trying to create a foreground service that check devices location every minute and I've managed to make it work. But I have some problems regarding the obligatory notification about the service being active.

I can't get the notification badge to stop showing and I can't make the notification minimized programatically.

Here are the relevant parts:

class LocationPollingService : Service() {

    companion object {
        const val NOTIF_ID = 2
        const val NOTIF_CHANNEL_ID = "background_service_notification"
        const val NOTIF_CHANNEL_NAME = "Background service"
    }
    @Nullable
    override fun onBind(intent: Intent?): IBinder? {
        return null
    }

    override fun onStartCommand(
        intent: Intent?,
        flags: Int,
        startId: Int
    ): Int {
        createBackgroundNotifChannel()
        startForeground()
        requestLocation()
        return super.onStartCommand(intent, flags, startId)
    }

    private fun startForeground() {
        val notificationIntent = Intent(this, SplashActivity::class.java)
        val pendingIntent = PendingIntent.getActivity(
            this, 0,
            notificationIntent, 0
        )

        val notification = NotificationCompat.Builder(
            this,
            NOTIF_CHANNEL_ID
        )
            .setOngoing(true)
            .setSmallIcon(R.drawable.ic_app_icon)
            .setContentTitle(getString(R.string.app_name))
            .setContentText(getString(R.string.im_watching_you))
            .setContentIntent(pendingIntent)
            .setCategory(NotificationCompat.CATEGORY_SERVICE)

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            startForeground(NOTIF_ID, notification.build())
        } else {
            notification.priority = NotificationCompat.PRIORITY_MIN
            startForeground(NOTIF_ID, notification.build())
        }
    }

    private fun createBackgroundNotifChannel() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            val channelBackground = NotificationChannel(
                NOTIF_CHANNEL_ID,
                NOTIF_CHANNEL_NAME,
                NotificationManager.IMPORTANCE_LOW
            ).apply {
                description = getString(
                    R.string.notification_channel_background_description
                )
                enableLights(false)
                enableVibration(false)
                setShowBadge(false)
            }


            val notificationManager: NotificationManager =
                getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

            notificationManager.createNotificationChannel(channelBackground)
        }
    }
}

If I understand correctly, IMPORTANCE_LOW is supposed to be used for this kind of notification and it is supposed to make it minimized by default. The notification is never minimized nor can I minimize it by clicking. The only way I can make it minimized is by manually changing notification channel settings in device settings (setting Notification style to Silent and minimized, instead of just silent, which is the default). I tried setting the importance to IMPORTANCE_MIN and IMPORTANCE_NONE, but that didn't change anything.

As mentioned, the second problem is that the badge is shown (the notification counter displayed on app icon), despite of setShowBadge(false).

What am I missing?

Filip Pranklin
  • 345
  • 1
  • 3
  • 14
  • Does this answer your question? [Is it possible to put a foreground service notification in a notification channel with IMPORTANCE\_MIN?](https://stackoverflow.com/questions/45485460/is-it-possible-to-put-a-foreground-service-notification-in-a-notification-channe) – greeble31 Dec 18 '19 at 14:22
  • Not really, I tried what's described in the answer there yesterday, but no luck When I set `IMPORTANCE_MIN` the debugger says that channels importance = 1 which is correct. When I change the settings manually, it says that importance = 2 which is `IMPORTANCE_LOW`. When I set to LOW programatically, it says importance = 2, but behaviour is the same as when importance = 1 so it must be something else. Edit: I may have mistaken something for something, but, bottom line, the docs say that foreground services should use `IMPORTANCE_LOW`, but it doesn't behave like the rest of the services. – Filip Pranklin Dec 18 '19 at 14:42
  • Could you include a picture of what you mean by "it doesn't behave like the rest of the services" – greeble31 Dec 18 '19 at 15:18
  • @greeble31 https://pasteboard.co/ILY7llK.png Unpause is my app, I want the notification to be like the Google weather notification beneath it – Filip Pranklin Dec 19 '19 at 12:07

2 Answers2

-2

You need to notify notification after build notification like below :

notificationManager.notify(NOTIF_ID , notification.build());

-2

for hiding notification badge from statusbar you should change notification channel priority:

val channelBackground = NotificationChannel( NOTIF_CHANNEL_ID, NOTIF_CHANNEL_NAME, NotificationManager.IMPORTANCE_MIN

Hadi
  • 125
  • 1
  • 4