根据报错信息可以知道是Notification
报的错误,不知道你们是不是这个原因。
仔细检查后发现是在发通知
之前忘记创建通知频道
了。
仅此而已,你有没有中招呢?!!!
- 发送普通通知
val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
manager.notify(Random.nextInt(), createNotification(context, "myNotification", title, subTitle))
- 创建前台服务通知
override fun onCreate() {
super.onCreate()
Log.d(TAG, "onCreate: create sevice", )
startForeground(1,
createNotification(baseContext, "myNotification", "服务已创建")
)
}
- 创建通知频道
val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val channel = NotificationChannel("myNotification", "我的通知", NotificationManager.IMPORTANCE_HIGH).apply {
setShowBadge(true)
enableVibration(true)
setAllowBubbles(true)
enableLights(true)
}
manager.createNotificationChannel(channel)