From 1f2390cf39d1e7ebbee4a15b04fab5ecea58da52 Mon Sep 17 00:00:00 2001 From: Erudition Date: Sun, 17 Sep 2023 23:24:03 -0500 Subject: [PATCH] Get service worker working with Capacitor bridge --- .../github/erudition/Minder/MainActivity.java | 35 ++++++++++++++++++- vite.config.ts | 6 ++-- www/scripts/capacitor/notifications.ts | 8 ++--- www/{sw.js => sw-unused.js} | 0 4 files changed, 42 insertions(+), 7 deletions(-) rename www/{sw.js => sw-unused.js} (100%) diff --git a/android/app/src/main/java/io/github/erudition/Minder/MainActivity.java b/android/app/src/main/java/io/github/erudition/Minder/MainActivity.java index 6e225081..6591ffcd 100644 --- a/android/app/src/main/java/io/github/erudition/Minder/MainActivity.java +++ b/android/app/src/main/java/io/github/erudition/Minder/MainActivity.java @@ -2,4 +2,37 @@ import com.getcapacitor.BridgeActivity; -public class MainActivity extends BridgeActivity {} +// C: added these imports for below onCreate +import android.os.Bundle; +import android.util.Log; +import android.webkit.ServiceWorkerClient; +import android.webkit.ServiceWorkerController; +import android.webkit.WebResourceRequest; +import android.webkit.WebResourceResponse; +import android.webkit.WebSettings; +import android.webkit.WebView; +import android.webkit.WebViewClient; + +public class MainActivity extends BridgeActivity { + + // https://github.com/ionic-team/capacitor/issues/5278#issuecomment-1653869040 + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) { + ServiceWorkerController swController = null; + swController = ServiceWorkerController.getInstance(); + + swController.setServiceWorkerClient(new ServiceWorkerClient() { + @Override + public WebResourceResponse shouldInterceptRequest(WebResourceRequest request) { + if (request.getUrl().toString().contains(".js")) { + request.getRequestHeaders().put("Accept", "text/html"); + } + return bridge.getLocalServer().shouldInterceptRequest(request); + } + }); + } + } +} diff --git a/vite.config.ts b/vite.config.ts index 46389a75..2dc276b0 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -11,9 +11,11 @@ export default defineConfig({ VitePWA({ registerType: 'autoUpdate', //devOptions: {enabled: true}, includeAssets: ['favicon.ico', 'apple-touch-icon.png', 'masked-icon.svg'], - workbox: { - globPatterns: ['**/*.{js,css,html,ico,png,svg}'] + workbox: { + navigateFallbackDenylist: [/.js/], + globPatterns: ['**/*.{css,ico,png,svg}'] // TODO removed js so capacitor plugins can work }, + outDir: "../dist", // weird it's not default, it looks for webapp files to cache here manifest: { name: 'Minder Prototype', short_name: 'Minder', diff --git a/www/scripts/capacitor/notifications.ts b/www/scripts/capacitor/notifications.ts index fcceeeb3..2923bd47 100644 --- a/www/scripts/capacitor/notifications.ts +++ b/www/scripts/capacitor/notifications.ts @@ -81,10 +81,10 @@ function setupChannel(notif) { { id: notif["channel"], name: notif["channel"], - description: notif["channelDescription"], - sound: notif["sound"], - importance: notif["priority"], - lightColor: notif["notificationLed"], + description: notif["channelDescription"] ? notif["channelDescription"] : null, + sound: notif["sound"] ? notif["sound"] : null, + importance: notif["priority"] ? notif["priority"] : null, + lightColor: notif["notificationLed"] ? notif["notificationLed"] : null, // vibration?: boolean; TODO not pattern } ); diff --git a/www/sw.js b/www/sw-unused.js similarity index 100% rename from www/sw.js rename to www/sw-unused.js