gpt4 book ai didi

Android 通知超时监听器

转载 作者:行者123 更新时间:2023-12-03 08:28:32 26 4
gpt4 key购买 nike

我确实使用Android OREO+中的Notification.Builder在android中实现了通知功能。如果用户没有单击通知,我需要在一定时间范围后取消通知。我使用 setTimeOutAfter 方法完成了该操作。

https://developer.android.com/reference/android/app/Notification.Builder.html#setTimeoutAfter(long) .

现在,我需要向服务器发送一条消息,告知未单击通知/发生超时。我怎样才能实现这个?有没有notificationTimeout监听器?

最佳答案

没有什么比超时监听器更好的了,但您可以使用删除 Intent 来达到您的目的。您需要一个广播接收器,以便在通知消失时执行某些操作(例如调用您的服务器)。

在代码中:

class NotificationDismissedReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
// call your server here
}
}

private fun getNotificationWithDeleteIntent() : Notification{
val deleteIntent = Intent(context, NotificationDismissedReceiver::class.java)
deleteIntent.action = "notification_cancelled"
val onDismissPendingIntent = PendingIntent.getBroadcast(context, 0, deleteIntent, PendingIntent.FLAG_CANCEL_CURRENT)

val builder = NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(textTitle)
.setContentText(textContent)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setTimeoutAfter(TIMEOUT)
.setDeleteIntent(onDismissPendingIntent)

return builder.build()
}

关于Android 通知超时监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65882237/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com