@ -21,6 +21,7 @@ import android.util.Log;
import androidx.core.app.NotificationCompat ;
import androidx.core.app.NotificationCompat ;
import androidx.work.Data ;
import androidx.work.Data ;
import androidx.work.ExistingWorkPolicy ;
import androidx.work.OneTimeWorkRequest ;
import androidx.work.OneTimeWorkRequest ;
import androidx.work.WorkManager ;
import androidx.work.WorkManager ;
@ -89,13 +90,22 @@ public class DailyNotificationReceiver extends BroadcastReceiver {
}
}
/ * *
/ * *
* Enqueue notification processing work to WorkManager
* Enqueue notification processing work to WorkManager with deduplication
*
* Uses unique work name based on notification ID to prevent duplicate
* work items from being enqueued for the same notification . WorkManager ' s
* enqueueUniqueWork automatically prevents duplicates when using the same
* work name .
*
*
* @param context Application context
* @param context Application context
* @param notificationId ID of notification to process
* @param notificationId ID of notification to process
* /
* /
private void enqueueNotificationWork ( Context context , String notificationId ) {
private void enqueueNotificationWork ( Context context , String notificationId ) {
try {
try {
// Create unique work name based on notification ID to prevent duplicates
// WorkManager will automatically skip if work with this name already exists
String workName = "display_" + notificationId ;
Data inputData = new Data . Builder ( )
Data inputData = new Data . Builder ( )
. putString ( "notification_id" , notificationId )
. putString ( "notification_id" , notificationId )
. putString ( "action" , "display" )
. putString ( "action" , "display" )
@ -106,8 +116,16 @@ public class DailyNotificationReceiver extends BroadcastReceiver {
. addTag ( "daily_notification_display" )
. addTag ( "daily_notification_display" )
. build ( ) ;
. build ( ) ;
WorkManager . getInstance ( context ) . enqueue ( workRequest ) ;
// Use unique work name with KEEP policy (don't replace if exists)
Log . d ( TAG , "DN|WORK_ENQUEUE display=" + notificationId ) ;
// This prevents duplicate work items from being enqueued even if
// the receiver is triggered multiple times for the same notification
WorkManager . getInstance ( context ) . enqueueUniqueWork (
workName ,
ExistingWorkPolicy . KEEP ,
workRequest
) ;
Log . d ( TAG , "DN|WORK_ENQUEUE display=" + notificationId + " work_name=" + workName ) ;
} catch ( Exception e ) {
} catch ( Exception e ) {
Log . e ( TAG , "DN|WORK_ENQUEUE_ERR display=" + notificationId + " err=" + e . getMessage ( ) , e ) ;
Log . e ( TAG , "DN|WORK_ENQUEUE_ERR display=" + notificationId + " err=" + e . getMessage ( ) , e ) ;
@ -115,13 +133,19 @@ public class DailyNotificationReceiver extends BroadcastReceiver {
}
}
/ * *
/ * *
* Enqueue notification dismissal work to WorkManager
* Enqueue notification dismissal work to WorkManager with deduplication
*
* Uses unique work name based on notification ID to prevent duplicate
* dismissal work items .
*
*
* @param context Application context
* @param context Application context
* @param notificationId ID of notification to dismiss
* @param notificationId ID of notification to dismiss
* /
* /
private void enqueueDismissalWork ( Context context , String notificationId ) {
private void enqueueDismissalWork ( Context context , String notificationId ) {
try {
try {
// Create unique work name based on notification ID to prevent duplicates
String workName = "dismiss_" + notificationId ;
Data inputData = new Data . Builder ( )
Data inputData = new Data . Builder ( )
. putString ( "notification_id" , notificationId )
. putString ( "notification_id" , notificationId )
. putString ( "action" , "dismiss" )
. putString ( "action" , "dismiss" )
@ -132,8 +156,14 @@ public class DailyNotificationReceiver extends BroadcastReceiver {
. addTag ( "daily_notification_dismiss" )
. addTag ( "daily_notification_dismiss" )
. build ( ) ;
. build ( ) ;
WorkManager . getInstance ( context ) . enqueue ( workRequest ) ;
// Use unique work name with REPLACE policy (allow new dismissal to replace pending)
Log . d ( TAG , "DN|WORK_ENQUEUE dismiss=" + notificationId ) ;
WorkManager . getInstance ( context ) . enqueueUniqueWork (
workName ,
ExistingWorkPolicy . REPLACE ,
workRequest
) ;
Log . d ( TAG , "DN|WORK_ENQUEUE dismiss=" + notificationId + " work_name=" + workName ) ;
} catch ( Exception e ) {
} catch ( Exception e ) {
Log . e ( TAG , "DN|WORK_ENQUEUE_ERR dismiss=" + notificationId + " err=" + e . getMessage ( ) , e ) ;
Log . e ( TAG , "DN|WORK_ENQUEUE_ERR dismiss=" + notificationId + " err=" + e . getMessage ( ) , e ) ;