|
@@ -1,7 +1,7 @@
|
|
|
import * as TaskManager from 'expo-task-manager';
|
|
|
import * as Notifications from 'expo-notifications';
|
|
|
import { Platform } from 'react-native';
|
|
|
-import { API_URL, APP_VERSION } from 'src/constants';
|
|
|
+import { API_URL, APP_VERSION, WEBSOCKET_URL } from 'src/constants';
|
|
|
import axios from 'axios';
|
|
|
import { API } from 'src/types';
|
|
|
import { storage, StoreType } from 'src/storage';
|
|
@@ -11,7 +11,6 @@ const BACKGROUND_NOTIFICATION_TASK = 'BACKGROUND-NOTIFICATION-TASK';
|
|
|
TaskManager.defineTask(
|
|
|
BACKGROUND_NOTIFICATION_TASK,
|
|
|
async ({ data, error }: { data?: any; error?: any }) => {
|
|
|
- console.log('Background notification task started', data);
|
|
|
if (error) {
|
|
|
return;
|
|
|
}
|
|
@@ -22,15 +21,9 @@ TaskManager.defineTask(
|
|
|
let messageId;
|
|
|
let fromUser;
|
|
|
|
|
|
- if (Platform.OS === 'ios') {
|
|
|
- groupToken = data?.UIApplicationLaunchOptionsRemoteNotificationKey?.data?.group_token;
|
|
|
- messageId = data?.UIApplicationLaunchOptionsRemoteNotificationKey?.data?.message_id;
|
|
|
- fromUser = data?.UIApplicationLaunchOptionsRemoteNotificationKey?.data?.from_user;
|
|
|
- } else {
|
|
|
- groupToken = data?.notification?.data?.group_token;
|
|
|
- messageId = data?.notification?.data?.message_id;
|
|
|
- fromUser = data?.notification?.data?.from_user;
|
|
|
- }
|
|
|
+ groupToken = data?.data?.group_token;
|
|
|
+ messageId = data?.data?.message_id;
|
|
|
+ fromUser = data?.data?.from_user;
|
|
|
|
|
|
if ((!groupToken && !fromUser) || !messageId) return;
|
|
|
|
|
@@ -66,6 +59,25 @@ TaskManager.defineTask(
|
|
|
}
|
|
|
);
|
|
|
}
|
|
|
+
|
|
|
+ const socket = new WebSocket(WEBSOCKET_URL);
|
|
|
+
|
|
|
+ socket.onopen = () => {
|
|
|
+ socket.send(JSON.stringify({ token }));
|
|
|
+ let data: any = { action: 'messages_received', messages_ids: [messageId] };
|
|
|
+ if (fromUser) {
|
|
|
+ data.conversation_with = fromUser;
|
|
|
+ } else {
|
|
|
+ data.conversation_with_group = groupToken;
|
|
|
+ }
|
|
|
+
|
|
|
+ socket.send(JSON.stringify(data));
|
|
|
+ socket.close();
|
|
|
+ };
|
|
|
+
|
|
|
+ socket.onerror = (error) => {
|
|
|
+ console.error('WebSocket error:', error);
|
|
|
+ };
|
|
|
} catch (err) {
|
|
|
console.error('Error sending notification data:', err);
|
|
|
}
|
|
@@ -76,7 +88,7 @@ TaskManager.defineTask(
|
|
|
export const registerBackgroundNotificationTask = async () => {
|
|
|
const isRegistered = await TaskManager.isTaskRegisteredAsync(BACKGROUND_NOTIFICATION_TASK);
|
|
|
|
|
|
- if (!isRegistered) {
|
|
|
+ if (!isRegistered && Platform.OS === 'ios') {
|
|
|
await Notifications.registerTaskAsync(BACKGROUND_NOTIFICATION_TASK);
|
|
|
}
|
|
|
};
|
|
@@ -88,3 +100,68 @@ export const unregisterBackgroundNotificationTask = async () => {
|
|
|
await TaskManager.unregisterTaskAsync(BACKGROUND_NOTIFICATION_TASK);
|
|
|
}
|
|
|
};
|
|
|
+
|
|
|
+export const handleNotificationData = async (
|
|
|
+ groupToken: string | undefined,
|
|
|
+ messageId: number,
|
|
|
+ fromUser: number | undefined
|
|
|
+) => {
|
|
|
+ const token = storage.get('token', StoreType.STRING);
|
|
|
+
|
|
|
+ if ((!groupToken && !fromUser) || !messageId) return;
|
|
|
+
|
|
|
+ try {
|
|
|
+ if (fromUser) {
|
|
|
+ await axios.postForm(
|
|
|
+ API_URL + '/' + API.MESSAGES_RECEIVED,
|
|
|
+ {
|
|
|
+ token,
|
|
|
+ from_user: fromUser,
|
|
|
+ messages_id: [messageId]
|
|
|
+ },
|
|
|
+ {
|
|
|
+ headers: {
|
|
|
+ Platform: Platform.OS,
|
|
|
+ 'App-Version': APP_VERSION
|
|
|
+ }
|
|
|
+ }
|
|
|
+ );
|
|
|
+ } else {
|
|
|
+ await axios.postForm(
|
|
|
+ API_URL + '/' + API.GROUP_MESSAGES_RECEIVED,
|
|
|
+ {
|
|
|
+ token,
|
|
|
+ group_token: groupToken,
|
|
|
+ messages_id: [messageId]
|
|
|
+ },
|
|
|
+ {
|
|
|
+ headers: {
|
|
|
+ Platform: Platform.OS,
|
|
|
+ 'App-Version': APP_VERSION
|
|
|
+ }
|
|
|
+ }
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ const socket = new WebSocket(WEBSOCKET_URL);
|
|
|
+
|
|
|
+ socket.onopen = () => {
|
|
|
+ socket.send(JSON.stringify({ token }));
|
|
|
+ let data: any = { action: 'messages_received', messages_ids: [messageId] };
|
|
|
+ if (fromUser) {
|
|
|
+ data.conversation_with = fromUser;
|
|
|
+ } else {
|
|
|
+ data.conversation_with_group = groupToken;
|
|
|
+ }
|
|
|
+
|
|
|
+ socket.send(JSON.stringify(data));
|
|
|
+ socket.close();
|
|
|
+ };
|
|
|
+
|
|
|
+ socket.onerror = (error) => {
|
|
|
+ console.error('WebSocket error:', error);
|
|
|
+ };
|
|
|
+ } catch (err) {
|
|
|
+ console.error('Error sending notification data:', err);
|
|
|
+ }
|
|
|
+};
|