Skip to content

Commit

Permalink
Get service worker working with Capacitor bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
Erudition committed Sep 18, 2023
1 parent 0f0d623 commit 1f2390c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
}
}
}
6 changes: 4 additions & 2 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
8 changes: 4 additions & 4 deletions www/scripts/capacitor/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
);
Expand Down
File renamed without changes.

0 comments on commit 1f2390c

Please sign in to comment.