diff --git a/android/app/src/main/java/com/compassconnections/app/MainActivity.java b/android/app/src/main/java/com/compassconnections/app/MainActivity.java index cdbd9c92..ae04999e 100644 --- a/android/app/src/main/java/com/compassconnections/app/MainActivity.java +++ b/android/app/src/main/java/com/compassconnections/app/MainActivity.java @@ -20,11 +20,53 @@ import com.getcapacitor.BridgeWebViewClient; import com.getcapacitor.Plugin; import com.getcapacitor.PluginHandle; +import org.json.JSONException; +import org.json.JSONObject; + import ee.forgr.capacitor.social.login.GoogleProvider; import ee.forgr.capacitor.social.login.ModifiedMainActivityForSocialLoginPlugin; import ee.forgr.capacitor.social.login.SocialLoginPlugin; +//import android.app.NotificationChannel; +//import android.app.NotificationManager; +//import android.os.Build; +//import com.google.firebase.messaging.RemoteMessage; +//import com.capacitorjs.plugins.pushnotifications.MessagingService; + +//public class MyMessagingService extends MessagingService { +// +// @Override +// public void onMessageReceived(RemoteMessage remoteMessage) { +// // TODO(developer): Handle FCM messages here. +// // Not getting messages here? See why this may be: https://goo.gl/39bRNJ +// Log.d(TAG, "From: " + remoteMessage.getFrom()); +// +// // Check if message contains a data payload. +// if (remoteMessage.getData().size() > 0) { +// Log.d(TAG, "Message data payload: " + remoteMessage.getData()); +// +// if (/* Check if data needs to be processed by long running job */ true) { +// // For long-running tasks (10 seconds or more) use WorkManager. +// scheduleJob(); +// } else { +// // Handle message within 10 seconds +// handleNow(); +// } +// +// } +// +// // Check if message contains a notification payload. +// if (remoteMessage.getNotification() != null) { +// Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody()); +// } +// +// // Also if you intend on generating your own notifications as a result of a received FCM +// // message, here is where that should be initiated. See sendNotification method below. +// } +//} + + public class MainActivity extends BridgeActivity implements ModifiedMainActivityForSocialLoginPlugin { // Declare this at class level @@ -60,20 +102,21 @@ public class MainActivity extends BridgeActivity implements ModifiedMainActivity protected void onNewIntent(Intent intent) { super.onNewIntent(intent); - String data = intent.getDataString(); - Log.i("CompassApp", "onNewIntent called with data: " + data); -// if (data != null && data.startsWith("com.compassmeet://auth")) { -// Log.i("CompassApp", "triggerWindowJSEvent oauthRedirect"); -// try { -// String payload = new JSONObject().put("data", data).toString(); -// Log.i("CompassApp", "Payload: " + payload); -// bridge.getWebView().post(() -> bridge.getWebView().evaluateJavascript("oauthRedirect(" + payload + ");", null)); -// } catch (JSONException e) { -// Log.i("CompassApp", "Failed to encode JSON payload", e); -// } -// } else { -// Log.i("CompassApp", "No relevant data"); -// } +// String data = intent.getDataString(); + String endpoint = intent.getStringExtra("endpoint"); + Log.i("CompassApp", "onNewIntent called with endpoint: " + endpoint); + if (endpoint != null) { + Log.i("CompassApp", "redirecting to endpoint: " + endpoint); + try { + String payload = new JSONObject().put("endpoint", endpoint).toString(); + Log.i("CompassApp", "Payload: " + payload); + bridge.getWebView().post(() -> bridge.getWebView().evaluateJavascript("bridgeRedirect(" + payload + ");", null)); + } catch (JSONException e) { + Log.i("CompassApp", "Failed to encode JSON payload", e); + } + } else { + Log.i("CompassApp", "No relevant data"); + } } @Override @@ -124,6 +167,7 @@ public class MainActivity extends BridgeActivity implements ModifiedMainActivity // This function will never be called, leave it empty @Override - public void IHaveModifiedTheMainActivityForTheUseWithSocialLoginPlugin() {} + public void IHaveModifiedTheMainActivityForTheUseWithSocialLoginPlugin() { + } } diff --git a/backend/api/src/helpers/private-messages.ts b/backend/api/src/helpers/private-messages.ts index d734b82f..9c925110 100644 --- a/backend/api/src/helpers/private-messages.ts +++ b/backend/api/src/helpers/private-messages.ts @@ -17,6 +17,7 @@ import webPush from 'web-push' import {parseJsonContentToText} from "common/util/parse" import {encryptMessage} from "shared/encryption" import * as admin from 'firebase-admin' +import {TokenMessage} from "firebase-admin/lib/messaging/messaging-api"; dayjs.extend(utc) dayjs.extend(timezone) @@ -317,22 +318,23 @@ export async function sendPushToToken( token: string, payload: PushPayload, ) { - const message = { + const message: TokenMessage = { token, - notification: { - title: payload.title, - body: payload.body, - // data: { - // url: payload.url, - // }, + android: { + notification: { + title: payload.title, + body: payload.body, + }, + }, + data: { + endpoint: payload.url, }, - data: payload.data, // optional custom key-value pairs } - // Fine to create at each call, as it's a cached singleton - const fcm = admin.messaging() try { - console.log('Sending notification to:', token, payload) + // Fine to create at each call, as it's a cached singleton + const fcm = admin.messaging() + console.log('Sending notification to:', token, message) const response = await fcm.send(message) console.log('Push sent successfully:', response) return response @@ -341,11 +343,11 @@ export async function sendPushToToken( if (err instanceof Error && 'code' in err) { const firebaseError = err as { code: string; message: string } console.warn('Firebase error:', firebaseError.code, firebaseError.message) - + // Handle specific error cases here if needed // For example, if token is no longer valid: if (firebaseError.code === 'messaging/registration-token-not-registered' || - firebaseError.code === 'messaging/invalid-argument') { + firebaseError.code === 'messaging/invalid-argument') { console.warn('Removing invalid FCM token') await removeMobileSubscription(pg, token, userId) } diff --git a/web/pages/_app.tsx b/web/pages/_app.tsx index 5cd26e4b..55ee9290 100644 --- a/web/pages/_app.tsx +++ b/web/pages/_app.tsx @@ -104,10 +104,15 @@ function MyApp({Component, pageProps}: AppProps) { return () => window.removeEventListener('appBackButton', handleBack) }, [router]) - // useEffect(() => { - // // Expose globally for native bridge - // (window as any).oauthRedirect = oauthRedirect - // }, []) + useEffect(() => { + const bridgeRedirect = (payload: any) => { + console.log('bridgeRedirect', payload) + const {endpoint} = payload + router.push(endpoint) + } + // Expose globally for native bridge + (window as any).bridgeRedirect = bridgeRedirect + }, []) const title = 'Compass' const description = 'The platform for intentional connections'