Avoids Notification duplicates

This commit is contained in:
Vitor Pamplona
2023-05-14 18:12:04 -04:00
parent f40b6c394c
commit cf9395d5e3
2 changed files with 12 additions and 12 deletions
@@ -46,7 +46,7 @@ class EventNotificationConsumer(private val applicationContext: Context) {
val user = note.author?.toBestDisplayName() ?: "" val user = note.author?.toBestDisplayName() ?: ""
val userPicture = note.author?.profilePicture() val userPicture = note.author?.profilePicture()
val noteUri = note.toNEvent() val noteUri = note.toNEvent()
notificationManager().sendDMNotification(content, user, userPicture, noteUri, applicationContext) notificationManager().sendDMNotification(event.id, content, user, userPicture, noteUri, applicationContext)
} }
} }
} }
@@ -88,7 +88,7 @@ class EventNotificationConsumer(private val applicationContext: Context) {
} }
val userPicture = senderInfo?.first?.profilePicture() val userPicture = senderInfo?.first?.profilePicture()
val noteUri = "nostr:Notifications" val noteUri = "nostr:Notifications"
notificationManager().sendZapNotification(content, title, userPicture, noteUri, applicationContext) notificationManager().sendZapNotification(event.id, content, title, userPicture, noteUri, applicationContext)
} }
} }
} }
@@ -15,10 +15,6 @@ import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.ui.MainActivity import com.vitorpamplona.amethyst.ui.MainActivity
object NotificationUtils { object NotificationUtils {
// Notification ID.
private var notificationId = 0
private var dmChannel: NotificationChannel? = null private var dmChannel: NotificationChannel? = null
private var zapChannel: NotificationChannel? = null private var zapChannel: NotificationChannel? = null
@@ -63,6 +59,7 @@ object NotificationUtils {
} }
fun NotificationManager.sendZapNotification( fun NotificationManager.sendZapNotification(
id: String,
messageBody: String, messageBody: String,
messageTitle: String, messageTitle: String,
pictureUrl: String?, pictureUrl: String?,
@@ -72,10 +69,11 @@ object NotificationUtils {
val zapChannel = getOrCreateZapChannel(applicationContext) val zapChannel = getOrCreateZapChannel(applicationContext)
val channelId = applicationContext.getString(R.string.app_notification_zaps_channel_id) val channelId = applicationContext.getString(R.string.app_notification_zaps_channel_id)
sendNotification(messageBody, messageTitle, pictureUrl, uri, channelId, applicationContext) sendNotification(id, messageBody, messageTitle, pictureUrl, uri, channelId, applicationContext)
} }
fun NotificationManager.sendDMNotification( fun NotificationManager.sendDMNotification(
id: String,
messageBody: String, messageBody: String,
messageTitle: String, messageTitle: String,
pictureUrl: String?, pictureUrl: String?,
@@ -85,10 +83,11 @@ object NotificationUtils {
val dmChannel = getOrCreateDMChannel(applicationContext) val dmChannel = getOrCreateDMChannel(applicationContext)
val channelId = applicationContext.getString(R.string.app_notification_dms_channel_id) val channelId = applicationContext.getString(R.string.app_notification_dms_channel_id)
sendNotification(messageBody, messageTitle, pictureUrl, uri, channelId, applicationContext) sendNotification(id, messageBody, messageTitle, pictureUrl, uri, channelId, applicationContext)
} }
fun NotificationManager.sendNotification( fun NotificationManager.sendNotification(
id: String,
messageBody: String, messageBody: String,
messageTitle: String, messageTitle: String,
pictureUrl: String?, pictureUrl: String?,
@@ -104,6 +103,7 @@ object NotificationUtils {
val imageLoader = ImageLoader(applicationContext) val imageLoader = ImageLoader(applicationContext)
val imageResult = imageLoader.executeBlocking(request) val imageResult = imageLoader.executeBlocking(request)
sendNotification( sendNotification(
id = id,
messageBody = messageBody, messageBody = messageBody,
messageTitle = messageTitle, messageTitle = messageTitle,
picture = imageResult.drawable as? BitmapDrawable, picture = imageResult.drawable as? BitmapDrawable,
@@ -113,6 +113,7 @@ object NotificationUtils {
) )
} else { } else {
sendNotification( sendNotification(
id = id,
messageBody = messageBody, messageBody = messageBody,
messageTitle = messageTitle, messageTitle = messageTitle,
picture = null, picture = null,
@@ -124,6 +125,7 @@ object NotificationUtils {
} }
private fun NotificationManager.sendNotification( private fun NotificationManager.sendNotification(
id: String,
messageBody: String, messageBody: String,
messageTitle: String, messageTitle: String,
picture: BitmapDrawable?, picture: BitmapDrawable?,
@@ -137,7 +139,7 @@ object NotificationUtils {
val contentPendingIntent = PendingIntent.getActivity( val contentPendingIntent = PendingIntent.getActivity(
applicationContext, applicationContext,
notificationId, id.hashCode(),
contentIntent, contentIntent,
PendingIntent.FLAG_MUTABLE PendingIntent.FLAG_MUTABLE
) )
@@ -171,9 +173,7 @@ object NotificationUtils {
.setPriority(NotificationCompat.PRIORITY_HIGH) .setPriority(NotificationCompat.PRIORITY_HIGH)
.setAutoCancel(true) .setAutoCancel(true)
notify(notificationId, builder.build()) notify(id.hashCode(), builder.build())
notificationId++
} }
/** /**