DEV: APIs to disable PWA badging and unsubscribe from user notifications (#14595)

DEV: APIs to disable PWA badging and unsubscribe from user notifications (#14595)

diff --git a/app/assets/javascripts/discourse/app/initializers/badging.js b/app/assets/javascripts/discourse/app/initializers/badging.js
index 73c90b0..41291d9 100644
--- a/app/assets/javascripts/discourse/app/initializers/badging.js
+++ b/app/assets/javascripts/discourse/app/initializers/badging.js
@@ -1,12 +1,19 @@
 // Updates the PWA badging if available
+
+let defaultBadgingDisabled = false;
+export function disableDefaultBadging() {
+  defaultBadgingDisabled = true;
+}
+
 export default {
   name: "badging",
   after: "message-bus",
 
   initialize(container) {
-    if (!navigator.setAppBadge) {
+    // must have the Badging API
+    if (defaultBadgingDisabled || !navigator.setAppBadge) {
       return;
-    } // must have the Badging API
+    }
 
     const user = container.lookup("current-user:main");
     if (!user) {
diff --git a/app/assets/javascripts/discourse/app/initializers/subscribe-user-notifications.js b/app/assets/javascripts/discourse/app/initializers/subscribe-user-notifications.js
index 2381e08..43f19da 100644
--- a/app/assets/javascripts/discourse/app/initializers/subscribe-user-notifications.js
+++ b/app/assets/javascripts/discourse/app/initializers/subscribe-user-notifications.js
@@ -13,11 +13,20 @@ import {
 } from "discourse/lib/push-notifications";
 import { isTesting } from "discourse-common/config/environment";
 
+let subscribeToNotifications = true;
+export function unsubscribeFromNotifications() {
+  subscribeToNotifications = false;
+}
+
 export default {
   name: "subscribe-user-notifications",
   after: "message-bus",
 
   initialize(container) {
+    if (!subscribeToNotifications) {
+      return;
+    }
+
     const user = container.lookup("current-user:main");
     const bus = container.lookup("message-bus:main");
     const appEvents = container.lookup("service:app-events");
diff --git a/app/assets/javascripts/discourse/app/lib/plugin-api.js b/app/assets/javascripts/discourse/app/lib/plugin-api.js
index fcc0a94..7094c0a 100644
--- a/app/assets/javascripts/discourse/app/lib/plugin-api.js
+++ b/app/assets/javascripts/discourse/app/lib/plugin-api.js
@@ -90,6 +90,8 @@ import {
   addSearchSuggestion,
 } from "discourse/widgets/search-menu-results";
 import { CUSTOM_USER_SEARCH_OPTIONS } from "select-kit/components/user-chooser";
+import { unsubscribeFromNotifications } from "discourse/initializers/subscribe-user-notifications";
+import { disableDefaultBadging } from "discourse/initializers/badging";
 
 // If you add any methods to the API ensure you bump up this number
 const PLUGIN_API_VERSION = "0.12.6";
@@ -1495,6 +1497,14 @@ class PluginApi {
       { ignoreMissing: true }
     );
   }
+  // Stops the subscribe-user-notifications initializer from listening to notifications
+  unsubscribeFromNotifications() {
+    unsubscribeFromNotifications();
+  }
+  // Stops the badging initializer from updating the PWA badge with the user's notification count
+  disableDefaultBadging() {
+    disableDefaultBadging();
+  }
 
   /**
    * Support for customizing the composer text. By providing a callback. Callbacks should

GitHub sha: e1d317566827bab78a1a34da8172834e31f5b7c1

This commit appears in #14595 which was approved by eviltrout. It was merged by markvanlan.