Closes 28414: NotificationManagerCompat extension to safely check if notifications are enabled

This commit is contained in:
rahulsainani 2023-01-05 17:55:58 +01:00 committed by mergify[bot]
parent 8994a66a3c
commit f62c56db2f
2 changed files with 22 additions and 6 deletions

View File

@ -0,0 +1,20 @@
/* 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.ext
import androidx.core.app.NotificationManagerCompat
/**
* Returns whether notifications are enabled, catches any exception that was thrown from
* [NotificationManagerCompat.areNotificationsEnabled] and returns false.
*/
@Suppress("TooGenericExceptionCaught")
fun NotificationManagerCompat.areNotificationsEnabledSafe(): Boolean {
return try {
areNotificationsEnabled()
} catch (e: Exception) {
false
}
}

View File

@ -11,6 +11,7 @@ import android.os.Build
import androidx.core.app.NotificationManagerCompat
import org.mozilla.fenix.GleanMetrics.Events.marketingNotificationAllowed
import org.mozilla.fenix.R
import org.mozilla.fenix.ext.areNotificationsEnabledSafe
// 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
@ -47,12 +48,7 @@ fun ensureMarketingChannelExists(context: Context): String {
channelEnabled = channel.importance != NotificationManager.IMPORTANCE_NONE
}
@Suppress("TooGenericExceptionCaught")
val notificationsEnabled = try {
NotificationManagerCompat.from(context).areNotificationsEnabled()
} catch (e: Exception) {
false
}
val notificationsEnabled = NotificationManagerCompat.from(context).areNotificationsEnabledSafe()
marketingNotificationAllowed.set(notificationsEnabled && channelEnabled)