pushNotificationTask.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import * as TaskManager from 'expo-task-manager';
  2. import * as Notifications from 'expo-notifications';
  3. import { Platform } from 'react-native';
  4. import { API_URL, APP_VERSION, WEBSOCKET_URL } from 'src/constants';
  5. import axios from 'axios';
  6. import { API } from 'src/types';
  7. import { storage, StoreType } from 'src/storage';
  8. const BACKGROUND_NOTIFICATION_TASK = 'BACKGROUND-NOTIFICATION-TASK';
  9. TaskManager.defineTask(
  10. BACKGROUND_NOTIFICATION_TASK,
  11. async ({ data, error }: { data?: any; error?: any }) => {
  12. if (error) {
  13. return;
  14. }
  15. if (data) {
  16. const token = storage.get('token', StoreType.STRING);
  17. let groupToken;
  18. let messageId;
  19. let fromUser;
  20. groupToken = data?.data?.group_token;
  21. messageId = data?.data?.message_id;
  22. fromUser = data?.data?.from_user;
  23. if ((!groupToken && !fromUser) || !messageId) return;
  24. try {
  25. if (fromUser) {
  26. await axios.postForm(
  27. API_URL + '/' + API.MESSAGES_RECEIVED,
  28. {
  29. token,
  30. from_user: fromUser,
  31. messages_id: [messageId]
  32. },
  33. {
  34. headers: {
  35. Platform: Platform.OS,
  36. 'App-Version': APP_VERSION
  37. }
  38. }
  39. );
  40. } else {
  41. await axios.postForm(
  42. API_URL + '/' + API.GROUP_MESSAGES_RECEIVED,
  43. {
  44. token,
  45. group_token: groupToken,
  46. messages_id: [messageId]
  47. },
  48. {
  49. headers: {
  50. Platform: Platform.OS,
  51. 'App-Version': APP_VERSION
  52. }
  53. }
  54. );
  55. }
  56. const socket = new WebSocket(WEBSOCKET_URL);
  57. socket.onopen = () => {
  58. socket.send(JSON.stringify({ token }));
  59. let data: any = { action: 'messages_received', messages_ids: [messageId] };
  60. if (fromUser) {
  61. data.conversation_with = fromUser;
  62. } else {
  63. data.conversation_with_group = groupToken;
  64. }
  65. socket.send(JSON.stringify(data));
  66. socket.close();
  67. };
  68. socket.onerror = (error) => {
  69. console.error('WebSocket error:', error);
  70. };
  71. } catch (err) {
  72. console.error('Error sending notification data:', err);
  73. }
  74. }
  75. }
  76. );
  77. export const registerBackgroundNotificationTask = async () => {
  78. try {
  79. const isRegistered = await TaskManager.isTaskRegisteredAsync(BACKGROUND_NOTIFICATION_TASK);
  80. if (!isRegistered && Platform.OS === 'ios') {
  81. await Notifications.registerTaskAsync(BACKGROUND_NOTIFICATION_TASK);
  82. }
  83. } catch (error) {
  84. console.warn('Background notification task registration failed:', error);
  85. }
  86. };
  87. export const unregisterBackgroundNotificationTask = async () => {
  88. try {
  89. const isRegistered = await TaskManager.isTaskRegisteredAsync(BACKGROUND_NOTIFICATION_TASK);
  90. if (isRegistered) {
  91. await TaskManager.unregisterTaskAsync(BACKGROUND_NOTIFICATION_TASK);
  92. }
  93. } catch (error) {
  94. console.warn('Background notification task unregistration failed:', error);
  95. }
  96. };
  97. export const handleNotificationData = async (
  98. groupToken: string | undefined,
  99. messageId: number,
  100. fromUser: number | undefined
  101. ) => {
  102. const token = storage.get('token', StoreType.STRING);
  103. if ((!groupToken && !fromUser) || !messageId) return;
  104. try {
  105. if (fromUser) {
  106. await axios.postForm(
  107. API_URL + '/' + API.MESSAGES_RECEIVED,
  108. {
  109. token,
  110. from_user: fromUser,
  111. messages_id: [messageId]
  112. },
  113. {
  114. headers: {
  115. Platform: Platform.OS,
  116. 'App-Version': APP_VERSION
  117. }
  118. }
  119. );
  120. } else {
  121. await axios.postForm(
  122. API_URL + '/' + API.GROUP_MESSAGES_RECEIVED,
  123. {
  124. token,
  125. group_token: groupToken,
  126. messages_id: [messageId]
  127. },
  128. {
  129. headers: {
  130. Platform: Platform.OS,
  131. 'App-Version': APP_VERSION
  132. }
  133. }
  134. );
  135. }
  136. const socket = new WebSocket(WEBSOCKET_URL);
  137. socket.onopen = () => {
  138. socket.send(JSON.stringify({ token }));
  139. let data: any = { action: 'messages_received', messages_ids: [messageId] };
  140. if (fromUser) {
  141. data.conversation_with = fromUser;
  142. } else {
  143. data.conversation_with_group = groupToken;
  144. }
  145. socket.send(JSON.stringify(data));
  146. socket.close();
  147. };
  148. socket.onerror = (error) => {
  149. console.error('WebSocket error:', error);
  150. };
  151. } catch (err) {
  152. console.error('Error sending notification data:', err);
  153. }
  154. };