Android 前台服务通知

Android 前台服务通知,android,push-notification,foreground-service,Android,Push Notification,Foreground Service,我试图创建一个前台服务,每分钟检查设备的位置,我已经设法使它工作。但是,我对服务激活的强制性通知有一些问题 我无法停止显示通知徽章,也无法通过编程将通知最小化 以下是相关部分: class LocationPollingService : Service() { companion object { const val NOTIF_ID = 2 const val NOTIF_CHANNEL_ID = "background_service_notifi

我试图创建一个前台服务,每分钟检查设备的位置,我已经设法使它工作。但是,我对服务激活的强制性通知有一些问题

我无法停止显示通知徽章,也无法通过编程将通知最小化

以下是相关部分:

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)
        }
    }
}
如果我理解正确,
重要性\u LOW
应该用于此类通知,并且应该在默认情况下使其最小化。通知永远不会最小化,我也不能通过单击将其最小化。我可以使其最小化的唯一方法是在设备设置中手动更改通知通道设置(将
通知样式
设置为
静音和最小化
,而不是默认设置为
静音
)。我尝试将重要性设置为
importance\u MIN
importance\u NONE
,但这并没有改变任何事情

如前所述,第二个问题是,尽管
设置ShowBadge(false)
,但仍会显示徽章(通知计数器显示在应用程序图标上)


我缺少什么?

您需要在生成通知后通知通知,如下所示:


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

用于从状态栏隐藏通知标记 您应该更改通知通道优先级:

val channelBackground=通知通道( 如果没有频道ID, 不包括频道名称,
NotificationManager.importionance\u MIN

它没有帮助。另外,我认为
启动了foreground(Int,Notification)
from
Service
会这样做。@Filipranklin服务不会这样做如果你想用你的徽章显示通知,你必须通知。请检查一下。服务不会这样做-是的,它不会这样做你不能隐藏前台服务通知这回答了你的问题吗?不是真的,我尝试了answ中描述的内容呃,昨天在那里,但是当我设置
重要性\u MIN
时没有运气。调试器说通道重要性=1,这是正确的。当我手动更改设置时,它说重要性=2,这是
重要性\u LOW
。当我以编程方式设置为LOW时,它说重要性=2,但行为与重要性=1s时相同编辑:我可能把某些东西弄错了,但是,底线是,文档说前台服务应该使用
重要性\u LOW
,但它的行为与其他服务不一样。你能描述一下“它的行为与其他服务不一样”的意思吗@greeble31 Unpause是我的应用程序,我希望它下面的通知像谷歌天气通知一样