Reputation: 89
I did implement a notification feature in android using the Notification.Builder in Android OREO+. I need to cancel the notification after a certain time frame, if the user has not clicked on the notification. which i completed using the setTimeOutAfter method.
https://developer.android.com/reference/android/app/Notification.Builder.html#setTimeoutAfter(long).
Now, i need to send a message to server that the notification wasn't clicked/timeout has occured. How can i implement this? Is there any notificationTimeout Listener?
Upvotes: 2
Views: 1941
Reputation: 8723
There's nothing like a timeout listener but you can use a delete intent for your purpose. You'll need a Broadcast Receiver
in order to do something (like calling your server) when the notification gets dismissed.
In code:
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()
}
Upvotes: 5