Skip to content

Commit e40a352

Browse files
committed
When clicking on the DM notif, go to the DM chat
1 parent 70267f0 commit e40a352

File tree

3 files changed

+83
-32
lines changed

3 files changed

+83
-32
lines changed

android/app/src/main/java/com/compassconnections/app/MainActivity.java

Lines changed: 59 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,53 @@
2020
import com.getcapacitor.Plugin;
2121
import com.getcapacitor.PluginHandle;
2222

23+
import org.json.JSONException;
24+
import org.json.JSONObject;
25+
2326
import ee.forgr.capacitor.social.login.GoogleProvider;
2427
import ee.forgr.capacitor.social.login.ModifiedMainActivityForSocialLoginPlugin;
2528
import ee.forgr.capacitor.social.login.SocialLoginPlugin;
2629

2730

31+
//import android.app.NotificationChannel;
32+
//import android.app.NotificationManager;
33+
//import android.os.Build;
34+
//import com.google.firebase.messaging.RemoteMessage;
35+
//import com.capacitorjs.plugins.pushnotifications.MessagingService;
36+
37+
//public class MyMessagingService extends MessagingService {
38+
//
39+
// @Override
40+
// public void onMessageReceived(RemoteMessage remoteMessage) {
41+
// // TODO(developer): Handle FCM messages here.
42+
// // Not getting messages here? See why this may be: https://goo.gl/39bRNJ
43+
// Log.d(TAG, "From: " + remoteMessage.getFrom());
44+
//
45+
// // Check if message contains a data payload.
46+
// if (remoteMessage.getData().size() > 0) {
47+
// Log.d(TAG, "Message data payload: " + remoteMessage.getData());
48+
//
49+
// if (/* Check if data needs to be processed by long running job */ true) {
50+
// // For long-running tasks (10 seconds or more) use WorkManager.
51+
// scheduleJob();
52+
// } else {
53+
// // Handle message within 10 seconds
54+
// handleNow();
55+
// }
56+
//
57+
// }
58+
//
59+
// // Check if message contains a notification payload.
60+
// if (remoteMessage.getNotification() != null) {
61+
// Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
62+
// }
63+
//
64+
// // Also if you intend on generating your own notifications as a result of a received FCM
65+
// // message, here is where that should be initiated. See sendNotification method below.
66+
// }
67+
//}
68+
69+
2870
public class MainActivity extends BridgeActivity implements ModifiedMainActivityForSocialLoginPlugin {
2971

3072
// Declare this at class level
@@ -60,20 +102,21 @@ public boolean isNativeApp() {
60102
protected void onNewIntent(Intent intent) {
61103
super.onNewIntent(intent);
62104

63-
String data = intent.getDataString();
64-
Log.i("CompassApp", "onNewIntent called with data: " + data);
65-
// if (data != null && data.startsWith("com.compassmeet://auth")) {
66-
// Log.i("CompassApp", "triggerWindowJSEvent oauthRedirect");
67-
// try {
68-
// String payload = new JSONObject().put("data", data).toString();
69-
// Log.i("CompassApp", "Payload: " + payload);
70-
// bridge.getWebView().post(() -> bridge.getWebView().evaluateJavascript("oauthRedirect(" + payload + ");", null));
71-
// } catch (JSONException e) {
72-
// Log.i("CompassApp", "Failed to encode JSON payload", e);
73-
// }
74-
// } else {
75-
// Log.i("CompassApp", "No relevant data");
76-
// }
105+
// String data = intent.getDataString();
106+
String endpoint = intent.getStringExtra("endpoint");
107+
Log.i("CompassApp", "onNewIntent called with endpoint: " + endpoint);
108+
if (endpoint != null) {
109+
Log.i("CompassApp", "redirecting to endpoint: " + endpoint);
110+
try {
111+
String payload = new JSONObject().put("endpoint", endpoint).toString();
112+
Log.i("CompassApp", "Payload: " + payload);
113+
bridge.getWebView().post(() -> bridge.getWebView().evaluateJavascript("bridgeRedirect(" + payload + ");", null));
114+
} catch (JSONException e) {
115+
Log.i("CompassApp", "Failed to encode JSON payload", e);
116+
}
117+
} else {
118+
Log.i("CompassApp", "No relevant data");
119+
}
77120
}
78121

79122
@Override
@@ -124,6 +167,7 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
124167

125168
// This function will never be called, leave it empty
126169
@Override
127-
public void IHaveModifiedTheMainActivityForTheUseWithSocialLoginPlugin() {}
170+
public void IHaveModifiedTheMainActivityForTheUseWithSocialLoginPlugin() {
171+
}
128172
}
129173

backend/api/src/helpers/private-messages.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import webPush from 'web-push'
1717
import {parseJsonContentToText} from "common/util/parse"
1818
import {encryptMessage} from "shared/encryption"
1919
import * as admin from 'firebase-admin'
20+
import {TokenMessage} from "firebase-admin/lib/messaging/messaging-api";
2021

2122
dayjs.extend(utc)
2223
dayjs.extend(timezone)
@@ -317,22 +318,23 @@ export async function sendPushToToken(
317318
token: string,
318319
payload: PushPayload,
319320
) {
320-
const message = {
321+
const message: TokenMessage = {
321322
token,
322-
notification: {
323-
title: payload.title,
324-
body: payload.body,
325-
// data: {
326-
// url: payload.url,
327-
// },
323+
android: {
324+
notification: {
325+
title: payload.title,
326+
body: payload.body,
327+
},
328+
},
329+
data: {
330+
endpoint: payload.url,
328331
},
329-
data: payload.data, // optional custom key-value pairs
330332
}
331-
// Fine to create at each call, as it's a cached singleton
332-
const fcm = admin.messaging()
333333

334334
try {
335-
console.log('Sending notification to:', token, payload)
335+
// Fine to create at each call, as it's a cached singleton
336+
const fcm = admin.messaging()
337+
console.log('Sending notification to:', token, message)
336338
const response = await fcm.send(message)
337339
console.log('Push sent successfully:', response)
338340
return response
@@ -341,11 +343,11 @@ export async function sendPushToToken(
341343
if (err instanceof Error && 'code' in err) {
342344
const firebaseError = err as { code: string; message: string }
343345
console.warn('Firebase error:', firebaseError.code, firebaseError.message)
344-
346+
345347
// Handle specific error cases here if needed
346348
// For example, if token is no longer valid:
347349
if (firebaseError.code === 'messaging/registration-token-not-registered' ||
348-
firebaseError.code === 'messaging/invalid-argument') {
350+
firebaseError.code === 'messaging/invalid-argument') {
349351
console.warn('Removing invalid FCM token')
350352
await removeMobileSubscription(pg, token, userId)
351353
}

web/pages/_app.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,15 @@ function MyApp({Component, pageProps}: AppProps<PageProps>) {
104104
return () => window.removeEventListener('appBackButton', handleBack)
105105
}, [router])
106106

107-
// useEffect(() => {
108-
// // Expose globally for native bridge
109-
// (window as any).oauthRedirect = oauthRedirect
110-
// }, [])
107+
useEffect(() => {
108+
const bridgeRedirect = (payload: any) => {
109+
console.log('bridgeRedirect', payload)
110+
const {endpoint} = payload
111+
router.push(endpoint)
112+
}
113+
// Expose globally for native bridge
114+
(window as any).bridgeRedirect = bridgeRedirect
115+
}, [])
111116

112117
const title = 'Compass'
113118
const description = 'The platform for intentional connections'

0 commit comments

Comments
 (0)