Skip to content

Commit

Permalink
setNotificationsOptions end point (#321)
Browse files Browse the repository at this point in the history
* make notification take same custom icon and background as foreground service

* implemented new radarnotificationsoptions class with usage in creating notifications and tests

* cleanup

* make name singular and formatting changes

* changed comments

* add punctioation to comment

* spacing formatting
  • Loading branch information
KennyHuRadar authored Nov 14, 2023
1 parent c09eebe commit 00dc134
Show file tree
Hide file tree
Showing 7 changed files with 214 additions and 7 deletions.
2 changes: 1 addition & 1 deletion sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ apply plugin: "org.jetbrains.dokka"
apply plugin: 'io.radar.mvnpublish'

ext {
radarVersion = '3.8.15'
radarVersion = '3.8.16'
}

String buildNumber = ".${System.currentTimeMillis()}"
Expand Down
17 changes: 17 additions & 0 deletions sdk/src/main/java/io/radar/sdk/Radar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1335,6 +1335,23 @@ object Radar {
RadarSettings.setForegroundService(context, options)
}

/**
* Settings for the all notifications.
*
* @see [](https://radar.com/documentation/sdk)
*
* @param[options] Notifications options
*/
@JvmStatic
fun setNotificationOptions(options: RadarNotificationOptions) {
if (!initialized) {
return
}

RadarSettings.setNotificationOptions(context, options)
}


/**
* Sets a receiver for client-side delivery of events, location updates, and debug logs.
*
Expand Down
18 changes: 14 additions & 4 deletions sdk/src/main/java/io/radar/sdk/RadarNotificationHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.app.PendingIntent
import android.content.Intent
import androidx.core.app.NotificationCompat
import io.radar.sdk.model.RadarEvent
import android.graphics.Color

class RadarNotificationHelper {

Expand Down Expand Up @@ -49,15 +50,24 @@ class RadarNotificationHelper {
channel.enableVibration(true)
notificationManager?.createNotificationChannel(channel)

val notification = NotificationCompat.Builder(context, CHANNEL_NAME)
.setSmallIcon(context.applicationContext.applicationInfo.icon)
val notificationOptions = RadarSettings.getNotificationOptions(context);

val iconString = notificationOptions?.getEventIcon()?: context.applicationContext.applicationInfo.icon.toString()
val smallIcon = context.applicationContext.resources.getIdentifier(iconString, "drawable", context.applicationContext.packageName)

val builder = NotificationCompat.Builder(context, CHANNEL_NAME)
.setSmallIcon(smallIcon)
.setAutoCancel(true)
.setContentText(notificationText)
.setStyle(NotificationCompat.BigTextStyle().bigText(notificationText))
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.build()

notificationManager?.notify(id, NOTIFICATION_ID, notification)
val iconColor = notificationOptions?.getEventColor()?: ""
if (iconColor.isNotEmpty()) {
builder.setColor(Color.parseColor(iconColor))
}

notificationManager?.notify(id, NOTIFICATION_ID, builder.build())
}
}
}
Expand Down
93 changes: 93 additions & 0 deletions sdk/src/main/java/io/radar/sdk/RadarNotificationOptions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package io.radar.sdk

import org.json.JSONObject
import java.util.Date

/**
* An options class used to configure local notifications.
*
* @see [](https://radar.com/documentation/sdk/android)
*/
data class RadarNotificationOptions(
/**
* Determines the name of the asset to be used for notifications. Optional, defaults to app icon.
*/
val iconString: String? = null,

/**
* Determines the background color of used for notifications. Optional.
*/
val iconColor: String? = null,

/**
* Determines the name of the asset to be used for forgroundService notifications. Optional, defaults to iconString.
*/
val foregroundServiceIconString: String? = null,

/**
* Determines the name of the asset to be used for forgroundService notifications. Optional, defaults to iconString.
*/
val foregroundServiceIconColor: String? = null,

/**
* Determines the name of the asset to be used for event notifications. Optional, defaults to iconString.
*/
val eventIconString:String? = null,

/**
* Determines the name of the asset to be used for event notifications. Optional, defaults to iconString.
*/
val eventIconColor: String? = null,
) {

companion object {

internal const val KEY_ICON_STRING = "iconString"
internal const val KEY_ICON_COLOR = "iconColor"
internal const val KEY_FOREGROUNDSERVICE_ICON_STRING = "foregroundServiceIconString"
internal const val KEY_FOREGROUNDSERVICE_ICON_COLOR = "foregroundServiceIconColor"
internal const val KEY_EVENT_ICON_STRING = "eventIconString"
internal const val KEY_EVENT_ICON_COLOR = "eventIconColor"


@JvmStatic
fun fromJson(obj: JSONObject): RadarNotificationOptions {
val iconString = if (obj.isNull(KEY_ICON_STRING)) null else obj.optString(KEY_ICON_STRING)
val iconColor = if (obj.isNull(KEY_ICON_COLOR)) null else obj.optString(KEY_ICON_COLOR)
val foregroundServiceIconString = if (obj.isNull(KEY_FOREGROUNDSERVICE_ICON_STRING)) null else obj.optString(KEY_FOREGROUNDSERVICE_ICON_STRING)
val foregroundServiceIconColor = if (obj.isNull(KEY_FOREGROUNDSERVICE_ICON_COLOR)) null else obj.optString(KEY_FOREGROUNDSERVICE_ICON_COLOR)
val eventIconString = if (obj.isNull(KEY_EVENT_ICON_STRING)) null else obj.optString(KEY_EVENT_ICON_STRING)
val eventIconColor = if (obj.isNull(KEY_EVENT_ICON_COLOR)) null else obj.optString(KEY_EVENT_ICON_COLOR)

return RadarNotificationOptions(iconString, iconColor, foregroundServiceIconString, foregroundServiceIconColor, eventIconString, eventIconColor)
}
}

fun toJson(): JSONObject {
val obj = JSONObject()
obj.put(KEY_ICON_STRING, iconString)
obj.put(KEY_ICON_COLOR, iconColor)
obj.put(KEY_FOREGROUNDSERVICE_ICON_STRING, foregroundServiceIconString)
obj.put(KEY_FOREGROUNDSERVICE_ICON_COLOR, foregroundServiceIconColor)
obj.put(KEY_EVENT_ICON_STRING, eventIconString)
obj.put(KEY_EVENT_ICON_COLOR, eventIconColor)
return obj
}

fun getForegroundServiceIcon(): String? {
return foregroundServiceIconString ?: iconString
}

fun getForegroundServiceColor(): String? {
return foregroundServiceIconColor ?: iconColor
}

fun getEventIcon(): String? {
return eventIconString ?: iconString
}

fun getEventColor(): String? {
return eventIconColor ?: iconColor
}

}
34 changes: 34 additions & 0 deletions sdk/src/main/java/io/radar/sdk/RadarSettings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ internal object RadarSettings {
private const val KEY_PREVIOUS_TRACKING_OPTIONS = "previous_tracking_options"
private const val KEY_REMOTE_TRACKING_OPTIONS = "remote_tracking_options"
private const val KEY_FOREGROUND_SERVICE = "foreground_service"
private const val KEY_NOTIFICATION_OPTIONS = "notification_options"
private const val KEY_FEATURE_SETTINGS = "feature_settings"
private const val KEY_TRIP_OPTIONS = "trip_options"
private const val KEY_LOG_LEVEL = "log_level"
Expand Down Expand Up @@ -226,6 +227,31 @@ internal object RadarSettings {
getSharedPreferences(context).edit { remove(KEY_REMOTE_TRACKING_OPTIONS) }
}

internal fun setNotificationOptions(context:Context,notificationOptions:RadarNotificationOptions){
val notificationOptionsJson = notificationOptions.toJson().toString()
getSharedPreferences(context).edit { putString(KEY_NOTIFICATION_OPTIONS, notificationOptionsJson) }
// Update foregroundServiceOptions as well.
var previousValue = getForegroundService(context)
setForegroundService(context,RadarTrackingOptions.RadarTrackingOptionsForegroundService(
previousValue.text,
previousValue.title,
previousValue.icon,
previousValue.updatesOnly,
previousValue.activity,
previousValue.importance,
previousValue.id,
previousValue.channelName,
notificationOptions.getForegroundServiceIcon()?:previousValue.iconString,
notificationOptions.getForegroundServiceColor()?:previousValue.iconColor
))
}

internal fun getNotificationOptions(context: Context):RadarNotificationOptions?{
val optionsJson = getSharedPreferences(context).getString(KEY_NOTIFICATION_OPTIONS, null) ?: return null
val optionsObj = JSONObject(optionsJson)
return RadarNotificationOptions.fromJson(optionsObj)
}

internal fun getForegroundService(context: Context): RadarTrackingOptions.RadarTrackingOptionsForegroundService {
val foregroundJson = getSharedPreferences(context).getString(KEY_FOREGROUND_SERVICE, null)
var foregroundService: RadarTrackingOptions.RadarTrackingOptionsForegroundService? = null
Expand All @@ -243,6 +269,14 @@ internal object RadarSettings {
context: Context,
foregroundService: RadarTrackingOptions.RadarTrackingOptionsForegroundService
) {
// Previous values of iconColor and iconString are preserved if new fields are null.
val previousValue = getForegroundService(context)
if (foregroundService.iconString == null) {
foregroundService.iconString = previousValue.iconString
}
if (foregroundService.iconColor == null) {
foregroundService.iconColor = previousValue.iconColor
}
val foregroundJson = foregroundService.toJson().toString()
getSharedPreferences(context).edit { putString(KEY_FOREGROUND_SERVICE, foregroundJson) }
}
Expand Down
4 changes: 2 additions & 2 deletions sdk/src/main/java/io/radar/sdk/RadarTrackingOptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -293,12 +293,12 @@ data class RadarTrackingOptions(
/**
* Determines the notification icon, like `R.drawable.ic_your_icon`. Optional, defaults to `applicationContext.applicationInfo.icon`.
*/
val iconString: String? = null,
var iconString: String? = null,

/**
* Determines the color notification icon. Optional.
*/
val iconColor: String? = null,
var iconColor: String? = null,
) {

companion object {
Expand Down
53 changes: 53 additions & 0 deletions sdk/src/test/java/io/radar/sdk/RadarTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,59 @@ class RadarTest {
assertEquals(metadata.toString(), Radar.getMetadata()?.toString())
}

@Test
fun test_Radar_setNotificationsOptions() {
val notificationOptions = RadarNotificationOptions(
"foo",
"red",
"bar",
"blue",
"hello",
"white")
Radar.setNotificationOptions(notificationOptions)
assertEquals(notificationOptions, RadarSettings.getNotificationOptions(context))
}


@Test
fun test_Radar_notificationSettingDefaults() {
Radar.setForegroundServiceOptions(RadarTrackingOptions.RadarTrackingOptionsForegroundService(
text = "Text",
title = "Title",
icon = 1337,
updatesOnly = true,
))
// Radar.setNotificationOptions has side effects on foregroundServiceOptions.
Radar.setNotificationOptions(RadarNotificationOptions(
"foo",
"red",
"bar",
"blue",
"hello",
"white"))
assertEquals("bar", RadarSettings.getForegroundService(context).iconString)
assertEquals("blue", RadarSettings.getForegroundService(context).iconColor)
// We do not clear existing values of iconString and iconColor with null values.
Radar.setForegroundServiceOptions(RadarTrackingOptions.RadarTrackingOptionsForegroundService(
text = "Text",
title = "Title",
icon = 1337,
updatesOnly = true,
))
assertEquals("bar", RadarSettings.getForegroundService(context).iconString)
assertEquals("blue", RadarSettings.getForegroundService(context).iconColor)
Radar.setForegroundServiceOptions(RadarTrackingOptions.RadarTrackingOptionsForegroundService(
text = "Text",
title = "Title",
iconString = "test",
iconColor = "red",
icon = 1337,
updatesOnly = true,
))
assertEquals("test", RadarSettings.getForegroundService(context).iconString)
assertEquals("red", RadarSettings.getForegroundService(context).iconColor)
}

@Test
fun test_Radar_setMetadata_null() {
Radar.setMetadata(null)
Expand Down

0 comments on commit 00dc134

Please sign in to comment.