【发布时间】:2018-12-08 03:57:27
【问题描述】:
当用户在 Android Oreo 及更高版本上点击通知时应用程序崩溃:java.lang.IllegalStateException: Not allowed to start service Intent。我知道这是因为 android Oreo 中引入了后台服务限制。但是我不确定我有什么选择,因为我需要在用户点击通知后立即启动 IntentService 来执行一些简短的后台工作。
在 FCM 的 onMessageReceived 方法中:
val denyIntent = Intent(this, DenyService::class.java)
denyIntent.putExtra(VIDEO_EXTRA_SESSION_ID, sessionId)
denyIntent.putExtra(VIDEO_DENY_REASON, "callee_action_deny")
val denyIntentCode = System.currentTimeMillis().toInt()
val pendingIntentDeny = PendingIntent.getService(this, denyIntentCode, denyIntent, 0)
notificationBuilder.setDeleteIntent(pendingIntentDeny)
notificationBuilder.addAction(
R.drawable.ic_call_reject,
getString(R.string.chat_action_reject),
pendingIntentDeny
)
class DenyService : IntentService("DenyCall")
【问题讨论】:
-
创建前台服务
-
@Psypher 问题是该服务是使用 PendingIntent 启动的,客户端代码无法控制服务的启动方式。
标签: android android-notifications