Close #27949: Add engagement notification for inactive users
parent
9b920a472c
commit
11efaff96c
@ -0,0 +1,60 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.fenix.onboarding
|
||||
|
||||
import android.app.NotificationChannel
|
||||
import android.app.NotificationManager
|
||||
import android.content.Context
|
||||
import android.os.Build
|
||||
import androidx.core.app.NotificationManagerCompat
|
||||
import org.mozilla.fenix.GleanMetrics.Events.marketingNotificationAllowed
|
||||
import org.mozilla.fenix.R
|
||||
|
||||
// Channel ID was not updated when it was renamed to marketing. Thus, we'll have to continue
|
||||
// to use this ID as the marketing channel ID
|
||||
private const val MARKETING_CHANNEL_ID = "org.mozilla.fenix.default.browser.channel"
|
||||
|
||||
// For notification that uses the marketing notification channel, IDs should be unique.
|
||||
const val DEFAULT_BROWSER_NOTIFICATION_ID = 1
|
||||
const val RE_ENGAGEMENT_NOTIFICATION_ID = 2
|
||||
|
||||
/**
|
||||
* Make sure the marketing notification channel exists.
|
||||
*
|
||||
* Returns the channel id to be used for notifications.
|
||||
*/
|
||||
fun ensureMarketingChannelExists(context: Context): String {
|
||||
var channelEnabled = true
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
val notificationManager: NotificationManager =
|
||||
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||
|
||||
var channel =
|
||||
notificationManager.getNotificationChannel(MARKETING_CHANNEL_ID)
|
||||
|
||||
if (channel == null) {
|
||||
channel = NotificationChannel(
|
||||
MARKETING_CHANNEL_ID,
|
||||
context.getString(R.string.notification_marketing_channel_name),
|
||||
NotificationManager.IMPORTANCE_DEFAULT,
|
||||
)
|
||||
|
||||
notificationManager.createNotificationChannel(channel)
|
||||
}
|
||||
|
||||
channelEnabled = channel.importance != NotificationManager.IMPORTANCE_NONE
|
||||
}
|
||||
|
||||
@Suppress("TooGenericExceptionCaught")
|
||||
val notificationsEnabled = try {
|
||||
NotificationManagerCompat.from(context).areNotificationsEnabled()
|
||||
} catch (e: Exception) {
|
||||
false
|
||||
}
|
||||
|
||||
marketingNotificationAllowed.set(notificationsEnabled && channelEnabled)
|
||||
|
||||
return MARKETING_CHANNEL_ID
|
||||
}
|
@ -0,0 +1,139 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.fenix.onboarding
|
||||
|
||||
import android.app.Notification
|
||||
import android.app.PendingIntent
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import androidx.annotation.VisibleForTesting
|
||||
import androidx.core.app.NotificationCompat
|
||||
import androidx.core.app.NotificationManagerCompat
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.work.ExistingWorkPolicy
|
||||
import androidx.work.OneTimeWorkRequest
|
||||
import androidx.work.WorkManager
|
||||
import androidx.work.Worker
|
||||
import androidx.work.WorkerParameters
|
||||
import mozilla.components.support.base.ids.SharedIdsHelper
|
||||
import mozilla.telemetry.glean.private.NoExtras
|
||||
import org.mozilla.fenix.GleanMetrics.Events
|
||||
import org.mozilla.fenix.HomeActivity
|
||||
import org.mozilla.fenix.R
|
||||
import org.mozilla.fenix.ext.settings
|
||||
import org.mozilla.fenix.utils.IntentUtils
|
||||
import org.mozilla.fenix.utils.Settings
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
/**
|
||||
* Worker that builds and schedules the re-engagement notification
|
||||
*/
|
||||
class ReEngagementNotificationWorker(
|
||||
context: Context,
|
||||
workerParameters: WorkerParameters,
|
||||
) : Worker(context, workerParameters) {
|
||||
|
||||
override fun doWork(): Result {
|
||||
val settings = applicationContext.settings()
|
||||
|
||||
if (isActiveUser(settings) || !settings.shouldShowReEngagementNotification()) {
|
||||
return Result.success()
|
||||
}
|
||||
|
||||
val channelId = ensureMarketingChannelExists(applicationContext)
|
||||
NotificationManagerCompat.from(applicationContext)
|
||||
.notify(
|
||||
NOTIFICATION_TAG,
|
||||
RE_ENGAGEMENT_NOTIFICATION_ID,
|
||||
buildNotification(channelId),
|
||||
)
|
||||
|
||||
// re-engagement notification should only be shown once
|
||||
settings.reEngagementNotificationShown = true
|
||||
|
||||
Events.reEngagementNotifShown.record(NoExtras())
|
||||
|
||||
return Result.success()
|
||||
}
|
||||
|
||||
private fun buildNotification(channelId: String): Notification {
|
||||
val intent = Intent(applicationContext, HomeActivity::class.java)
|
||||
intent.putExtra(INTENT_RE_ENGAGEMENT_NOTIFICATION, true)
|
||||
|
||||
val pendingIntent = PendingIntent.getActivity(
|
||||
applicationContext,
|
||||
SharedIdsHelper.getNextIdForTag(applicationContext, NOTIFICATION_PENDING_INTENT_TAG),
|
||||
intent,
|
||||
IntentUtils.defaultIntentPendingFlags,
|
||||
)
|
||||
|
||||
with(applicationContext) {
|
||||
val appName = getString(R.string.app_name)
|
||||
return NotificationCompat.Builder(this, channelId)
|
||||
.setSmallIcon(R.drawable.ic_status_logo)
|
||||
.setContentTitle(
|
||||
applicationContext.getString(R.string.notification_re_engagement_title),
|
||||
)
|
||||
.setContentText(
|
||||
applicationContext.getString(R.string.notification_re_engagement_text, appName),
|
||||
)
|
||||
.setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL)
|
||||
.setColor(ContextCompat.getColor(this, R.color.primary_text_light_theme))
|
||||
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
|
||||
.setShowWhen(false)
|
||||
.setContentIntent(pendingIntent)
|
||||
.setAutoCancel(true)
|
||||
.build()
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val NOTIFICATION_TARGET_URL = "https://www.mozilla.org/firefox/privacy/"
|
||||
private const val NOTIFICATION_PENDING_INTENT_TAG = "org.mozilla.fenix.re-engagement"
|
||||
private const val INTENT_RE_ENGAGEMENT_NOTIFICATION = "org.mozilla.fenix.re-engagement.intent"
|
||||
private const val NOTIFICATION_TAG = "org.mozilla.fenix.re-engagement.tag"
|
||||
private const val NOTIFICATION_WORK_NAME = "org.mozilla.fenix.re-engagement.work"
|
||||
private const val NOTIFICATION_DELAY = Settings.TWO_DAYS_MS
|
||||
|
||||
// We are trying to reach the users that are inactive after the initial 24 hours
|
||||
private const val INACTIVE_USER_THRESHOLD = NOTIFICATION_DELAY - Settings.ONE_DAY_MS
|
||||
|
||||
/**
|
||||
* Check if the intent is from the re-engagement notification
|
||||
*/
|
||||
fun isReEngagementNotificationIntent(intent: Intent) =
|
||||
intent.extras?.containsKey(INTENT_RE_ENGAGEMENT_NOTIFICATION) ?: false
|
||||
|
||||
/**
|
||||
* Schedules the re-engagement notification if needed.
|
||||
*/
|
||||
fun setReEngagementNotificationIfNeeded(context: Context) {
|
||||
val instanceWorkManager = WorkManager.getInstance(context)
|
||||
|
||||
if (!context.settings().shouldSetReEngagementNotification()) {
|
||||
return
|
||||
}
|
||||
|
||||
val notificationWork = OneTimeWorkRequest.Builder(ReEngagementNotificationWorker::class.java)
|
||||
.setInitialDelay(NOTIFICATION_DELAY, TimeUnit.MILLISECONDS)
|
||||
.build()
|
||||
|
||||
instanceWorkManager.beginUniqueWork(
|
||||
NOTIFICATION_WORK_NAME,
|
||||
ExistingWorkPolicy.KEEP,
|
||||
notificationWork,
|
||||
).enqueue()
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
internal fun isActiveUser(settings: Settings): Boolean {
|
||||
if (System.currentTimeMillis() - settings.lastBrowseActivity > INACTIVE_USER_THRESHOLD) {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.fenix.onboarding
|
||||
|
||||
import io.mockk.spyk
|
||||
import junit.framework.TestCase.assertFalse
|
||||
import mozilla.components.support.test.robolectric.testContext
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
|
||||
import org.mozilla.fenix.utils.Settings
|
||||
|
||||
@RunWith(FenixRobolectricTestRunner::class)
|
||||
class ReEngagementNotificationWorkerTest {
|
||||
lateinit var settings: Settings
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
settings = Settings(testContext)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `GIVEN last browser activity THEN determine if the user is active correctly`() {
|
||||
val localSetting = spyk(settings)
|
||||
|
||||
localSetting.lastBrowseActivity = System.currentTimeMillis()
|
||||
assert(ReEngagementNotificationWorker.isActiveUser(localSetting))
|
||||
|
||||
localSetting.lastBrowseActivity = System.currentTimeMillis() - Settings.FOUR_HOURS_MS
|
||||
assert(ReEngagementNotificationWorker.isActiveUser(localSetting))
|
||||
|
||||
localSetting.lastBrowseActivity = System.currentTimeMillis() - Settings.ONE_DAY_MS
|
||||
assertFalse(ReEngagementNotificationWorker.isActiveUser(localSetting))
|
||||
|
||||
localSetting.lastBrowseActivity = 0
|
||||
assertFalse(ReEngagementNotificationWorker.isActiveUser(localSetting))
|
||||
|
||||
localSetting.lastBrowseActivity = -1000
|
||||
assertFalse(ReEngagementNotificationWorker.isActiveUser(localSetting))
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue