From 582f852e7adcdecc2d493e05f943b01dd5941b65 Mon Sep 17 00:00:00 2001 From: Aayush Gupta Date: Thu, 5 Feb 2026 12:56:37 +0800 Subject: [PATCH] Add missing permission checks for notifications Notifications can be disabled manually even after permission has been granted once. Always check if they are enabled before notifying the user. Signed-off-by: Aayush Gupta --- .../org/schabi/newpipe/NewVersionWorker.kt | 4 +++- .../org/schabi/newpipe/error/ErrorUtil.kt | 7 +++++-- .../feed/notifications/NotificationHelper.kt | 21 ++++++++++++------- .../local/feed/service/FeedLoadService.kt | 4 +++- .../services/BaseImportExportService.java | 9 ++++++-- .../player/notification/NotificationUtil.java | 8 +++++-- 6 files changed, 38 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/NewVersionWorker.kt b/app/src/main/java/org/schabi/newpipe/NewVersionWorker.kt index fb48d3f70..4cdcc6c69 100644 --- a/app/src/main/java/org/schabi/newpipe/NewVersionWorker.kt +++ b/app/src/main/java/org/schabi/newpipe/NewVersionWorker.kt @@ -82,7 +82,9 @@ class NewVersionWorker( ) val notificationManager = NotificationManagerCompat.from(applicationContext) - notificationManager.notify(2000, notificationBuilder.build()) + if (notificationManager.areNotificationsEnabled()) { + notificationManager.notify(2000, notificationBuilder.build()) + } } @Throws(IOException::class, ReCaptchaException::class) diff --git a/app/src/main/java/org/schabi/newpipe/error/ErrorUtil.kt b/app/src/main/java/org/schabi/newpipe/error/ErrorUtil.kt index 7facb5d85..0fa302623 100644 --- a/app/src/main/java/org/schabi/newpipe/error/ErrorUtil.kt +++ b/app/src/main/java/org/schabi/newpipe/error/ErrorUtil.kt @@ -134,8 +134,11 @@ class ErrorUtil { ) ) - NotificationManagerCompat.from(context) - .notify(ERROR_REPORT_NOTIFICATION_ID, notificationBuilder.build()) + val notificationManager = NotificationManagerCompat.from(context) + if (notificationManager.areNotificationsEnabled()) { + notificationManager + .notify(ERROR_REPORT_NOTIFICATION_ID, notificationBuilder.build()) + } ContextCompat.getMainExecutor(context).execute { // since the notification is silent, also show a toast, otherwise the user is confused diff --git a/app/src/main/java/org/schabi/newpipe/local/feed/notifications/NotificationHelper.kt b/app/src/main/java/org/schabi/newpipe/local/feed/notifications/NotificationHelper.kt index d2d16a755..aa03aafc5 100644 --- a/app/src/main/java/org/schabi/newpipe/local/feed/notifications/NotificationHelper.kt +++ b/app/src/main/java/org/schabi/newpipe/local/feed/notifications/NotificationHelper.kt @@ -92,8 +92,10 @@ class NotificationHelper(val context: Context) { // Show individual stream notifications, set channel icon only if there is actually // one showStreamNotifications(newStreams, data.serviceId, data.url, bitmap) - // Show summary notification - manager.notify(data.pseudoId, summaryBuilder.build()) + // Show summary notification if enabled + if (manager.areNotificationsEnabled()) { + manager.notify(data.pseudoId, summaryBuilder.build()) + } iconLoadingTargets.remove(this) // allow it to be garbage-collected } @@ -101,8 +103,10 @@ class NotificationHelper(val context: Context) { override fun onBitmapFailed(e: Exception, errorDrawable: Drawable) { // Show individual stream notifications showStreamNotifications(newStreams, data.serviceId, data.url, null) - // Show summary notification - manager.notify(data.pseudoId, summaryBuilder.build()) + // Show summary notification if enabled + if (manager.areNotificationsEnabled()) { + manager.notify(data.pseudoId, summaryBuilder.build()) + } iconLoadingTargets.remove(this) // allow it to be garbage-collected } @@ -124,9 +128,12 @@ class NotificationHelper(val context: Context) { channelUrl: String, channelIcon: Bitmap? ) { - for (stream in newStreams) { - val notification = createStreamNotification(stream, serviceId, channelUrl, channelIcon) - manager.notify(stream.url.hashCode(), notification) + if (manager.areNotificationsEnabled()) { + newStreams.forEach { stream -> + val notification = + createStreamNotification(stream, serviceId, channelUrl, channelIcon) + manager.notify(stream.url.hashCode(), notification) + } } } diff --git a/app/src/main/java/org/schabi/newpipe/local/feed/service/FeedLoadService.kt b/app/src/main/java/org/schabi/newpipe/local/feed/service/FeedLoadService.kt index 48ad5df94..bd128b3bc 100644 --- a/app/src/main/java/org/schabi/newpipe/local/feed/service/FeedLoadService.kt +++ b/app/src/main/java/org/schabi/newpipe/local/feed/service/FeedLoadService.kt @@ -185,7 +185,9 @@ class FeedLoadService : Service() { } } - notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build()) + if (notificationManager.areNotificationsEnabled()) { + notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build()) + } } // ///////////////////////////////////////////////////////////////////////// diff --git a/app/src/main/java/org/schabi/newpipe/local/subscription/services/BaseImportExportService.java b/app/src/main/java/org/schabi/newpipe/local/subscription/services/BaseImportExportService.java index b7c11b160..850409e25 100644 --- a/app/src/main/java/org/schabi/newpipe/local/subscription/services/BaseImportExportService.java +++ b/app/src/main/java/org/schabi/newpipe/local/subscription/services/BaseImportExportService.java @@ -144,7 +144,9 @@ public abstract class BaseImportExportService extends Service { notificationBuilder.setContentText(text); } - notificationManager.notify(getNotificationId(), notificationBuilder.build()); + if (notificationManager.areNotificationsEnabled()) { + notificationManager.notify(getNotificationId(), notificationBuilder.build()); + } } protected void stopService() { @@ -174,7 +176,10 @@ public abstract class BaseImportExportService extends Service { .setContentTitle(title) .setStyle(new NotificationCompat.BigTextStyle().bigText(textOrEmpty)) .setContentText(textOrEmpty); - notificationManager.notify(getNotificationId(), notificationBuilder.build()); + + if (notificationManager.areNotificationsEnabled()) { + notificationManager.notify(getNotificationId(), notificationBuilder.build()); + } } protected NotificationCompat.Builder createNotification() { diff --git a/app/src/main/java/org/schabi/newpipe/player/notification/NotificationUtil.java b/app/src/main/java/org/schabi/newpipe/player/notification/NotificationUtil.java index 9b9c47b0e..be98abb7a 100644 --- a/app/src/main/java/org/schabi/newpipe/player/notification/NotificationUtil.java +++ b/app/src/main/java/org/schabi/newpipe/player/notification/NotificationUtil.java @@ -72,7 +72,9 @@ public final class NotificationUtil { notificationBuilder = createNotification(); } updateNotification(); - notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build()); + if (notificationManager.areNotificationsEnabled()) { + notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build()); + } } public synchronized void updateThumbnail() { @@ -84,7 +86,9 @@ public final class NotificationUtil { } setLargeIcon(notificationBuilder); - notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build()); + if (notificationManager.areNotificationsEnabled()) { + notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build()); + } } }