Skip to content

Commit

Permalink
Thirteenth release
Browse files Browse the repository at this point in the history
  • Loading branch information
cristidregan committed Oct 12, 2021
1 parent 65d8f79 commit c93eddc
Show file tree
Hide file tree
Showing 18 changed files with 241 additions and 93 deletions.
2 changes: 1 addition & 1 deletion OmetriaSDK/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ android {
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.2.0"
versionName "1.2.1"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
Expand Down
33 changes: 24 additions & 9 deletions OmetriaSDK/src/main/java/com/android/ometriasdk/core/Ometria.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ import com.android.ometriasdk.core.listener.ProcessAppLinkListener
import com.android.ometriasdk.core.network.Client
import com.android.ometriasdk.core.network.ConnectionFactory
import com.android.ometriasdk.core.network.OmetriaThreadPoolExecutor
import com.android.ometriasdk.core.network.toOmetriaNotification
import com.android.ometriasdk.lifecycle.OmetriaActivityLifecycleHelper
import com.android.ometriasdk.notification.NotificationHandler
import com.android.ometriasdk.notification.OmetriaNotification
import com.android.ometriasdk.notification.OmetriaNotificationInteractionHandler
import com.google.firebase.messaging.RemoteMessage
import java.util.*
Expand Down Expand Up @@ -75,6 +77,7 @@ class Ometria private constructor() : OmetriaNotificationInteractionHandler {
* @param application The application context.
* @param apiToken The api key that has been attributed to your project.
* @param notificationIcon The icon that will be used when displaying push notifications.
* @param notificationColor The color that will be used when displaying push notifications.
*/
@JvmStatic
fun initialize(
Expand Down Expand Up @@ -345,8 +348,8 @@ class Ometria private constructor() : OmetriaNotificationInteractionHandler {
* @param link A string representing the URL that has been opened.
* @param page A string representing the name of the screen that has been opened as a result of decomposing the URL.
*/
fun trackDeepLinkOpenedEvent(link: String, page: String) {
trackEvent(OmetriaEventType.DEEP_LINK_OPENED, mapOf(LINK to link, PAGE to page))
fun trackDeepLinkOpenedEvent(link: String?, page: String) {
trackEvent(OmetriaEventType.DEEP_LINK_OPENED, mapOf(LINK to link.orEmpty(), PAGE to page))
}

internal fun trackErrorOccurredEvent(
Expand Down Expand Up @@ -401,13 +404,25 @@ class Ometria private constructor() : OmetriaNotificationInteractionHandler {
repository.getRedirectForUrl(url, listener)
}

override fun onDeepLinkInteraction(deepLink: String) {
Logger.d(Constants.Logger.PUSH_NOTIFICATIONS, "Open URL: $deepLink")
val intent = Intent(Intent.ACTION_VIEW)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
intent.data = Uri.parse(deepLink)
ometriaConfig.application.startActivity(intent)
/**
* Retrieves the [OmetriaNotification] object.
* @param remoteMessage The object that will be processed, received from Firebase messaging.
*/
fun parseNotification(remoteMessage: RemoteMessage): OmetriaNotification? =
remoteMessage.toOmetriaNotification()

override fun onNotificationInteraction(ometriaNotification: OmetriaNotification) {
ometriaNotification.deepLinkActionUrl.let { safeDeeplinkActionUrl ->
Logger.d(
Constants.Logger.PUSH_NOTIFICATIONS,
"Open URL: $safeDeeplinkActionUrl"
)
val intent = Intent(Intent.ACTION_VIEW)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
intent.data = Uri.parse(safeDeeplinkActionUrl)
ometriaConfig.application.startActivity(intent)

trackDeepLinkOpenedEvent(deepLink, "Browser")
trackDeepLinkOpenedEvent(safeDeeplinkActionUrl, "Browser")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ package com.android.ometriasdk.core.event
*
* @param totalPrice A float value representing the pricing.
* @param currency A string representing the currency in ISO currency format. e.g. "USD", "GBP"
* @param items: (List[OmetriaBasketItem]) An array containing the item entries in this basket.
* @param link A deeplink to the web or in-app page for this basket. Can be used in
* a notification sent to the user, e.g. "Forgot to check out? Here's
* your basket to continue: <link>". Following that link should take
* them straight to the basket page.
* @param items (List[OmetriaBasketItem]) An array containing the item entries in this basket.
* @param link A deeplink to the web or in-app page for this basket. Can be used in
* a notification sent to the user, e.g. "Forgot to check out? Here's your basket to continue: <link>".
* Following that link should take them straight to the basket page.
*/
data class OmetriaBasket(
val totalPrice: Float,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import com.android.ometriasdk.core.Logger
import com.android.ometriasdk.core.Ometria
import com.android.ometriasdk.core.event.OmetriaEvent
import com.android.ometriasdk.core.network.model.OmetriaApiError
import com.android.ometriasdk.notification.KEY_OMETRIA
import com.android.ometriasdk.notification.OmetriaNotification
import com.android.ometriasdk.notification.OmetriaNotificationBody
import com.google.firebase.messaging.RemoteMessage
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
Expand Down Expand Up @@ -74,7 +77,7 @@ internal fun String.toOmetriaApiError(): OmetriaApiError {
)
}

internal fun String.toOmetriaNotification(): OmetriaNotification? {
internal fun String.toOmetriaNotificationBody(): OmetriaNotificationBody {
val jsonObject = JSONObject(this)

var context: Map<String, Any>? = null
Expand All @@ -85,21 +88,48 @@ internal fun String.toOmetriaNotification(): OmetriaNotification? {
context = jsonObject.getJSONObject("context").toMap()
} catch (e: JSONException) {
Logger.e(PUSH_NOTIFICATIONS, e.message, e)
Ometria.instance()
.trackErrorOccurredEvent(e.javaClass.name, e.message, jsonObject.toMap())
Ometria.instance().trackErrorOccurredEvent(e.javaClass.name, e.message, jsonObject.toMap())
}
try {
deepLinkActionUrl = jsonObject.getString("deepLinkActionUrl")
} catch (e: JSONException) {
Logger.w(PUSH_NOTIFICATIONS, e.message.orEmpty())
}
try {
imageUrl = jsonObject.getString("imageUrl")
} catch (e: JSONException) {
Logger.w(PUSH_NOTIFICATIONS, e.message.orEmpty())
}

return OmetriaNotification(
return OmetriaNotificationBody(
imageUrl,
deepLinkActionUrl,
context
)
}

internal fun RemoteMessage.toOmetriaNotification(): OmetriaNotification? {
val ometriaNotificationString = this.data[KEY_OMETRIA]
ometriaNotificationString ?: return null

return ometriaNotificationString.toOmetriaNotificationBody().toOmetriaNotification()
}

@Suppress("UNCHECKED_CAST")
internal fun OmetriaNotificationBody.toOmetriaNotification(): OmetriaNotification {
val deepLinkActionUrl = this.deepLinkActionUrl
val imageUrl = this.imageUrl
val campaignType: String? = context?.get("campaign_type") as? String
val externalCustomerId: String? = context?.get("ext_customer_id") as? String
val sendId: String? = context?.get("send_id") as? String
val tracking: Map<String, Any>? = context?.get("tracking") as? Map<String, Any>

return OmetriaNotification(
deepLinkActionUrl,
imageUrl,
campaignType,
externalCustomerId,
sendId,
tracking
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.android.ometriasdk.core.event.OmetriaBasket
import com.android.ometriasdk.core.event.OmetriaBasketItem
import com.android.ometriasdk.core.event.OmetriaEvent
import com.android.ometriasdk.core.network.model.OmetriaApiRequest
import com.android.ometriasdk.notification.OmetriaNotificationBody
import org.json.JSONArray
import org.json.JSONObject

Expand Down Expand Up @@ -113,5 +114,14 @@ internal fun OmetriaEvent.toAPIJson(): JSONObject {
jsonObject.put("type", type)
jsonObject.put("data", data?.dataToJson())

return jsonObject
}

internal fun OmetriaNotificationBody.toJson(): JSONObject {
val jsonObject = JSONObject()
jsonObject.put("imageUrl", imageUrl)
jsonObject.put("deepLinkActionUrl", deepLinkActionUrl)
jsonObject.put("context", context?.dataToJson() ?: JSONObject())

return jsonObject
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.android.ometriasdk.core.Constants
import com.android.ometriasdk.core.Logger
import com.android.ometriasdk.core.Ometria
import com.android.ometriasdk.core.network.OmetriaThreadPoolExecutor
import com.android.ometriasdk.core.network.toOmetriaNotification
import com.android.ometriasdk.core.network.toOmetriaNotificationBody
import com.google.firebase.messaging.RemoteMessage
import java.io.IOException
import java.net.URL
Expand Down Expand Up @@ -42,29 +42,29 @@ internal class NotificationHandler(
val ometriaNotificationString = remoteMessage.data[KEY_OMETRIA]
ometriaNotificationString ?: return

val ometriaNotification = ometriaNotificationString.toOmetriaNotification()
ometriaNotification?.context?.let {
val ometriaNotificationBody = ometriaNotificationString.toOmetriaNotificationBody()
ometriaNotificationBody.context?.let {
Ometria.instance().trackNotificationReceivedEvent(it)
}

val title = remoteMessage.data[KEY_TITLE]
val body = remoteMessage.data[KEY_BODY]

if (ometriaNotification?.imageUrl != null) {
loadImage(ometriaNotification.imageUrl) {
if (ometriaNotificationBody.imageUrl != null) {
loadImage(ometriaNotificationBody.imageUrl) {
ometriaPushNotification.createPushNotification(
title,
body,
it,
ometriaNotification,
ometriaNotificationBody,
remoteMessage.collapseKey
)
}
} else {
ometriaPushNotification.createPushNotification(
title = title,
body = body,
ometriaNotification = ometriaNotification,
ometriaNotificationBody = ometriaNotificationBody,
collapseId = remoteMessage.collapseKey
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
package com.android.ometriasdk.notification

/**
* Created by cristiandregan
* on 27/08/2020.
* An object that exposes the content of a received notification.
*
* @param deepLinkActionUrl The URL that was sent in the notification. We append tracking parameters to the URL
* specified in the account and campaign settings (these can be changed in the Ometria app).
* @param imageUrl The image URL that was sent in the notification.
* @param campaignType Can be trigger, mass, transactional (currently only trigger is used).
* @param externalCustomerId The id of the contact that was specified at customer creation (in the mobile app) or ingesting to Ometria.
* @param sendId Unique id of the message.
* @param tracking A map that contains all tracking fields specified in the account and campaign
* settings (can be changed in the Ometria app, uses some defaults if not specified).
*/

internal data class OmetriaNotification(
val imageUrl: String?,
data class OmetriaNotification(
val deepLinkActionUrl: String?,
val context: Map<String, Any>?,
)
val imageUrl: String?,
val campaignType: String?,
val externalCustomerId: String?,
val sendId: String?,
val tracking: Map<String, Any>?
) {
override fun toString(): String {
return "deepLinkActionUrl: $deepLinkActionUrl\n\n" +
"imageUrl: $imageUrl\n\n" +
"campaignType: $campaignType\n\n" +
"externalCustomerId: $externalCustomerId\n\n" +
"sendId: $sendId\n\n" +
"tracking: $tracking"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.android.ometriasdk.notification

/**
* Created by cristiandregan
* on 27/08/2020.
*/

internal data class OmetriaNotificationBody(
val imageUrl: String?,
val deepLinkActionUrl: String?,
val context: Map<String, Any>?
)
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,10 @@ package com.android.ometriasdk.notification
* An interface that allows you to control what happens when a user interacts with an Ometria originated push notification
*/
interface OmetriaNotificationInteractionHandler {
fun onDeepLinkInteraction(deepLink: String)
@Deprecated(
message = "Use the new onNotificationInteraction(ometriaNotification: OmetriaNotification) method"
)
fun onDeepLinkInteraction(deepLink: String) {/* default implementation */ }

fun onNotificationInteraction(ometriaNotification: OmetriaNotification) {/* default implementation */ }
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import android.graphics.Bitmap
import android.os.Build
import android.os.Bundle
import androidx.core.app.NotificationCompat
import com.android.ometriasdk.core.network.dataToJson
import com.android.ometriasdk.core.network.toJson

/**
* Created by cristiandregan
Expand All @@ -31,13 +31,13 @@ internal class OmetriaPushNotification(
title: String?,
body: String?,
image: Bitmap? = null,
ometriaNotification: OmetriaNotification?,
ometriaNotificationBody: OmetriaNotificationBody?,
collapseId: String?
) {
val contentIntent = PendingIntent.getBroadcast(
context,
System.currentTimeMillis().toInt(),
getRoutingIntent(ometriaNotification),
getRoutingIntent(ometriaNotificationBody),
PendingIntent.FLAG_UPDATE_CURRENT
)

Expand Down Expand Up @@ -65,13 +65,10 @@ internal class OmetriaPushNotification(
notificationManager.notify(collapseId.hashCode(), notification)
}

private fun getRoutingIntent(ometriaNotification: OmetriaNotification?): Intent {
private fun getRoutingIntent(ometriaNotificationBody: OmetriaNotificationBody?): Intent {
val options = Bundle()
ometriaNotification?.let {
options.putString(NOTIFICATION_ACTION_URL_KEY, it.deepLinkActionUrl)
it.context?.let { context ->
options.putString(OMETRIA_CONTEXT_KEY, context.dataToJson().toString())
}
ometriaNotificationBody?.let {
options.putString(OMETRIA_NOTIFICATION_BODY_KEY, it.toJson().toString())
}

return Intent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ import android.content.Intent
import android.webkit.URLUtil
import com.android.ometriasdk.core.Ometria
import com.android.ometriasdk.core.event.OmetriaEventType
import com.android.ometriasdk.core.network.toMap
import org.json.JSONObject
import com.android.ometriasdk.core.network.toOmetriaNotification
import com.android.ometriasdk.core.network.toOmetriaNotificationBody

/**
* Created by cristiandregan
* on 30/07/2020.
*/

internal const val OMETRIA_CONTEXT_KEY = "key_ometria_context"
const val NOTIFICATION_ACTION_URL_KEY = "deep_link_action_url_key"
internal const val OMETRIA_NOTIFICATION_BODY_KEY = "ometria_notification_body_key"

/**
* Used by Push notification's routing intent.
Expand All @@ -26,18 +25,26 @@ internal class PushClickBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
val action = intent?.action
if (action != null && action == PUSH_TAP_ACTION && context != null) {
val deepLinkActionUrl = intent.getStringExtra(NOTIFICATION_ACTION_URL_KEY)
if (deepLinkActionUrl != null && URLUtil.isValidUrl(deepLinkActionUrl)) {
Ometria.instance().notificationInteractionHandler.onDeepLinkInteraction(deepLinkActionUrl)
} else {
val launcherIntent = context.packageManager.getLaunchIntentForPackage(context.packageName)
launcherIntent?.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
context.startActivity(launcherIntent)
}
val ometriaNotificationBody =
intent.getStringExtra(OMETRIA_NOTIFICATION_BODY_KEY)?.toOmetriaNotificationBody()


ometriaNotificationBody?.let { safeOmetriaNotificationBody ->
if (safeOmetriaNotificationBody.deepLinkActionUrl != null
&& URLUtil.isValidUrl(safeOmetriaNotificationBody.deepLinkActionUrl)
) {
Ometria.instance().notificationInteractionHandler.onDeepLinkInteraction(
safeOmetriaNotificationBody.deepLinkActionUrl
)

}
Ometria.instance().notificationInteractionHandler.onNotificationInteraction(
safeOmetriaNotificationBody.toOmetriaNotification()
)

val ometriaContextString = intent.getStringExtra(OMETRIA_CONTEXT_KEY)
ometriaContextString?.let {
Ometria.instance().trackNotificationInteractedEvent(JSONObject(it).toMap())
safeOmetriaNotificationBody.context?.let {
Ometria.instance().trackNotificationInteractedEvent(it)
}
}
}
}
Expand Down
Loading

0 comments on commit c93eddc

Please sign in to comment.