Android通知MediaStyle忽略持续标记的问题。
创始人
2024-08-21 19:00:37
0

在使用MediaStyle的通知时,如果想要让通知变成常驻通知,即使用户点击了'清除所有”按钮,通知也不会消失,需要设置setOngoing(true)标记。然而,在某些Android版本中,例如Android 8.0以上的版本,设置setOngoing(true)会被忽略,导致通知会在用户清除所有通知时被移除。

为了解决这个问题,可以使用前台服务的方式来让通知常驻。代码示例如下:

class MyService : Service() {

    companion object {
        const val NOTIFICATION_ID = 1001
    }

    override fun onBind(intent: Intent): IBinder? {
        return null
    }

    override fun onCreate() {
        super.onCreate()

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(
                "my_channel_id",
                "My Channel",
                NotificationManager.IMPORTANCE_LOW
            )

            channel.setShowBadge(false)

            val manager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
            manager.createNotificationChannel(channel)
        }

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

        val notification = NotificationCompat.Builder(this, "my_channel_id")
            .setContentTitle("My Notification")
            .setContentText("Content text")
            .setSmallIcon(R.drawable.ic_notification)
            .setPriority(NotificationCompat.PRIORITY_LOW)
            .setContentIntent(pendingIntent)
            .setOngoing(true) // 必须设置为常驻通知
            .build()

        startForeground(NOTIFICATION_ID, notification)
    }
}

在这个示例中,我们创建了一个前台服务来让通知常驻,并设置了setOngoing(true)。同时,我们还创建了一个NotificationChannel,用于适配Android 8.0及以上版本。最后,我们调用了startForeground()方法将通知设置为前台通知。

这样做的

相关内容

热门资讯

透视ai代打!wepoker怎... 透视ai代打!wepoker怎么买辅助,wepoker轻量版书签(其实存在有挂)1、每一步都需要思考...
透视教学!wepoker俱乐部... 透视教学!wepoker俱乐部辅助,wepoker私人局辅助挂(本来真的是有挂)1、用户打开应用后不...
透视能赢!wepoker辅助器... 透视能赢!wepoker辅助器如何使用,wepoker私人辅助器(真是有挂);1、下载好wepoke...
透视神器!wepoker怎么提... 透视神器!wepoker怎么提高运气,wepoker怎么设置盖牌(竟然真的有挂)1、完成wepoke...
透视数据!wepoker插件程... 透视数据!wepoker插件程序激活码,wepoker有脚本吗(竟然有挂)1、许多玩家不知道wepo...
透视代打!we poker辅助... 透视代打!we poker辅助器,wepoker有辅助工具吗(都是是有挂)1)wepoker有辅助工...
透视挂!we poker辅助器... 透视挂!we poker辅助器v3.3,wepoker免费透视脚本(一贯是有挂)1、首先打开wepo...
透视教学!wepoker破解工... 透视教学!wepoker破解工具,wepoker看底牌(切实真的是有挂)1、每一步都需要思考,不同水...
透视讲解!wepoker透视脚... 透视讲解!wepoker透视脚本苹果版,wepokerplus透视脚本免费(好像有挂)1、实时wep...
透视游戏!wepoker辅助器... 透视游戏!wepoker辅助器如何使用,wepoker辅助透视(好像是有挂)1、wepoker辅助器...