Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Experiment/WIP] Android notification improvements. #407

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions example/src/main/java/io/radar/example/MyRadarReceiver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package io.radar.example

import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.location.Location
import android.os.Build
import androidx.core.app.NotificationCompat
Expand All @@ -12,7 +14,7 @@ import io.radar.sdk.RadarReceiver
import io.radar.sdk.model.RadarEvent
import io.radar.sdk.model.RadarUser

class MyRadarReceiver : RadarReceiver() {
class MyRadarReceiver() : RadarReceiver() {

companion object {

Expand All @@ -33,11 +35,18 @@ class MyRadarReceiver : RadarReceiver() {
notificationManager.createNotificationChannel(channel)
}

val intent = Intent(context, MainActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
val pendingIntent: PendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE)

val builder = NotificationCompat.Builder(context, channelId)
.setSmallIcon(R.drawable.ic_notification)
.setStyle(NotificationCompat.BigTextStyle().bigText(body))
.setContentText(body)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setContentIntent(pendingIntent)
.setAutoCancel(true)

with(NotificationManagerCompat.from(context)) {
notify(identifier % 20, builder.build())
Expand All @@ -47,7 +56,10 @@ class MyRadarReceiver : RadarReceiver() {
}

override fun onEventsReceived(context: Context, events: Array<RadarEvent>, user: RadarUser?) {
events.forEach { event -> notify(context, Utils.stringForRadarEvent(event)) }
events.forEach { event -> {
//notify(context, Utils.stringForRadarEvent(event))
}
}
}

override fun onLocationUpdated(context: Context, location: Location, user: RadarUser) {
Expand All @@ -61,11 +73,11 @@ class MyRadarReceiver : RadarReceiver() {
}

override fun onError(context: Context, status: Radar.RadarStatus) {
notify(context, Utils.stringForRadarStatus(status))
//notify(context, Utils.stringForRadarStatus(status))
}

override fun onLog(context: Context, message: String) {
notify(context, message)
//notify(context, message)
}

}
8 changes: 8 additions & 0 deletions sdk/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver
android:name=".RadarNotificationReceiver"
android:enabled="true"
android:exported="false">
<intent-filter android:priority="999">
<action android:name="io.radar.sdk.NotificationReceiver.CONVERSION" />
</intent-filter>
</receiver>
<service android:name=".RadarJobScheduler"
android:permission="android.permission.BIND_JOB_SERVICE"
android:exported="true" />
Expand Down
26 changes: 23 additions & 3 deletions sdk/src/main/java/io/radar/sdk/Radar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ object Radar {
internal var initialized = false
internal var isFlushingReplays = false
private lateinit var context: Context
private var activity: Activity? = null
internal var activity: Activity? = null
internal lateinit var handler: Handler
private var receiver: RadarReceiver? = null
private var verifiedReceiver: RadarVerifiedReceiver? = null
Expand Down Expand Up @@ -3166,12 +3166,32 @@ object Radar {
if (!RadarSettings.getSdkConfiguration(context).useOpenedAppConversion) {
return
}
// if opened_app has been logged in the last 1000 milliseconds, don't log it again
// if opened_app_test has been logged in the last 1000 milliseconds, don't log it again
val timestamp = System.currentTimeMillis()
val lastAppOpenTime = RadarSettings.getLastAppOpenTimeMillis(context)
if (timestamp - lastAppOpenTime > 1000) {
RadarSettings.updateLastAppOpenTimeMillis(context)
sendLogConversionRequest("opened_app", callback = object : RadarLogConversionCallback {
sendLogConversionRequest("opened_app_test", callback = object : RadarLogConversionCallback {
override fun onComplete(status: RadarStatus, event: RadarEvent?) {
logger.i("Conversion name = ${event?.conversionName}: status = $status; event = $event")
}
})
}
}

internal fun logOpenedAppConversionFromNotification() {
if (!RadarSettings.getSdkConfiguration(context).useOpenedAppConversion) {
return
}
// if opened_app_test has been logged in the last 1000 milliseconds, don't log it again
val timestamp = System.currentTimeMillis()
val lastAppOpenTime = RadarSettings.getLastAppOpenTimeMillis(context)
if (timestamp - lastAppOpenTime > 1000) {
RadarSettings.updateLastAppOpenTimeMillis(context)
val metaData = JSONObject().apply {
put("conversionSource", "radar_notification")
}
sendLogConversionRequest("opened_app_test", metaData, callback = object : RadarLogConversionCallback {
override fun onComplete(status: RadarStatus, event: RadarEvent?) {
logger.i("Conversion name = ${event?.conversionName}: status = $status; event = $event")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,5 +139,11 @@ internal class RadarActivityLifecycleCallbacks(
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
Log.w(TAG, "ON CREATE ${count}")
updatePermissionsDenied(activity)
val res = activity.intent.extras?.getBoolean("radarNotification")
Log.i("testing",activity.intent.extras.toString())
if (res == true) {
Log.i("testing","called from notification")
Radar.logOpenedAppConversionFromNotification()
}
}
}
58 changes: 47 additions & 11 deletions sdk/src/main/java/io/radar/sdk/RadarNotificationHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,36 @@ import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.content.Context.NOTIFICATION_SERVICE
import android.graphics.Color
import android.os.Build
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 {

internal companion object {
private const val CHANNEL_NAME = "Location"
private const val NOTIFICATION_ID = 20160525 // Radar's birthday!
val SUMMARY_NOTIFICATION_GROUP = "io.radar.sdk.event_notification"
val SUMMARY_ID = 0

@SuppressLint("DiscouragedApi")
@SuppressLint("DiscouragedApi", "LaunchActivityFromNotification")
internal fun showNotifications(context: Context, events: Array<RadarEvent>) {
if (Build.VERSION.SDK_INT < 26) {
return
}

var notificationsCount = 0
val notificationManager =
context.getSystemService(NOTIFICATION_SERVICE) as? NotificationManager

val importance = NotificationManager.IMPORTANCE_HIGH
val channel = NotificationChannel(CHANNEL_NAME, CHANNEL_NAME, importance)
channel.enableVibration(true)
notificationManager?.createNotificationChannel(channel)

for (event in events) {
var notificationText: String? = null
var notificationText: String? = "dummy var"

if (event.type == RadarEvent.RadarEventType.USER_ENTERED_GEOFENCE) {
notificationText = event.geofence?.metadata?.optString("radar:entryNotificationText")
Expand All @@ -44,13 +53,7 @@ class RadarNotificationHelper {
if (notificationText != null && notificationText.isNotEmpty()) {
val id = event._id

val notificationManager =
context.getSystemService(NOTIFICATION_SERVICE) as? NotificationManager

val importance = NotificationManager.IMPORTANCE_HIGH
val channel = NotificationChannel(CHANNEL_NAME, CHANNEL_NAME, importance)
channel.enableVibration(true)
notificationManager?.createNotificationChannel(channel)

val notificationOptions = RadarSettings.getNotificationOptions(context);

Expand All @@ -68,11 +71,44 @@ class RadarNotificationHelper {
if (iconColor.isNotEmpty()) {
builder.setColor(Color.parseColor(iconColor))
}

builder.setContentIntent(RadarNotificationReceiver.getNotificationConversionPendingIntent(context))
builder.setAutoCancel(true)
builder.setGroup(SUMMARY_NOTIFICATION_GROUP)

notificationManager?.notify(id, NOTIFICATION_ID, builder.build())
notificationsCount++
}
}
if (notificationsCount > 0) {
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 notificationText = "You have ${notificationsCount} location sensitive notifications"
val builder = NotificationCompat.Builder(context, CHANNEL_NAME)
.setSmallIcon(smallIcon)
.setAutoCancel(true)
.setContentText(notificationText)
.setStyle(NotificationCompat.BigTextStyle().bigText(notificationText))
.setPriority(NotificationCompat.PRIORITY_DEFAULT)

val iconColor = notificationOptions?.getEventColor()?: ""
if (iconColor.isNotEmpty()) {
builder.setColor(Color.parseColor(iconColor))
}

builder.setContentIntent(RadarNotificationReceiver.getNotificationConversionPendingIntent(context))
builder.setAutoCancel(true)
builder.setGroup(SUMMARY_NOTIFICATION_GROUP)
builder.setGroupSummary(true)

notificationManager?.notify(SUMMARY_ID, builder.build())
}


}

}

}
3 changes: 2 additions & 1 deletion sdk/src/main/java/io/radar/sdk/RadarNotificationOptions.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.radar.sdk

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

/**
* An options class used to configure local notifications.
Expand Down Expand Up @@ -38,6 +37,8 @@ data class RadarNotificationOptions(
* Determines the name of the asset to be used for event notifications. Optional, defaults to iconString.
*/
val eventIconColor: String? = null,


) {

companion object {
Expand Down
39 changes: 39 additions & 0 deletions sdk/src/main/java/io/radar/sdk/RadarNotificationReceiver.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.radar.sdk

import android.app.PendingIntent
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log

class RadarNotificationReceiver : BroadcastReceiver() {

internal companion object{
internal const val ACTION_NOTIFICATION_CONVERSION = "io.radar.sdk.NotificationReceiver.CONVERSION"
private const val REQUEST_CODE_NOTIFICATION_CONVERSION = 201605255
internal fun getNotificationConversionPendingIntent(context: Context): PendingIntent? {
val activityIntent = Intent(context, Radar.activity!!::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
activityIntent.putExtra("radarNotification", true)
val pendingIntent: PendingIntent = PendingIntent.getActivity(context, 0, activityIntent,
PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE)
return pendingIntent
}
private fun baseIntent(context: Context): Intent = Intent(context, RadarNotificationReceiver::class.java)
}
override fun onReceive(context: Context, intent: Intent) {
if (!Radar.initialized) {
Radar.initialize(context)
}
if (intent.action == ACTION_NOTIFICATION_CONVERSION) {
Radar.logger.d("Received broadcast inside radarnoticiationreciever | action = ${intent.action}")

val mainIntent = Intent(context, Radar.activity!!::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
context.startActivity(mainIntent)
Log.i("testing", "running chunk of code")
}
}
}