Close #27795: Add metrics ping to see if user allows notifications

This commit is contained in:
Roger Yang 2022-11-10 11:03:11 -05:00 committed by mergify[bot]
parent 20b396e0cd
commit 5222d2bd8b
3 changed files with 62 additions and 4 deletions

View File

@ -211,6 +211,22 @@ events:
notification_emails:
- android-probes@mozilla.com
expires: 122
marketing_notification_allowed:
type: boolean
description: |
True if marketing notifications are allowed, otherwise false.
bugs:
- https://github.com/mozilla-mobile/fenix/issues/27795
data_reviews:
- https://github.com/mozilla-mobile/fenix/pull/27797
data_sensitivity:
- technical
notification_emails:
- android-probes@mozilla.com
expires: 122
metadata:
tags:
- Notifications
toolbar_menu_visible:
type: event
description: |
@ -1752,6 +1768,24 @@ metrics:
metadata:
tags:
- Wallpapers
notifications_allowed:
type: boolean
description: |
True if notifications are allowed, otherwise false.
send_in_pings:
- metrics
bugs:
- https://github.com/mozilla-mobile/fenix/issues/27795
data_reviews:
- https://github.com/mozilla-mobile/fenix/pull/27797
data_sensitivity:
- technical
notification_emails:
- android-probes@mozilla.com
expires: 122
metadata:
tags:
- Notifications
customize_home:
most_visited_sites:

View File

@ -14,6 +14,7 @@ import android.util.Log.INFO
import androidx.annotation.CallSuper
import androidx.annotation.VisibleForTesting
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.app.NotificationManagerCompat
import androidx.core.content.getSystemService
import androidx.lifecycle.ProcessLifecycleOwner
import androidx.work.Configuration.Builder
@ -714,6 +715,15 @@ open class FenixApplication : LocaleAwareApplication(), Provider {
Wallpaper.nameIsDefault(settings.currentWallpaperName)
defaultWallpaper.set(isDefaultTheCurrentWallpaper)
@Suppress("TooGenericExceptionCaught")
try {
notificationsAllowed.set(
NotificationManagerCompat.from(applicationContext).areNotificationsEnabled(),
)
} catch (e: Exception) {
Logger.warn("Failed to check if notifications are enabled", e)
}
}
with(AndroidAutofill) {

View File

@ -22,6 +22,7 @@ import androidx.work.WorkerParameters
import mozilla.components.service.glean.private.NoExtras
import mozilla.components.support.base.ids.SharedIdsHelper
import org.mozilla.fenix.GleanMetrics.Events
import org.mozilla.fenix.GleanMetrics.Events.marketingNotificationAllowed
import org.mozilla.fenix.HomeActivity
import org.mozilla.fenix.R
import org.mozilla.fenix.ext.settings
@ -35,9 +36,9 @@ class DefaultBrowserNotificationWorker(
) : Worker(context, workerParameters) {
override fun doWork(): Result {
ensureChannelExists()
val channelId = ensureChannelExists()
NotificationManagerCompat.from(applicationContext)
.notify(NOTIFICATION_TAG, NOTIFICATION_ID, buildNotification())
.notify(NOTIFICATION_TAG, NOTIFICATION_ID, buildNotification(channelId))
Events.defaultBrowserNotifShown.record(NoExtras())
// default browser notification should only happen once
@ -49,8 +50,7 @@ class DefaultBrowserNotificationWorker(
/**
* Build the default browser notification.
*/
private fun buildNotification(): Notification {
val channelId = ensureChannelExists()
private fun buildNotification(channelId: String): Notification {
val intent = Intent(applicationContext, HomeActivity::class.java)
intent.putExtra(INTENT_DEFAULT_BROWSER_NOTIFICATION, true)
@ -87,6 +87,7 @@ class DefaultBrowserNotificationWorker(
* Returns the channel id to be used for notifications.
*/
private fun ensureChannelExists(): String {
var channelEnabled = true
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationManager: NotificationManager =
applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
@ -98,8 +99,21 @@ class DefaultBrowserNotificationWorker(
)
notificationManager.createNotificationChannel(channel)
val existingChannel = notificationManager.getNotificationChannel(NOTIFICATION_CHANNEL_ID)
channelEnabled =
existingChannel != null && existingChannel.importance != NotificationManager.IMPORTANCE_NONE
}
@Suppress("TooGenericExceptionCaught")
val notificationsEnabled = try {
NotificationManagerCompat.from(applicationContext).areNotificationsEnabled()
} catch (e: Exception) {
false
}
marketingNotificationAllowed.set(notificationsEnabled && channelEnabled)
return NOTIFICATION_CHANNEL_ID
}