You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android 14下无法创建通知通道,点击按钮后通知中心无显示问题求助

Android 14下无法创建通知通道,点击按钮后通知中心无显示问题求助

各位大佬好,我最近在开发Android项目时遇到个头疼的问题:点击Start按钮后本应弹出一条通知,但在Android 14设备上测试时,通知中心完全没反应。我用IDEA作为开发工具,下面是ServerService.kt里onStartCommand方法的代码,麻烦大家帮忙看看哪里出问题了🙏

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {

    val channelId = intent?.getStringExtra("channel_id") ?: "default_id"

    val pendingIntent = intent?.getParcelableExtra("pending_intent", PendingIntent::class.java)

    val initializationThread = Thread {
        val args = arrayOf("-Xmx2048m", "-Dwz-path=wz", "-Djava.net.preferIPv4Stack=true")
        Server.getInstance(applicationContext)
        Server.main(args)
    }

    initializationThread.start()

    val notificationManager = getSystemService(ComponentActivity.NOTIFICATION_SERVICE) as NotificationManager
    val channel = NotificationChannel(channelId, "OpenMapleServer Channel", NotificationManager.IMPORTANCE_DEFAULT)
    notificationManager.createNotificationChannel(channel)

    val builder = NotificationCompat.Builder(this, "1")
    builder.setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle("OpenMapleServer Notification")
        .setContentText("OpenMapleServer is Running")
        .setContentIntent(pendingIntent)
        .setChannelId("1")
        .setAutoCancel(true)
        .setPriority(NotificationCompat.PRIORITY_DEFAULT)

    val notification = builder.build()
    notificationManager.notify(0, notification);

    startForeground(startId, notification)

    return START_STICKY
}

我自己琢磨了几个可能的问题点,也想听听大家的看法:

  • 通道ID不匹配:创建通知通道用的是channelId变量(从Intent获取,默认值是"default_id"),但构建Notification时却硬编码成了"1",Android 14要求通知必须绑定已创建的通道,这会不会是核心问题?
  • 通知权限缺失:Android 13及以上版本需要申请POST_NOTIFICATIONS权限,我是不是漏了在Manifest里声明,或者没做动态权限请求?
  • 前台通知调用顺序:我先调用notificationManager.notify再调用startForeground,是不是顺序搞反了?毕竟startForeground本身就会把通知设为前台通知,会不会重复操作导致异常?
  • 图标不符合规范:R.drawable.ic_launcher会不会不符合通知图标的要求?比如有透明区域或者尺寸不达标?

希望有经验的大佬能帮我指出问题,或者给出修改建议,谢谢大家!

备注:内容来源于stack exchange,提问作者k0ala Xing

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.20 10:18:00