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

Android通知无法显示问题:点击按钮触发通知无响应排查

问题诊断与修复方案

你的代码存在几个关键问题导致通知无法正常显示,逐一解决如下:

1. 未适配Android 13+的通知权限要求

从Android 13(API级别33)开始,发送通知必须主动申请POST_NOTIFICATIONS权限,你的代码完全没有处理这个逻辑:

  • 首先在AndroidManifest.xml中添加权限声明:
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
  • 然后在代码中动态检查并申请权限,避免直接发送通知被系统拦截。

2. NotificationChannel的名称设置不合理

创建频道时第二个参数是频道的显示名称,你用了1.toString()这种无意义的数字字符串,虽然语法合法,但可能导致系统对频道识别异常,建议替换为有意义的名称(比如"测试通知频道")。

3. 通知小图标不符合系统规范

Android强制要求通知小图标必须是纯白色、透明背景的资源,如果R.drawable.cooked_egg是彩色或带有不透明背景的图片,通知会被系统直接拦截不显示。


修正后的完整代码

class MainActivity : AppCompatActivity() {

    lateinit var notificationManager: NotificationManager
    private val channelId = "i.apps.notifications"
    private val channelName = "测试通知频道"
    private val description = "Test notification"
    private val REQUEST_NOTIFICATION_PERMISSION = 100

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val btn = findViewById<Button>(R.id.btn)

        val intent = Intent(this, MainActivity::class.java)
        val pendingIntent: PendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE)

        notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        createNotificationChannel()

        val builder = NotificationCompat.Builder(this, channelId)
            .setSmallIcon(R.drawable.ic_notification) // 替换为符合要求的纯白透明图标
            .setContentTitle("My notification")
            .setContentText("Hello World!")
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setContentIntent(pendingIntent)
            .setAutoCancel(true)
        
        btn.setOnClickListener {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.POST_NOTIFICATIONS), REQUEST_NOTIFICATION_PERMISSION)
                    return@setOnClickListener
                }
            }
            // 权限校验通过,发送通知
            with(NotificationManagerCompat.from(this)) {
                notify(3, builder.build())
            }
        }
    }

    private fun createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val descriptionText = description
            val importance = NotificationManager.IMPORTANCE_DEFAULT
            val channel = NotificationChannel(channelId, channelName, importance).apply {
                description = descriptionText
            }
            notificationManager.createNotificationChannel(channel)
        }
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == REQUEST_NOTIFICATION_PERMISSION) {
            if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // 用户授予权限后重新发送通知
                val intent = Intent(this, MainActivity::class.java)
                val pendingIntent: PendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE)
                val builder = NotificationCompat.Builder(this, channelId)
                    .setSmallIcon(R.drawable.ic_notification)
                    .setContentTitle("My notification")
                    .setContentText("Hello World!")
                    .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                    .setContentIntent(pendingIntent)
                    .setAutoCancel(true)
                with(NotificationManagerCompat.from(this)) {
                    notify(3, builder.build())
                }
            }
        }
    }
}

内容的提问来源于stack exchange,提问作者HBMuni

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 17:42:41