if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val NOTIFICATION_CHANNEL_ID = "net.sarasarasa.lifeup" val channelName = "FinishTaskIntentService" val channel = NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_MIN)
val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager manager.createNotificationChannel(channel)
val notification = Notification.Builder(applicationContext, NOTIFICATION_CHANNEL_ID).build()
startForeground(1001, notification) }
val extras = intent?.extras
if (extras != null) { ... // 业务逻辑 } }
}
一开始我是直接new Notification传参,然后测试发现又抛了一个异常:
1
Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification