Skip to content

Commit

Permalink
Merge pull request #84 from rees46/feat/data-from-push-notification
Browse files Browse the repository at this point in the history
Feat/data from push notification
  • Loading branch information
TorinAsakura authored Dec 16, 2024
2 parents 15c8bfe + 2b2c223 commit 126ff6b
Show file tree
Hide file tree
Showing 26 changed files with 258 additions and 126 deletions.
2 changes: 1 addition & 1 deletion personalization-sdk/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name=".features.notification.data.service.NotificationService" />
<service android:name=".features.notification.service.NotificationService" />

</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.personalization

import com.personalization.features.notification.domain.model.NotificationData
import com.personalization.sdk.data.models.dto.notification.NotificationData

fun interface OnMessageListener {
fun onMessage(data: NotificationData)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class RegisterManager @Inject constructor(
}

private fun processFirebaseToken(token: String) {

val savedToken = getPreferencesValueUseCase.getToken()
val lastUpdate = getPreferencesValueUseCase.getLastPushTokenDate()
val currentDate = Date().time
Expand Down
10 changes: 4 additions & 6 deletions personalization-sdk/src/main/kotlin/com/personalization/SDK.kt
Original file line number Diff line number Diff line change
Expand Up @@ -766,13 +766,11 @@ open class SDK {
}

private fun receiveMessage(remoteMessage: RemoteMessage) {
notificationReceived(remoteMessage.data)
notificationReceived(data = remoteMessage.data)

onMessageListener?.let { listener ->
listener.onMessage(
data = remoteMessage.toNotificationData()
)
}
onMessageListener?.onMessage(
data = remoteMessage.toNotificationData()
)
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.personalization.di

import com.personalization.SDK
import com.personalization.features.notification.data.service.NotificationService
import com.personalization.features.notification.service.NotificationService
import com.personalization.sdk.data.di.DataSourcesModule
import com.personalization.sdk.data.di.ModelsModule
import com.personalization.sdk.data.di.RepositoriesModule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.json.JSONObject
class JsonResponseErrorHandler(
private val tag: String,
private val response: JSONObject?,
private val message: String? = "Response is null or incorrect"
) {

/**
Expand Down Expand Up @@ -44,7 +45,10 @@ class JsonResponseErrorHandler(
* Logs the error message with the associated tag
* @param message The error message to log
*/
fun logError(message: String, exception: Exception? = null) {
fun logError(
message: String? = this.message,
exception: Exception? = null
) {
Log.e(tag, message, exception)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.personalization.features.notification.actions.mapper

import com.personalization.errors.JsonResponseErrorHandler
import org.json.JSONArray

fun parseActionUrls(actionUrlsJson: String?): List<String> {
return try {
actionUrlsJson?.let {
val jsonArray = JSONArray(it)
List(jsonArray.length()) { index -> jsonArray.getString(index) }
} ?: emptyList()
} catch (exception: Exception) {
JsonResponseErrorHandler(
tag = "parseNotificationActions",
response = null
).logError(exception = exception)
emptyList()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.personalization.features.notification.actions.mapper

import com.personalization.errors.JsonResponseErrorHandler
import com.personalization.features.notification.actions.model.NotificationAction
import com.personalization.features.notification.domain.model.NotificationConstants.NOTIFICATION_ACTION
import com.personalization.features.notification.domain.model.NotificationConstants.NOTIFICATION_TITLE
import org.json.JSONArray

fun parseNotificationActions(actionsJson: String?): List<NotificationAction> {
return try {
actionsJson?.let {
val jsonArray = JSONArray(it)
List(jsonArray.length()) { index ->
val jsonObject = jsonArray.getJSONObject(index)
NotificationAction(
action = jsonObject.getString(NOTIFICATION_ACTION),
title = jsonObject.getString(NOTIFICATION_TITLE)
)
}
} ?: emptyList()
} catch (exception: Exception) {
JsonResponseErrorHandler(
tag = "parseNotificationActions",
response = null
).logError(exception = exception)

emptyList()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.personalization.features.notification.actions.model

data class NotificationAction(
val action: String,
val title: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.personalization.features.notification.data.mapper

import com.google.firebase.messaging.RemoteMessage
import com.personalization.features.notification.actions.mapper.parseActionUrls
import com.personalization.features.notification.actions.mapper.parseNotificationActions
import com.personalization.features.notification.domain.model.NotificationConstants.NOTIFICATION_ACTIONS
import com.personalization.features.notification.domain.model.NotificationConstants.NOTIFICATION_ACTION_URLS
import com.personalization.features.notification.domain.model.NotificationConstants.NOTIFICATION_BODY
import com.personalization.features.notification.domain.model.NotificationConstants.NOTIFICATION_EVENT
import com.personalization.features.notification.domain.model.NotificationConstants.NOTIFICATION_ICON
import com.personalization.features.notification.domain.model.NotificationConstants.NOTIFICATION_IMAGE
import com.personalization.features.notification.domain.model.NotificationConstants.NOTIFICATION_PARAM_ID
import com.personalization.features.notification.domain.model.NotificationConstants.NOTIFICATION_TITLE
import com.personalization.features.notification.domain.model.NotificationConstants.TYPE_PARAM
import com.personalization.features.notification.event.mapper.parseNotificationEvent
import com.personalization.sdk.data.models.dto.notification.NotificationData

fun RemoteMessage.toNotificationData(): NotificationData = NotificationData(
id = this.data[NOTIFICATION_PARAM_ID],
title = this.data[NOTIFICATION_TITLE],
body = this.data[NOTIFICATION_BODY],
icon = this.data[NOTIFICATION_ICON],
type = this.data[TYPE_PARAM],
actions = parseNotificationActions(this.data[NOTIFICATION_ACTIONS]),
actionUrls = parseActionUrls(this.data[NOTIFICATION_ACTION_URLS]),
image = this.data[NOTIFICATION_IMAGE],
event = parseNotificationEvent(this.data[NOTIFICATION_EVENT])
)

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,17 @@ object NotificationConstants {
const val ACTION_NEXT_IMAGE = "ACTION_NEXT_IMAGE"
const val NOTIFICATION_TYPE = "NOTIFICATION_TYPE"
const val TRACK_CLICKED = "track/clicked"
const val NOTIFICATION_IMAGES = "images"
const val NOTIFICATION_IMAGE = "image"
const val NOTIFICATION_ICON = "icon"
const val NOTIFICATION_TITLE = "title"
const val NOTIFICATION_BODY = "body"
const val NOTIFICATION_ACTIONS = "actions"
const val NOTIFICATION_ACTION = "action"
const val NOTIFICATION_ACTION_URLS = "action_urls"
const val NOTIFICATION_PARAM_ID = "id"
const val NOTIFICATION_PAYLOAD = "payload"
const val NOTIFICATION_EVENT = "event"
const val NOTIFICATION_URI = "uri"
const val TYPE_PARAM = "type"
const val CODE_PARAM = "code"

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.personalization.features.notification.event.mapper

import com.personalization.errors.JsonResponseErrorHandler
import com.personalization.features.notification.domain.model.NotificationConstants.NOTIFICATION_PAYLOAD
import com.personalization.features.notification.domain.model.NotificationConstants.NOTIFICATION_URI
import com.personalization.features.notification.domain.model.NotificationConstants.TYPE_PARAM
import com.personalization.features.notification.event.model.NotificationEvent
import org.json.JSONObject

fun parseNotificationEvent(eventJson: String?): NotificationEvent {
val emptyData = NotificationEvent(
type = "",
uri = "",
payload = emptyMap()
)
return try {
eventJson?.let {
val jsonObject = JSONObject(it)

NotificationEvent(
type = jsonObject.optString(TYPE_PARAM, ""),
uri = jsonObject.optString(NOTIFICATION_URI, ""),
payload = jsonObject.optJSONObject(NOTIFICATION_PAYLOAD)?.let { payloadObj ->
payloadObj.keys().asSequence().associateWith { key -> payloadObj[key] }
} ?: emptyMap()
)

} ?: emptyData

} catch (exception: Exception) {
JsonResponseErrorHandler(
tag = "parseNotificationEvent",
response = null
).logError(exception = exception)

emptyData
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.personalization.features.notification.event.model

data class NotificationEvent(
val type: String?,
val uri: String?,
val payload: Map<String, Any>? = null
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import android.graphics.Bitmap
import android.widget.RemoteViews
import androidx.core.app.NotificationCompat
import com.personalization.features.notification.domain.model.NotificationConstants.NOTIFICATION_CHANNEL
import com.personalization.features.notification.domain.model.NotificationData
import com.personalization.resources.NotificationResources
import com.personalization.sdk.data.models.dto.notification.NotificationData
import javax.inject.Inject

class NotificationHelper @Inject constructor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package com.personalization.features.notification.presentation.helpers
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import com.personalization.features.notification.data.service.NotificationService
import com.personalization.features.notification.domain.model.NotificationConstants.CURRENT_IMAGE_INDEX
import com.personalization.features.notification.domain.model.NotificationConstants.NOTIFICATION_BODY
import com.personalization.features.notification.domain.model.NotificationConstants.NOTIFICATION_IMAGES
import com.personalization.features.notification.domain.model.NotificationConstants.NOTIFICATION_IMAGE
import com.personalization.features.notification.domain.model.NotificationConstants.NOTIFICATION_TITLE
import com.personalization.features.notification.domain.model.NotificationData
import com.personalization.features.notification.service.NotificationService
import com.personalization.sdk.data.models.dto.notification.NotificationData
import java.util.concurrent.atomic.AtomicInteger
import kotlin.math.abs

Expand All @@ -27,7 +27,7 @@ object NotificationNavigationHelper {
putExtra(CURRENT_IMAGE_INDEX, newIndex)
putExtra(NOTIFICATION_TITLE, data.title)
putExtra(NOTIFICATION_BODY, data.body)
putExtra(NOTIFICATION_IMAGES, data.images)
putExtra(NOTIFICATION_IMAGE, data.image)
}

return PendingIntent.getService(
Expand All @@ -44,7 +44,7 @@ object NotificationNavigationHelper {
): PendingIntent {
val retryIntent = Intent(context, NotificationService::class.java).apply {
putExtra(CURRENT_IMAGE_INDEX, 0)
putExtra(NOTIFICATION_IMAGES, data.images)
putExtra(NOTIFICATION_IMAGE, data.image)
putExtra(NOTIFICATION_TITLE, data.title)
putExtra(NOTIFICATION_BODY, data.body)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.personalization.features.notification.presentation.helpers

import android.widget.RemoteViews
import com.personalization.R
import com.personalization.features.notification.domain.model.NotificationData
import com.personalization.sdk.data.models.dto.notification.NotificationData

object NotificationTextHelper {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import android.widget.RemoteViews
import com.personalization.R
import com.personalization.features.notification.domain.model.NotificationConstants.ACTION_NEXT_IMAGE
import com.personalization.features.notification.domain.model.NotificationConstants.ACTION_PREVIOUS_IMAGE
import com.personalization.features.notification.domain.model.NotificationData
import com.personalization.sdk.data.models.dto.notification.NotificationData

object NotificationViewHelper {

Expand Down
Loading

0 comments on commit 126ff6b

Please sign in to comment.