Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce a de-dupe mechanism for push notifications #177

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import com.klaviyo.pushFcm.KlaviyoRemoteMessage.imageUrl
import com.klaviyo.pushFcm.KlaviyoRemoteMessage.isKlaviyoNotification
import com.klaviyo.pushFcm.KlaviyoRemoteMessage.notificationCount
import com.klaviyo.pushFcm.KlaviyoRemoteMessage.notificationPriority
import com.klaviyo.pushFcm.KlaviyoRemoteMessage.notificationTag
import com.klaviyo.pushFcm.KlaviyoRemoteMessage.sound
import com.klaviyo.pushFcm.KlaviyoRemoteMessage.title
import java.net.URL
Expand Down Expand Up @@ -62,12 +63,13 @@ class KlaviyoNotification(private val message: RemoteMessage) {
internal const val COLOR_KEY = "color"
internal const val NOTIFICATION_COUNT_KEY = "notification_count"
internal const val NOTIFICATION_PRIORITY = "notification_priority"
internal const val NOTIFICATION_TAG = "notification_tag"
private const val DOWNLOAD_TIMEOUT_MS = 5_000

/**
* Get an integer ID to associate with a notification or its pending intent
* The notification system service will de-dupe on this ID alone,
* and I don't think we want our notifications to be de-duped
* The notification system service will de-dupe on this if we get a null
* notification tag from the payload
*
* NOTE: The FCM SDK also uses a timestamp to construct its integer IDs
*/
Expand Down Expand Up @@ -104,7 +106,7 @@ class KlaviyoNotification(private val message: RemoteMessage) {

NotificationManagerCompat
.from(context)
.notify(generateId(), notification.build())
.notify(message.notificationTag ?: generateId().toString(), 0, notification.build())
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 can't approve my own PR, but approved lol


return true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ object KlaviyoRemoteMessage {
get() = this.data[KlaviyoNotification.NOTIFICATION_PRIORITY]?.toInt()
?: NotificationCompat.PRIORITY_DEFAULT

/**
* Parse out notification tag, used as a de-duping mechanism
*/
val RemoteMessage.notificationTag: String? get() = this.data[KlaviyoNotification.NOTIFICATION_TAG]

/**
* Determine if the message originated from Klaviyo from the tracking params
*/
Expand Down
Loading