我正试着在点击开始按钮时创建通知消息,但点击按钮后通知中心什么都没有,有什么 idea 吗?

使用IDEA在Android 14上进行测试.

https://github.com/speedyHKjournalist/MapleServerAndroid/blob/e51a1177050b9fa120edbe0ef77180115e987c32/app/src/main/java/com/mapleserver/ServerService.kt#L21

    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
    }

推荐答案

由于 comments 不能标记为答案,所以我将我的 comments 复制到答案中:

Android 13修改了通知权限:你确定你和你的应用程序可以发送通知吗?见https://developer.android.com/develop/ui/views/notifications/notification-permission

Android相关问答推荐

我遇到了一个HashMaps对象没有存储在Firebase数据库中的问题.HashMap的一个对象put方法未被存储

如何将子零部件的大小调整为可以调整大小的父组件大小?

Jetpack编写使用自定义主题覆盖库中主题部分

Android Kotlin ImageView内置于Kotlin ImageView中.适配器未按预期更新

插入视图模型时,dagger 未命中绑定错误

设置文本 colored颜色 动画时如何减少重新组合?

有没有什么方法可以让Beeware在安卓手机上显示图片?

有没有办法让协程通道在接收时遵循特定的顺序而不是先进先出

如何禁用自动登录 google play games services android unity?

Jetpack Compose:在屏幕外制作长水平图像的动画

为什么我要使用 $version 而不是2.7.0?

Kotlin File(path).walkTopDown() 找不到文件

获取 ArithmeticException:除以零,但我没有在任何地方除以零

在 Jetpack Compose 中重用具有重复代码的列

Jetpack Compose - 每次点击按钮都不起作用

java.lang.IllegalArgumentException:与请求匹配的导航目的地 NavDeepLinkRequest

如何使用 ConstraintLayout 链接两个文本

为什么我在 Jetpack Compose 中被警告可选修饰符参数应该具有默认值修饰符?

构成material 3 中的分隔符

任何 IRCTC 的公共 API 来判断 PNR 状态和座位可用性?