Skip to content

Commit

Permalink
Fix bug for generating notification's android notification ID
Browse files Browse the repository at this point in the history
* Bug: The private setter was generating a random android notification ID but this private setter is never called. Setters are not triggered in initialization. This resulted in the android notification ID always being zero for every notification.
* The fix: Now we set the random ID in initialization, and make the notification property immutable (a val instead of var) as it is never re-set anyway.
  • Loading branch information
nan-li authored and jinliu9508 committed Jan 31, 2024
1 parent df78d98 commit 8f7063f
Showing 1 changed file with 7 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,17 @@ import org.json.JSONObject
import java.security.SecureRandom

class NotificationGenerationJob(
private var _notification: Notification,
inNotification: Notification,
var jsonPayload: JSONObject,
) {
var notification: Notification
get() = _notification
private set(value) {
// If there is no android ID on the notification coming in, create one either
// copying from the previous one or generating a new one.
if (value != null && !value!!.hasNotificationId()) {
val curNotification = _notification
if (curNotification != null && curNotification.hasNotificationId()) {
value.androidNotificationId = curNotification.androidNotificationId
} else {
value.androidNotificationId = SecureRandom().nextInt()
}
}
val notification: Notification = inNotification.setAndroidNotificationId()

_notification = value
private fun Notification.setAndroidNotificationId() = this.also {
// If there is no android ID on the notification coming in, generate a new one.
if (it != null && !it.hasNotificationId()) {
it.androidNotificationId = SecureRandom().nextInt()
}
}

var isRestoring = false
var isNotificationToDisplay = false
Expand Down

0 comments on commit 8f7063f

Please sign in to comment.