Skip to content

Commit c270e28

Browse files
committed
Code simplification updates
- replace notificationId null check with early continue - create private helper method for getting latest notification Id
1 parent b1444d4 commit c270e28

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

OneSignalSDK/onesignal/notifications/src/main/java/com/onesignal/notifications/internal/listeners/NotificationListener.kt

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,23 +75,21 @@ internal class NotificationListener(
7575
val deviceType = _deviceService.deviceType
7676

7777
for (i in 0 until data.length()) {
78-
val notificationId = NotificationFormatHelper.getOSNotificationIdFromJson(data[i] as JSONObject?)
78+
val notificationId = NotificationFormatHelper.getOSNotificationIdFromJson(data[i] as JSONObject?) ?: continue
79+
7980
if (postedOpenedNotifIds.contains(notificationId)) {
8081
continue
8182
}
82-
if (notificationId != null) {
83-
postedOpenedNotifIds.add(notificationId)
84-
}
83+
84+
postedOpenedNotifIds.add(notificationId)
8585

8686
try {
87-
if (notificationId != null) {
88-
_backend.updateNotificationAsOpened(
89-
appId,
90-
notificationId,
91-
subscriptionId,
92-
deviceType,
93-
)
94-
}
87+
_backend.updateNotificationAsOpened(
88+
appId,
89+
notificationId,
90+
subscriptionId,
91+
deviceType,
92+
)
9593
} catch (ex: BackendException) {
9694
Logging.error("Notification opened confirmation failed with statusCode: ${ex.statusCode} response: ${ex.response}")
9795
}
@@ -104,8 +102,7 @@ internal class NotificationListener(
104102
)
105103

106104
// Handle the first element in the data array as the latest notification
107-
val latestNotification = if (data.length() > 0) data[0] as JSONObject else null
108-
val latestNotificationId = NotificationFormatHelper.getOSNotificationIdFromJson(latestNotification)
105+
val latestNotificationId = getLatestNotificationId(data)
109106

110107
if (shouldInitDirectSessionFromNotificationOpen(activity)) {
111108
// We want to set the app entry state to NOTIFICATION_CLICK when coming from background
@@ -130,4 +127,9 @@ internal class NotificationListener(
130127
}
131128
return true
132129
}
130+
131+
private fun getLatestNotificationId(data: JSONArray): String? {
132+
val latestNotification = if (data.length() > 0) data[0] as JSONObject else null
133+
return NotificationFormatHelper.getOSNotificationIdFromJson(latestNotification)
134+
}
133135
}

OneSignalSDK/onesignal/notifications/src/main/java/com/onesignal/notifications/internal/open/impl/NotificationOpenedProcessor.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ import com.onesignal.core.internal.config.ConfigModelStore
3838
import com.onesignal.core.internal.database.impl.OneSignalDbContract
3939
import com.onesignal.debug.internal.logging.Logging
4040
import com.onesignal.notifications.internal.common.NotificationConstants
41-
import com.onesignal.notifications.internal.common.NotificationFormatHelper
4241
import com.onesignal.notifications.internal.common.NotificationHelper
4342
import com.onesignal.notifications.internal.data.INotificationRepository
4443
import com.onesignal.notifications.internal.lifecycle.INotificationLifecycleService

0 commit comments

Comments
 (0)