防止通知离开通知栏[Android]

防止通知离开通知栏[Android],android,kotlin,android-notifications,Android,Kotlin,Android Notifications,首先,如果有关于这个的话题,我很抱歉,但我没有找到确切的解决方案。 问题是,我正在设置通知,但当我关闭应用程序时,系统会自动删除通知。您可能认为它与持续的属性相关,但据我所知,它是必需的用户交互。在我的例子中,通知会自动消失 代码如下: class NotificationHelper { private val CHANNEL_ID: String = "Default Channel ID" private var context: Context private var

首先,如果有关于这个的话题,我很抱歉,但我没有找到确切的解决方案。 问题是,我正在设置通知,但当我关闭应用程序时,系统会自动删除通知。您可能认为它与持续的属性相关,但据我所知,它是必需的用户交互。在我的例子中,通知会自动消失

代码如下:

class NotificationHelper {
private val CHANNEL_ID: String = "Default Channel ID"
private var context: Context
private var notificationManager: NotificationManager
private var notificationBuilder: NotificationCompat.Builder

constructor(context: Context) {
    this.context = context
    this.notificationManager =
        this.context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

    // NotificationChannel is required on Oreo and newer
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        this.notificationManager.createNotificationChannel(
            NotificationChannel(
                CHANNEL_ID,
                this.context.getString(R.string.channel_name),
                NotificationManager.IMPORTANCE_HIGH
            )
        )
    }

    notificationBuilder = NotificationCompat.Builder(this.context, CHANNEL_ID)
        .setSmallIcon(R.drawable.ic_set_notification_black)
        .setPriority(NotificationCompat.PRIORITY_HIGH)
        .setCategory(NotificationCompat.CATEGORY_MESSAGE)
        .setNotificationSilent()
        .setPriority(NotificationCompat.PRIORITY_MAX)
        .setContentIntent(
            PendingIntent.getActivity(
                this.context, 0, Intent(
                    this.context,
                    MainActivity::class.java
                ), 0
            )
        )
}

fun setNotification(notification: Notification) {
    val deleteIntent = Intent(context, BootOrUpdateBroadcastReceiver::class.java)
    deleteIntent.action = GlobalInfo.ACTION_NOTIFICATION_DELETE
    deleteIntent.putExtra(GlobalInfo.NotificationEntry.COLUMN_ID, notification.id)

    val notificationToBeShown: android.app.Notification = this.notificationBuilder
        .setContentTitle(notification.title)
        .setContentText(notification.content)
        .setStyle(NotificationCompat.BigTextStyle().bigText(notification.content))
        .setAutoCancel(notification.removeByClick)
        .setOngoing(!notification.removeBySwipeAway)
        .setDeleteIntent(
            PendingIntent.getBroadcast(
                context,
                0,
                deleteIntent,
                PendingIntent.FLAG_CANCEL_CURRENT
            )
        )
        .build()

    with(NotificationManagerCompat.from(this.context)) {
        // notificationId is a unique int for each notification that you must define
        notify(notification.id, notificationToBeShown)
    }

}

fun clearNotification(notificationId: Int) {
    this.notificationManager.cancel(notificationId)
}

fun clearAllNotifications() {
    this.notificationManager.cancelAll()
}

}
另外,我想使用粘性服务,但对我来说没有意义。应用程序关闭时,服务也会被销毁。应该有另一种方法


提前感谢:)

您可能认为它与“正在进行的”属性相关,但据我所知,这是必需的用户交互
您能解释一下吗?当应用程序关闭时,正在进行的通知将被取消;否,当应用程序关闭时,通知不会被销毁。我的意思是,我不会试图以用户身份通过刷走或单击通知来取消通知。这是由android设备在我关闭应用程序时完成的。所以,它与持续属性无关。我只是希望通知永远留在那里。另外,您是否建议我使用前台服务?它与
持续的
属性有何关系?我不明白你的逻辑。当应用程序关闭时,无论您正在尝试什么,正在进行的通知都将自动(由您所说的android设备)解除。您已经在代码中使用了该属性。是的,如果
您的应用程序需要执行用户注意到的任务,即使他们没有直接与应用程序交互,您也可以使用前台服务,如文档中所述。您的意思是一样的。无论持续属性是否为true,通知都将被驳回。所以问题一定出在别的地方。同时,我会尝试你的建议,并写下结果。谢谢^^