onNotificationPosted 方法中出现 ForegroundServiceStartNotAllowedException

问题描述 投票:0回答:1

将 firebase 上的 target android 版本升级到 33 后,出现 ForegroundServiceStartNotAllowedException 崩溃问题。有谁知道如何解决这些问题,下面我发送一段与音乐服务相关的代码。我正在使用 exoplayer 2.18.1.

private fun createNotificationListener(): PlayerNotificationManager.NotificationListener {
    return object : PlayerNotificationManager.NotificationListener {

        override fun onNotificationPosted(
            notificationId: Int,
            notification: Notification,
            ongoing: Boolean
        ) {
            if (ongoing && !isForegroundService) {
                playerNotification = notification
                startForeground(notificationId, notification)
                isForegroundService = true
            }
        }

        override fun onNotificationCancelled(notificationId: Int, dismissedByUser: Boolean) {
            stopForeground(false)
            isForegroundService = false
            stopSelf()
        }
    }
}

我想减少与使用音乐服务相关的错误

android kotlin exoplayer
1个回答
0
投票

当应用程序在不满足必要条件的情况下尝试启动前台服务时,会抛出

ForegroundServiceStartNotAllowedException
。从Android 12(API级别33)开始,启动前台服务的条件更加严格。

以下是解决问题的步骤:

  1. 使用

    Service.startForeground()
    立即:启动前台服务时,应该在服务启动后立即调用
    startForeground()
    。任何延迟都可能导致系统终止您的服务或抛出
    ForegroundServiceStartNotAllowedException

  2. 使用

    Context.startForegroundService()
    :启动服务时,使用
    Context.startForegroundService()
    而不是
    Context.startService()
    。这向系统表明您打算将该服务设为前台服务。

  3. 正确处理服务生命周期:确保正确处理服务的生命周期方法。例如,如果不再需要该服务,请拨打

    stopSelf()
    停止服务。

  4. 更新通知:如果您使用 ExoPlayer 的

    PlayerNotificationManager
    ,请确保在播放状态或元数据发生变化时更新通知。这将确保通知保持准确并且服务保持在前台。

  5. 检查待处理意图:确保您用于前台服务的通知具有待处理意图,以便用户可以从通知返回到应用程序。

鉴于您提供的代码,这里有一个更新版本,应该有助于减少错误:

private fun createNotificationListener(): PlayerNotificationManager.NotificationListener {
    return object : PlayerNotificationManager.NotificationListener {

        override fun onNotificationPosted(
            notificationId: Int,
            notification: Notification,
            ongoing: Boolean
        ) {
            if (ongoing && !isForegroundService) {
                playerNotification = notification
                // Ensure you're calling startForeground immediately after starting the service
                startForeground(notificationId, notification)
                isForegroundService = true
            } else if (!ongoing) {
                // Handle the case where the notification is no longer ongoing
                stopForeground(false)
                isForegroundService = false
            }
        }

        override fun onNotificationCancelled(notificationId: Int, dismissedByUser: Boolean) {
            stopForeground(true) // Consider using true to remove the notification
            isForegroundService = false
            stopSelf()
        }
    }
}

最后,确保您在清单中拥有必要的权限:

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

启动服务时:

val intent = Intent(context, YourMusicService::class.java)
context.startForegroundService(intent)

通过执行这些步骤并确保遵守前台服务最佳实践,您应该能够减少与在 Android 12 及更高版本上使用音乐服务相关的错误。

© www.soinside.com 2019 - 2024. All rights reserved.