根据报错信息可以知道是Notification报的错误,不知道你们是不是这个原因。

仔细检查后发现是在发通知之前忘记创建通知频道了。

仅此而已,你有没有中招呢?!!!

  1. 发送普通通知
    val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    manager.notify(Random.nextInt(), createNotification(context, "myNotification", title, subTitle))
  1. 创建前台服务通知
    override fun onCreate() {
        super.onCreate()
        Log.d(TAG, "onCreate: create sevice", )
        startForeground(1,
            createNotification(baseContext, "myNotification", "服务已创建")
        )
    }
  1. 创建通知频道
    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)
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐