mirror of
https://github.com/CompassConnections/Compass.git
synced 2026-04-03 06:23:22 -04:00
Enhance deep link handling for Android with pending link consumption and JavaScript interface
This commit is contained in:
@@ -11,7 +11,7 @@ android {
|
||||
applicationId "com.compassconnections.app"
|
||||
minSdkVersion rootProject.ext.minSdkVersion
|
||||
targetSdkVersion rootProject.ext.targetSdkVersion
|
||||
versionCode 76
|
||||
versionCode 77
|
||||
versionName "1.15.0"
|
||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||
aaptOptions {
|
||||
|
||||
@@ -82,6 +82,13 @@ public class MainActivity extends BridgeActivity implements ModifiedMainActivity
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@JavascriptInterface
|
||||
public String getPendingDeepLink() {
|
||||
String link = pendingDeepLink;
|
||||
pendingDeepLink = null; // consume it
|
||||
return link;
|
||||
}
|
||||
|
||||
@JavascriptInterface
|
||||
public void downloadFile(String filename, String content) {
|
||||
try {
|
||||
@@ -202,29 +209,35 @@ public class MainActivity extends BridgeActivity implements ModifiedMainActivity
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
Log.i("CompassApp", "onCreate called");
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
WebView webView = this.bridge.getWebView();
|
||||
webView.setWebViewClient(new BridgeWebViewClient(this.bridge) {
|
||||
@Override
|
||||
public void onPageFinished(WebView view, String url) {
|
||||
super.onPageFinished(view, url);
|
||||
if (pendingDeepLink != null) {
|
||||
handleDeepLink(pendingDeepLink);
|
||||
pendingDeepLink = null;
|
||||
}
|
||||
}
|
||||
});
|
||||
webView.setWebViewClient(new BridgeWebViewClient(this.bridge));
|
||||
|
||||
// WebView.setWebContentsDebuggingEnabled(true);
|
||||
|
||||
// Set a recognizable User-Agent (always reliable)
|
||||
WebSettings settings = webView.getSettings();
|
||||
settings.setUserAgentString(settings.getUserAgentString() + " CompassAppWebView");
|
||||
|
||||
settings.setJavaScriptEnabled(true);
|
||||
webView.addJavascriptInterface(new WebAppInterface(this), "AndroidBridge");
|
||||
|
||||
registerPlugin(PushNotificationsPlugin.class);
|
||||
// Initialize the Bridge with Push Notifications plugin
|
||||
// this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{
|
||||
// add(com.getcapacitor.plugin.PushNotifications.class);
|
||||
// }});
|
||||
|
||||
askNotificationPermission();
|
||||
|
||||
appUpdateManager = AppUpdateManagerFactory.create(this);
|
||||
checkForUpdates();
|
||||
|
||||
Uri data = getIntent().getData();
|
||||
if (data != null) pendingDeepLink = data.toString();
|
||||
|
||||
if (pendingDeepLink != null) {
|
||||
handleDeepLink(pendingDeepLink);
|
||||
pendingDeepLink = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void handleDeepLink(String url) {
|
||||
|
||||
@@ -54,7 +54,7 @@ export const LOCAL_WEB_DOMAIN = `localhost:3000`
|
||||
export const LOCAL_BACKEND_DOMAIN = `${IS_WEBVIEW_DEV_PHONE ? '192.168.1.3' : IS_LOCAL_ANDROID ? '10.0.2.2' : 'localhost'}:8088`
|
||||
|
||||
export const DOMAIN = IS_LOCAL ? LOCAL_WEB_DOMAIN : ENV_CONFIG.domain
|
||||
export const DEPLOYED_WEB_URL = `https://www.${ENV_CONFIG.domain}`
|
||||
export const DEPLOYED_WEB_URL = `https://${ENV_CONFIG.domain}`
|
||||
export const WEB_URL = IS_LOCAL ? `http://${LOCAL_WEB_DOMAIN}` : `https://${DOMAIN}`
|
||||
export const BACKEND_DOMAIN = IS_LOCAL ? LOCAL_BACKEND_DOMAIN : ENV_CONFIG.backendDomain
|
||||
export const FIREBASE_CONFIG = ENV_CONFIG.firebaseConfig
|
||||
|
||||
@@ -150,6 +150,7 @@ function MyApp(props: AppProps<PageProps>) {
|
||||
}, [router])
|
||||
|
||||
useEffect(() => {
|
||||
if (!Capacitor.isNativePlatform()) return
|
||||
const handleAppLink = (payload: any) => {
|
||||
debug('handleAppLink', payload)
|
||||
const {endpoint} = payload
|
||||
@@ -158,6 +159,11 @@ function MyApp(props: AppProps<PageProps>) {
|
||||
}
|
||||
}
|
||||
;(window as any).handleAppLink = handleAppLink
|
||||
|
||||
const link = window.AndroidBridge?.getPendingDeepLink?.()
|
||||
if (link) {
|
||||
handleAppLink({url: link, endpoint: new URL(link).pathname})
|
||||
}
|
||||
}, [])
|
||||
|
||||
const title = 'Compass'
|
||||
|
||||
@@ -8,6 +8,7 @@ declare global {
|
||||
interface Window {
|
||||
AndroidBridge?: {
|
||||
downloadFile: (filename: string, content: string) => void
|
||||
getPendingDeepLink: () => string | null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user