From ed35e57e1c70b254836fd5a3c5a7cd0c0b8588dd Mon Sep 17 00:00:00 2001
From: Dani Palou <dani@moodle.com>
Date: Thu, 6 Jun 2019 09:44:05 +0200
Subject: [PATCH] MOBILE-3039 core: Don't allow disable sound in iOS and
 Android 8+

---
 .../notifications/pages/settings/settings.ts  |  3 ++-
 .../providers/pushnotifications.ts            | 10 ++++++++-
 src/providers/local-notifications.ts          | 21 ++++++++++++++++++-
 3 files changed, 31 insertions(+), 3 deletions(-)

diff --git a/src/addon/notifications/pages/settings/settings.ts b/src/addon/notifications/pages/settings/settings.ts
index ed4cabc45..58ab72395 100644
--- a/src/addon/notifications/pages/settings/settings.ts
+++ b/src/addon/notifications/pages/settings/settings.ts
@@ -55,7 +55,8 @@ export class AddonNotificationsSettingsPage implements OnDestroy {
             @Optional() private svComponent: CoreSplitViewComponent) {
 
         this.notifPrefsEnabled = notificationsProvider.isNotificationPreferencesEnabled();
-        this.canChangeSound = localNotificationsProvider.isAvailable() && !appProvider.isDesktop();
+        this.canChangeSound = localNotificationsProvider.canDisableSound();
+
         if (this.canChangeSound) {
             configProvider.get(CoreConstants.SETTINGS_NOTIFICATION_SOUND, true).then((enabled) => {
                 this.notificationSound = !!enabled;
diff --git a/src/core/pushnotifications/providers/pushnotifications.ts b/src/core/pushnotifications/providers/pushnotifications.ts
index c61bf7a29..f6b0201d4 100644
--- a/src/core/pushnotifications/providers/pushnotifications.ts
+++ b/src/core/pushnotifications/providers/pushnotifications.ts
@@ -246,7 +246,15 @@ export class CorePushNotificationsProvider {
      * @return {Promise<PushOptions>} Promise with the push options resolved when done.
      */
     protected getOptions(): Promise<PushOptions> {
-        return this.configProvider.get(CoreConstants.SETTINGS_NOTIFICATION_SOUND, true).then((soundEnabled) => {
+        let promise;
+
+        if (this.localNotificationsProvider.canDisableSound()) {
+            promise = this.configProvider.get(CoreConstants.SETTINGS_NOTIFICATION_SOUND, true);
+        } else {
+            promise = Promise.resolve(true);
+        }
+
+        return promise.then((soundEnabled) => {
             return {
                 android: {
                     sound: !!soundEnabled,
diff --git a/src/providers/local-notifications.ts b/src/providers/local-notifications.ts
index 308ffb4c7..ae9de1652 100644
--- a/src/providers/local-notifications.ts
+++ b/src/providers/local-notifications.ts
@@ -205,6 +205,17 @@ export class CoreLocalNotificationsProvider {
         });
     }
 
+    /**
+     * Check whether sound can be disabled for notifications.
+     *
+     * @return {boolean} Whether sound can be disabled for notifications.
+     */
+    canDisableSound(): boolean {
+        // Only allow disabling sound in Android 7 or lower. In iOS and Android 8+ it can easily be done with system settings.
+        return this.isAvailable() && !this.appProvider.isDesktop() && this.platform.is('android') &&
+                this.platform.version().major < 8;
+    }
+
     /**
      * Create the default channel. It is used to change the name.
      *
@@ -577,7 +588,15 @@ export class CoreLocalNotificationsProvider {
             return this.localNotifications.cancel(notification.id).finally(() => {
                 if (!triggered) {
                     // Check if sound is enabled for notifications.
-                    return this.configProvider.get(CoreConstants.SETTINGS_NOTIFICATION_SOUND, true).then((soundEnabled) => {
+                    let promise;
+
+                    if (this.canDisableSound()) {
+                        promise = this.configProvider.get(CoreConstants.SETTINGS_NOTIFICATION_SOUND, true);
+                    } else {
+                        promise = Promise.resolve(true);
+                    }
+
+                    return promise.then((soundEnabled) => {
                         if (!soundEnabled) {
                             notification.sound = null;
                         } else {