@stream-io/video-react-native-sdk 1.28.1 → 1.29.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (172) hide show
  1. package/android/src/main/AndroidManifest.xml +8 -1
  2. package/android/src/main/AndroidManifestNew.xml +11 -0
  3. package/android/src/main/java/com/streamvideo/reactnative/StreamVideoReactNativeModule.kt +42 -5
  4. package/android/src/main/java/com/streamvideo/reactnative/keepalive/KeepAliveNotification.kt +83 -0
  5. package/android/src/main/java/com/streamvideo/reactnative/keepalive/StreamCallKeepAliveHeadlessService.kt +134 -0
  6. package/dist/commonjs/hooks/push/useCallingExpWithCallingStateEffect.js +235 -0
  7. package/dist/commonjs/hooks/push/useCallingExpWithCallingStateEffect.js.map +1 -0
  8. package/dist/commonjs/hooks/push/useIosVoipPushEventsSetupEffect.js +18 -31
  9. package/dist/commonjs/hooks/push/useIosVoipPushEventsSetupEffect.js.map +1 -1
  10. package/dist/commonjs/hooks/useAndroidKeepCallAliveEffect.js +60 -97
  11. package/dist/commonjs/hooks/useAndroidKeepCallAliveEffect.js.map +1 -1
  12. package/dist/commonjs/index.js +1 -0
  13. package/dist/commonjs/index.js.map +1 -1
  14. package/dist/commonjs/modules/call-manager/CallManager.js +20 -0
  15. package/dist/commonjs/modules/call-manager/CallManager.js.map +1 -1
  16. package/dist/commonjs/providers/StreamCall/index.js +6 -6
  17. package/dist/commonjs/providers/StreamCall/index.js.map +1 -1
  18. package/dist/commonjs/utils/StreamVideoRN/index.js +33 -21
  19. package/dist/commonjs/utils/StreamVideoRN/index.js.map +1 -1
  20. package/dist/commonjs/utils/internal/registerSDKGlobals.js +47 -3
  21. package/dist/commonjs/utils/internal/registerSDKGlobals.js.map +1 -1
  22. package/dist/commonjs/utils/keepCallAliveHeadlessTask.js +48 -0
  23. package/dist/commonjs/utils/keepCallAliveHeadlessTask.js.map +1 -0
  24. package/dist/commonjs/utils/push/android.js +145 -200
  25. package/dist/commonjs/utils/push/android.js.map +1 -1
  26. package/dist/commonjs/utils/push/internal/ios.js +16 -34
  27. package/dist/commonjs/utils/push/internal/ios.js.map +1 -1
  28. package/dist/commonjs/utils/push/internal/rxSubjects.js +1 -20
  29. package/dist/commonjs/utils/push/internal/rxSubjects.js.map +1 -1
  30. package/dist/commonjs/utils/push/internal/utils.js +17 -1
  31. package/dist/commonjs/utils/push/internal/utils.js.map +1 -1
  32. package/dist/commonjs/utils/push/ios.js.map +1 -1
  33. package/dist/commonjs/utils/push/libs/callingx.js +75 -0
  34. package/dist/commonjs/utils/push/libs/callingx.js.map +1 -0
  35. package/dist/commonjs/utils/push/libs/index.js +8 -19
  36. package/dist/commonjs/utils/push/libs/index.js.map +1 -1
  37. package/dist/commonjs/utils/push/libs/notifee/index.js +0 -19
  38. package/dist/commonjs/utils/push/libs/notifee/index.js.map +1 -1
  39. package/dist/commonjs/utils/push/setupCallingExpEvents.js +75 -0
  40. package/dist/commonjs/utils/push/setupCallingExpEvents.js.map +1 -0
  41. package/dist/commonjs/utils/push/setupIosVoipPushEvents.js +7 -6
  42. package/dist/commonjs/utils/push/setupIosVoipPushEvents.js.map +1 -1
  43. package/dist/module/hooks/push/useCallingExpWithCallingStateEffect.js +228 -0
  44. package/dist/module/hooks/push/useCallingExpWithCallingStateEffect.js.map +1 -0
  45. package/dist/module/hooks/push/useIosVoipPushEventsSetupEffect.js +18 -31
  46. package/dist/module/hooks/push/useIosVoipPushEventsSetupEffect.js.map +1 -1
  47. package/dist/module/hooks/useAndroidKeepCallAliveEffect.js +62 -99
  48. package/dist/module/hooks/useAndroidKeepCallAliveEffect.js.map +1 -1
  49. package/dist/module/index.js +1 -0
  50. package/dist/module/index.js.map +1 -1
  51. package/dist/module/modules/call-manager/CallManager.js +20 -0
  52. package/dist/module/modules/call-manager/CallManager.js.map +1 -1
  53. package/dist/module/providers/StreamCall/index.js +6 -6
  54. package/dist/module/providers/StreamCall/index.js.map +1 -1
  55. package/dist/module/utils/StreamVideoRN/index.js +33 -21
  56. package/dist/module/utils/StreamVideoRN/index.js.map +1 -1
  57. package/dist/module/utils/internal/registerSDKGlobals.js +48 -4
  58. package/dist/module/utils/internal/registerSDKGlobals.js.map +1 -1
  59. package/dist/module/utils/keepCallAliveHeadlessTask.js +42 -0
  60. package/dist/module/utils/keepCallAliveHeadlessTask.js.map +1 -0
  61. package/dist/module/utils/push/android.js +147 -202
  62. package/dist/module/utils/push/android.js.map +1 -1
  63. package/dist/module/utils/push/internal/ios.js +16 -34
  64. package/dist/module/utils/push/internal/ios.js.map +1 -1
  65. package/dist/module/utils/push/internal/rxSubjects.js +0 -19
  66. package/dist/module/utils/push/internal/rxSubjects.js.map +1 -1
  67. package/dist/module/utils/push/internal/utils.js +14 -0
  68. package/dist/module/utils/push/internal/utils.js.map +1 -1
  69. package/dist/module/utils/push/ios.js.map +1 -1
  70. package/dist/module/utils/push/libs/callingx.js +67 -0
  71. package/dist/module/utils/push/libs/callingx.js.map +1 -0
  72. package/dist/module/utils/push/libs/index.js +1 -2
  73. package/dist/module/utils/push/libs/index.js.map +1 -1
  74. package/dist/module/utils/push/libs/notifee/index.js +0 -18
  75. package/dist/module/utils/push/libs/notifee/index.js.map +1 -1
  76. package/dist/module/utils/push/setupCallingExpEvents.js +69 -0
  77. package/dist/module/utils/push/setupCallingExpEvents.js.map +1 -0
  78. package/dist/module/utils/push/setupIosVoipPushEvents.js +7 -6
  79. package/dist/module/utils/push/setupIosVoipPushEvents.js.map +1 -1
  80. package/dist/typescript/hooks/push/useCallingExpWithCallingStateEffect.d.ts +5 -0
  81. package/dist/typescript/hooks/push/useCallingExpWithCallingStateEffect.d.ts.map +1 -0
  82. package/dist/typescript/hooks/push/useIosVoipPushEventsSetupEffect.d.ts.map +1 -1
  83. package/dist/typescript/hooks/useAndroidKeepCallAliveEffect.d.ts.map +1 -1
  84. package/dist/typescript/index.d.ts +1 -0
  85. package/dist/typescript/index.d.ts.map +1 -1
  86. package/dist/typescript/modules/call-manager/CallManager.d.ts.map +1 -1
  87. package/dist/typescript/utils/StreamVideoRN/index.d.ts +20 -2
  88. package/dist/typescript/utils/StreamVideoRN/index.d.ts.map +1 -1
  89. package/dist/typescript/utils/StreamVideoRN/types.d.ts +54 -29
  90. package/dist/typescript/utils/StreamVideoRN/types.d.ts.map +1 -1
  91. package/dist/typescript/utils/internal/registerSDKGlobals.d.ts.map +1 -1
  92. package/dist/typescript/utils/keepCallAliveHeadlessTask.d.ts +10 -0
  93. package/dist/typescript/utils/keepCallAliveHeadlessTask.d.ts.map +1 -0
  94. package/dist/typescript/utils/push/android.d.ts +1 -2
  95. package/dist/typescript/utils/push/android.d.ts.map +1 -1
  96. package/dist/typescript/utils/push/internal/ios.d.ts.map +1 -1
  97. package/dist/typescript/utils/push/internal/rxSubjects.d.ts +0 -12
  98. package/dist/typescript/utils/push/internal/rxSubjects.d.ts.map +1 -1
  99. package/dist/typescript/utils/push/internal/utils.d.ts +4 -0
  100. package/dist/typescript/utils/push/internal/utils.d.ts.map +1 -1
  101. package/dist/typescript/utils/push/ios.d.ts +1 -2
  102. package/dist/typescript/utils/push/ios.d.ts.map +1 -1
  103. package/dist/typescript/utils/push/libs/callingx.d.ts +9 -0
  104. package/dist/typescript/utils/push/libs/callingx.d.ts.map +1 -0
  105. package/dist/typescript/utils/push/libs/firebaseMessaging/index.d.ts +16 -2
  106. package/dist/typescript/utils/push/libs/firebaseMessaging/index.d.ts.map +1 -1
  107. package/dist/typescript/utils/push/libs/index.d.ts +1 -2
  108. package/dist/typescript/utils/push/libs/index.d.ts.map +1 -1
  109. package/dist/typescript/utils/push/libs/notifee/index.d.ts +0 -1
  110. package/dist/typescript/utils/push/libs/notifee/index.d.ts.map +1 -1
  111. package/dist/typescript/utils/push/setupCallingExpEvents.d.ts +8 -0
  112. package/dist/typescript/utils/push/setupCallingExpEvents.d.ts.map +1 -0
  113. package/dist/typescript/utils/push/setupIosVoipPushEvents.d.ts.map +1 -1
  114. package/expo-config-plugin/dist/withAndroidManifest.js +1 -33
  115. package/expo-config-plugin/dist/withAndroidPermissions.js +2 -7
  116. package/expo-config-plugin/dist/withAppDelegate.js +19 -197
  117. package/expo-config-plugin/dist/withMainActivity.js +1 -1
  118. package/expo-config-plugin/dist/withiOSInfoPlist.js +2 -3
  119. package/ios/StreamInCallManager.swift +4 -0
  120. package/ios/StreamVideoReactNative.h +7 -4
  121. package/ios/StreamVideoReactNative.m +191 -82
  122. package/package.json +10 -15
  123. package/src/hooks/push/useCallingExpWithCallingStateEffect.ts +361 -0
  124. package/src/hooks/push/useIosVoipPushEventsSetupEffect.ts +21 -34
  125. package/src/hooks/useAndroidKeepCallAliveEffect.ts +95 -120
  126. package/src/index.ts +1 -0
  127. package/src/modules/call-manager/CallManager.ts +30 -0
  128. package/src/providers/StreamCall/index.tsx +6 -6
  129. package/src/utils/StreamVideoRN/index.ts +40 -30
  130. package/src/utils/StreamVideoRN/types.ts +56 -29
  131. package/src/utils/internal/registerSDKGlobals.ts +42 -4
  132. package/src/utils/keepCallAliveHeadlessTask.ts +54 -0
  133. package/src/utils/push/android.ts +223 -308
  134. package/src/utils/push/internal/ios.ts +25 -46
  135. package/src/utils/push/internal/rxSubjects.ts +0 -29
  136. package/src/utils/push/internal/utils.ts +25 -0
  137. package/src/utils/push/ios.ts +1 -6
  138. package/src/utils/push/libs/callingx.ts +90 -0
  139. package/src/utils/push/libs/index.ts +1 -2
  140. package/src/utils/push/libs/notifee/index.ts +0 -27
  141. package/src/utils/push/setupCallingExpEvents.ts +100 -0
  142. package/src/utils/push/setupIosVoipPushEvents.ts +11 -7
  143. package/CHANGELOG.md +0 -3089
  144. package/android/src/main/java/com/streamvideo/reactnative/util/CallAliveServiceChecker.kt +0 -95
  145. package/dist/commonjs/hooks/push/useIosCallkeepWithCallingStateEffect.js +0 -160
  146. package/dist/commonjs/hooks/push/useIosCallkeepWithCallingStateEffect.js.map +0 -1
  147. package/dist/commonjs/utils/push/libs/callkeep.js +0 -17
  148. package/dist/commonjs/utils/push/libs/callkeep.js.map +0 -1
  149. package/dist/commonjs/utils/push/libs/voipPushNotification.js +0 -17
  150. package/dist/commonjs/utils/push/libs/voipPushNotification.js.map +0 -1
  151. package/dist/commonjs/utils/push/setupIosCallKeepEvents.js +0 -205
  152. package/dist/commonjs/utils/push/setupIosCallKeepEvents.js.map +0 -1
  153. package/dist/module/hooks/push/useIosCallkeepWithCallingStateEffect.js +0 -153
  154. package/dist/module/hooks/push/useIosCallkeepWithCallingStateEffect.js.map +0 -1
  155. package/dist/module/utils/push/libs/callkeep.js +0 -11
  156. package/dist/module/utils/push/libs/callkeep.js.map +0 -1
  157. package/dist/module/utils/push/libs/voipPushNotification.js +0 -11
  158. package/dist/module/utils/push/libs/voipPushNotification.js.map +0 -1
  159. package/dist/module/utils/push/setupIosCallKeepEvents.js +0 -199
  160. package/dist/module/utils/push/setupIosCallKeepEvents.js.map +0 -1
  161. package/dist/typescript/hooks/push/useIosCallkeepWithCallingStateEffect.d.ts +0 -5
  162. package/dist/typescript/hooks/push/useIosCallkeepWithCallingStateEffect.d.ts.map +0 -1
  163. package/dist/typescript/utils/push/libs/callkeep.d.ts +0 -3
  164. package/dist/typescript/utils/push/libs/callkeep.d.ts.map +0 -1
  165. package/dist/typescript/utils/push/libs/voipPushNotification.d.ts +0 -3
  166. package/dist/typescript/utils/push/libs/voipPushNotification.d.ts.map +0 -1
  167. package/dist/typescript/utils/push/setupIosCallKeepEvents.d.ts +0 -6
  168. package/dist/typescript/utils/push/setupIosCallKeepEvents.d.ts.map +0 -1
  169. package/src/hooks/push/useIosCallkeepWithCallingStateEffect.ts +0 -235
  170. package/src/utils/push/libs/callkeep.ts +0 -16
  171. package/src/utils/push/libs/voipPushNotification.ts +0 -17
  172. package/src/utils/push/setupIosCallKeepEvents.ts +0 -252
@@ -1,5 +1,4 @@
1
1
  import {
2
- Call,
3
2
  CallingState,
4
3
  StreamVideoClient,
5
4
  videoLoggerSystem,
@@ -15,30 +14,15 @@ import {
15
14
  getExpoNotificationsLibNoThrow,
16
15
  getFirebaseMessagingLib,
17
16
  getFirebaseMessagingLibNoThrow,
18
- getIncomingCallForegroundServiceTypes,
19
17
  getNotifeeLibThrowIfNotInstalledForPush,
20
18
  type NotifeeLib,
21
19
  } from './libs';
22
- import {
23
- pushAcceptedIncomingCallCId$,
24
- pushAndroidBackgroundDeliveredIncomingCallCId$,
25
- pushNonRingingCallData$,
26
- pushRejectedIncomingCallCId$,
27
- pushTappedIncomingCallCId$,
28
- } from './internal/rxSubjects';
20
+ import { pushNonRingingCallData$ } from './internal/rxSubjects';
29
21
  import { pushUnsubscriptionCallbacks } from './internal/constants';
30
- import {
31
- canAddPushWSSubscriptionsRef,
32
- clearPushWSEventSubscriptions,
33
- processCallFromPushInBackground,
34
- shouldCallBeEnded,
35
- } from './internal/utils';
22
+ import { canListenToWS, shouldCallBeClosed } from './internal/utils';
36
23
  import { setPushLogoutCallback } from '../internal/pushLogoutCallback';
37
- import { getAndroidDefaultRingtoneUrl } from '../getAndroidDefaultRingtoneUrl';
38
24
  import { StreamVideoRN } from '../StreamVideoRN';
39
-
40
- const ACCEPT_CALL_ACTION_ID = 'accept';
41
- const DECLINE_CALL_ACTION_ID = 'decline';
25
+ import { getCallingxLib } from './libs/callingx';
42
26
 
43
27
  type PushConfig = NonNullable<StreamVideoConfig['push']>;
44
28
 
@@ -107,11 +91,10 @@ export async function initAndroidPushToken(
107
91
  await setDeviceToken(token);
108
92
  }
109
93
  }
110
- // TODO: remove the incomingCallChannel check and find a better way once we have telecom integration for android
111
- const messaging =
112
- pushConfig.isExpo && !pushConfig.android.incomingCallChannel
113
- ? getFirebaseMessagingLibNoThrow(true)
114
- : getFirebaseMessagingLib();
94
+
95
+ const messaging = pushConfig.isExpo
96
+ ? getFirebaseMessagingLibNoThrow(true)
97
+ : getFirebaseMessagingLib();
115
98
  if (messaging) {
116
99
  logger.debug(`setting firebase token listeners`);
117
100
  const unsubscribe = messaging().onTokenRefresh((refreshedToken) =>
@@ -127,10 +110,10 @@ export async function initAndroidPushToken(
127
110
  * Creates notification from the push message data.
128
111
  * For Ringing and Non-Ringing calls.
129
112
  */
113
+
130
114
  export const firebaseDataHandler = async (
131
115
  data: FirebaseMessagingTypes.RemoteMessage['data'],
132
116
  ) => {
133
- if (Platform.OS !== 'android') return;
134
117
  /* Example data from firebase
135
118
  "message": {
136
119
  "data": {
@@ -146,227 +129,245 @@ export const firebaseDataHandler = async (
146
129
  // other stuff
147
130
  }
148
131
  */
132
+ if (Platform.OS !== 'android') return;
133
+
134
+ const logger = videoLoggerSystem.getLogger('firebaseDataHandler');
149
135
  const pushConfig = StreamVideoRN.getConfig().push;
150
136
  if (!pushConfig || !data || data.sender !== 'stream.video') {
151
137
  return;
152
138
  }
153
- const notifeeLib = getNotifeeLibThrowIfNotInstalledForPush();
154
- const notifee = notifeeLib.default;
155
- const settings = await notifee.getNotificationSettings();
156
- if (settings.authorizationStatus !== 1) {
157
- const logger = videoLoggerSystem.getLogger('firebaseDataHandler');
158
- logger.debug(
159
- `Notification permission not granted, unable to post ${data.type} notifications`,
160
- );
161
- return;
162
- }
163
139
 
164
140
  if (data.type === 'call.ring') {
165
141
  const call_cid = data.call_cid as string;
166
- const created_by_id = data.created_by_id as string;
167
- const receiver_id = data.receiver_id as string;
142
+ if (!call_cid) {
143
+ logger.debug(
144
+ `call_cid is not provided, skipping the call.ring notification`,
145
+ );
146
+ return;
147
+ }
168
148
 
169
- const video_client = await pushConfig.createStreamVideoClient();
170
- await video_client?.onRingingCall(call_cid);
149
+ const callingx = getCallingxLib();
171
150
 
172
- const shouldCallBeClosed = (callToCheck: Call) => {
173
- const { mustEndCall } = shouldCallBeEnded(
174
- callToCheck,
175
- created_by_id,
176
- receiver_id,
151
+ await callingx.checkPermissions();
152
+ if (!callingx.isNotificationsAllowed) {
153
+ logger.debug(
154
+ `Notification permission not granted, unable to post ${data.type} notifications`,
177
155
  );
178
- return mustEndCall;
179
- };
156
+ return;
157
+ }
158
+
159
+ const client = await pushConfig.createStreamVideoClient();
160
+ if (!client) {
161
+ logger.debug(
162
+ `video client not found, skipping the call.ring notification`,
163
+ );
164
+ return;
165
+ }
166
+
167
+ const shouldRejectCallWhenBusy = client['rejectCallWhenBusy'] ?? false;
168
+ if (callingx.hasRegisteredCall() && shouldRejectCallWhenBusy) {
169
+ logger.debug(
170
+ `registered call found, skipping the call.ring notification`,
171
+ );
172
+ return;
173
+ }
180
174
 
181
- const canListenToWS = () =>
182
- canAddPushWSSubscriptionsRef.current &&
183
- AppState.currentState !== 'active';
184
175
  const asForegroundService = canListenToWS();
185
176
 
177
+ const callerName = data.created_by_display_name as string;
178
+ const hasVideo = data.video === 'true';
179
+
186
180
  if (asForegroundService) {
187
181
  // Listen to call events from WS through fg service
188
182
  // note: this will replace the current empty fg service runner
189
- notifee.registerForegroundService(() => {
190
- return new Promise(async () => {
191
- const client = await pushConfig.createStreamVideoClient();
192
- if (!client) {
193
- videoLoggerSystem
194
- .getLogger('firebaseMessagingOnMessageHandler')
195
- .debug(
196
- `Closing fg service as there is no client to create from push config`,
197
- );
198
- notifee.stopForegroundService();
199
- return;
200
- }
201
- const callFromPush = await client.onRingingCall(call_cid);
202
- let _shouldCallBeClosed = shouldCallBeClosed(callFromPush);
203
- if (_shouldCallBeClosed) {
204
- videoLoggerSystem
205
- .getLogger('firebaseMessagingOnMessageHandler')
206
- .debug(
207
- `Closing fg service callCid: ${call_cid} shouldCallBeClosed: ${_shouldCallBeClosed}`,
208
- );
209
- notifee.stopForegroundService();
210
- return;
211
- }
212
- const unsubscribeFunctions: Array<() => void> = [];
213
- // check if service needs to be closed if accept/decline event was done on another device
214
- const unsubscribe = callFromPush.on('all', (event) => {
215
- const _canListenToWS = canListenToWS();
216
- if (!_canListenToWS) {
217
- videoLoggerSystem
218
- .getLogger('firebaseMessagingOnMessageHandler')
219
- .debug(
220
- `Closing fg service from event callCid: ${call_cid} canListenToWS: ${_canListenToWS}`,
221
- { event },
183
+ //we need to start service (e.g. by calling display incoming call) and than launch bg task, consider making those steps independent
184
+ callingx.registerBackgroundTask((_: unknown, stopTask: () => void) => {
185
+ return new Promise((resolve) => {
186
+ const finishBackgroundTask = () => {
187
+ callingx.log(
188
+ `Finishing background task for callCid: ${call_cid}`,
189
+ 'debug',
190
+ );
191
+ resolve(undefined);
192
+ stopTask();
193
+ };
194
+
195
+ (async () => {
196
+ try {
197
+ const _client = await pushConfig.createStreamVideoClient();
198
+ if (!_client) {
199
+ logger.debug(
200
+ `Closing fg service as there is no client to create from push config`,
222
201
  );
223
- unsubscribeFunctions.forEach((fn) => fn());
224
- notifee.stopForegroundService();
225
- return;
226
- }
227
- _shouldCallBeClosed = shouldCallBeClosed(callFromPush);
228
- if (_shouldCallBeClosed) {
229
- videoLoggerSystem
230
- .getLogger('firebaseMessagingOnMessageHandler')
231
- .debug(
232
- `Closing fg service from event callCid: ${call_cid} canListenToWS: ${_canListenToWS} shouldCallBeClosed: ${_shouldCallBeClosed}`,
233
- { event },
202
+ finishBackgroundTask();
203
+ return;
204
+ }
205
+
206
+ const callFromPush = await _client.onRingingCall(call_cid);
207
+ if (shouldCallBeClosed(callFromPush, data)) {
208
+ logger.debug(
209
+ `Closing fg service callCid: ${call_cid} shouldCallBeClosed`,
234
210
  );
235
- unsubscribeFunctions.forEach((fn) => fn());
236
- notifee.stopForegroundService();
237
- }
238
- });
239
- // check if service needs to be closed if call was left
240
- const subscription = callFromPush.state.callingState$.subscribe(
241
- (callingState) => {
242
- if (
243
- callingState === CallingState.IDLE ||
244
- callingState === CallingState.LEFT
245
- ) {
246
- videoLoggerSystem
247
- .getLogger('firebaseMessagingOnMessageHandler')
248
- .debug(
249
- `Closing fg service from callingState callCid: ${call_cid} callingState: ${callingState}`,
250
- );
251
- unsubscribeFunctions.forEach((fn) => fn());
252
- notifee.stopForegroundService();
211
+
212
+ finishBackgroundTask();
213
+ callingx.log(
214
+ `Ending call with callCid: ${call_cid} shouldCallBeClosed`,
215
+ 'debug',
216
+ );
217
+ //TODO: think about sending appropriate reason for end call
218
+ callingx.endCallWithReason(call_cid, 'remote');
219
+ return;
253
220
  }
254
- },
255
- );
256
- unsubscribeFunctions.push(unsubscribe);
257
- unsubscribeFunctions.push(() => subscription.unsubscribe());
258
- pushUnsubscriptionCallbacks.get(call_cid)?.forEach((cb) => cb());
259
- pushUnsubscriptionCallbacks.set(call_cid, unsubscribeFunctions);
221
+
222
+ const unsubscribeFunctions: Array<() => void> = [];
223
+ // check if service needs to be closed if accept/decline event was done on another device
224
+ const unsubscribe = callFromPush.on('all', (event) => {
225
+ const _canListenToWS = canListenToWS();
226
+ if (!_canListenToWS) {
227
+ logger.debug(
228
+ `Closing fg service from event callCid: ${call_cid} canListenToWS: ${_canListenToWS}`,
229
+ { event },
230
+ );
231
+ unsubscribeFunctions.forEach((fn) => fn());
232
+
233
+ finishBackgroundTask();
234
+ return;
235
+ }
236
+
237
+ if (shouldCallBeClosed(callFromPush, data)) {
238
+ logger.debug(
239
+ `Closing fg service from event callCid: ${call_cid} canListenToWS: ${_canListenToWS} shouldCallBeClosed`,
240
+ { event },
241
+ );
242
+ unsubscribeFunctions.forEach((fn) => fn());
243
+
244
+ finishBackgroundTask();
245
+ //TODO: think about sending appropriate reason for end call
246
+ callingx.endCallWithReason(call_cid, 'rejected');
247
+ }
248
+ });
249
+
250
+ // check if service needs to be closed if call was left
251
+ const stateSubscription =
252
+ callFromPush.state.callingState$.subscribe((callingState) => {
253
+ if (
254
+ callingState === CallingState.IDLE ||
255
+ callingState === CallingState.LEFT
256
+ ) {
257
+ logger.debug(
258
+ `Closing fg service from callingState callCid: ${call_cid} callingState: ${callingState}`,
259
+ );
260
+ unsubscribeFunctions.forEach((fn) => fn());
261
+
262
+ //TODO: think about sending appropriate reason for end call
263
+ callingx.log(
264
+ `Ending call with callCid: ${call_cid} callingState: ${callingState}`,
265
+ 'debug',
266
+ );
267
+ finishBackgroundTask();
268
+ callingx.endCallWithReason(call_cid, 'remote');
269
+ }
270
+ });
271
+
272
+ const endCallSubscription = callingx.addEventListener(
273
+ 'endCall',
274
+ async ({ callId }: { callId: string }) => {
275
+ unsubscribeFunctions.forEach((fn) => fn());
276
+ try {
277
+ await callFromPush.leave({
278
+ reject: true,
279
+ reason: 'decline',
280
+ });
281
+ } catch (error) {
282
+ logger.error(
283
+ `Failed to leave call with callCid: ${call_cid} error: ${error}`,
284
+ );
285
+ } finally {
286
+ callingx.log(
287
+ `Ending call with callCid: ${call_cid} callId: ${callId}`,
288
+ 'debug',
289
+ );
290
+ finishBackgroundTask();
291
+ callingx.endCallWithReason(callId, 'rejected');
292
+ }
293
+ },
294
+ );
295
+
296
+ //stop background task when app comes to foreground
297
+ const appStateSubscription = AppState.addEventListener(
298
+ 'change',
299
+ (nextAppState) => {
300
+ const _canListenToWS = canListenToWS();
301
+ callingx.log(
302
+ `AppState changed to: ${nextAppState} for callCid: ${call_cid} canListenToWS: ${_canListenToWS}`,
303
+ 'debug',
304
+ );
305
+ if (!_canListenToWS) {
306
+ unsubscribeFunctions.forEach((fn) => fn());
307
+ finishBackgroundTask();
308
+ return;
309
+ }
310
+ },
311
+ );
312
+
313
+ unsubscribeFunctions.push(unsubscribe);
314
+ unsubscribeFunctions.push(() => stateSubscription.unsubscribe());
315
+ unsubscribeFunctions.push(() => endCallSubscription.remove());
316
+ unsubscribeFunctions.push(() => appStateSubscription.remove());
317
+ pushUnsubscriptionCallbacks.get(call_cid)?.forEach((cb) => cb());
318
+ pushUnsubscriptionCallbacks.set(call_cid, unsubscribeFunctions);
319
+ } catch (error) {
320
+ callingx.log(
321
+ `Failed to start background task with callCid: ${call_cid} error: ${error}`,
322
+ 'error',
323
+ );
324
+ finishBackgroundTask();
325
+ }
326
+ })();
260
327
  });
261
328
  });
262
329
  }
263
- const incomingCallChannel = pushConfig.android.incomingCallChannel;
264
- const incomingCallNotificationTextGetters =
265
- pushConfig.android.incomingCallNotificationTextGetters;
266
- if (!incomingCallChannel || !incomingCallNotificationTextGetters) {
267
- const logger = videoLoggerSystem.getLogger(
268
- 'firebaseMessagingOnMessageHandler',
269
- );
270
- logger.error(
271
- "Can't show incoming call notification as either or both incomingCallChannel and incomingCallNotificationTextGetters were not provided",
272
- );
273
- return;
274
- }
275
- /*
276
- * Sound has to be set on channel level for android 8 and above and cant be updated later after creation!
277
- * For android 7 and below, sound should be set on notification level
278
- */
279
- // set default ringtone if not provided
280
- if (!incomingCallChannel.sound) {
281
- incomingCallChannel.sound = await getAndroidDefaultRingtoneUrl();
282
- }
283
- await notifee.createChannel(incomingCallChannel);
284
- const { getTitle, getBody, getAcceptButtonTitle, getDeclineButtonTitle } =
285
- incomingCallNotificationTextGetters;
286
- const createdUserName = data.created_by_display_name as string;
287
-
288
- const title = getTitle(createdUserName);
289
- const body = getBody(createdUserName);
290
330
 
291
- videoLoggerSystem
292
- .getLogger('firebaseMessagingOnMessageHandler')
293
- .debug(
294
- `Displaying incoming call notification with callCid: ${call_cid} title: ${title} body: ${body} asForegroundService: ${asForegroundService}`,
295
- );
296
-
297
- const channelId = incomingCallChannel.id;
298
- await notifee.displayNotification({
299
- id: call_cid,
300
- title: getTitle(createdUserName),
301
- body: getBody(createdUserName),
302
- data,
303
- android: {
304
- channelId,
305
- smallIcon: pushConfig.android.smallIcon,
306
- importance: 4, // high importance
307
- foregroundServiceTypes: getIncomingCallForegroundServiceTypes(),
308
- asForegroundService,
309
- ongoing: true,
310
- sound: incomingCallChannel.sound,
311
- vibrationPattern: incomingCallChannel.vibrationPattern,
312
- loopSound: true,
313
- pressAction: {
314
- id: 'default',
315
- launchActivity: 'default', // open the app when the notification is pressed
316
- },
317
- actions: [
318
- {
319
- title: getDeclineButtonTitle?.() ?? 'Decline',
320
- pressAction: {
321
- id: DECLINE_CALL_ACTION_ID,
322
- },
323
- },
324
- {
325
- title: getAcceptButtonTitle?.() ?? 'Accept',
326
- pressAction: {
327
- id: ACCEPT_CALL_ACTION_ID,
328
- launchActivity: 'default', // open the app when the notification is pressed
329
- },
330
- },
331
- ],
332
- category: notifeeLib.AndroidCategory.CALL,
333
- fullScreenAction: {
334
- id: 'stream_ringing_incoming_call',
335
- },
336
- timeoutAfter: 60000, // 60 seconds, after which the notification will be dismissed automatically
337
- },
338
- });
331
+ await callingx.displayIncomingCall(
332
+ call_cid,
333
+ call_cid,
334
+ callerName,
335
+ hasVideo,
336
+ );
337
+ logger.debug(
338
+ `Displaying incoming call notification with callCid: ${call_cid} asForegroundService: ${asForegroundService}`,
339
+ );
339
340
 
340
341
  if (asForegroundService) {
341
342
  // no need to check if call has be closed as that will be handled by the fg service
342
343
  return;
343
344
  }
344
345
 
345
- // check if call needs to be closed if accept/decline event was done
346
- // before the notification was shown
347
- const client = await pushConfig.createStreamVideoClient();
348
- if (!client) {
349
- return;
350
- }
351
346
  const callFromPush = await client.onRingingCall(call_cid);
352
347
 
353
- if (shouldCallBeClosed(callFromPush)) {
354
- videoLoggerSystem
355
- .getLogger('firebaseMessagingOnMessageHandler')
356
- .debug(
357
- `Removing incoming call notification immediately with callCid: ${call_cid} as it should be closed`,
358
- );
359
- notifee.cancelDisplayedNotification(call_cid);
348
+ if (shouldCallBeClosed(callFromPush, data)) {
349
+ logger.debug(
350
+ `Removing incoming call notification immediately with callCid: ${call_cid} as it should be closed`,
351
+ );
352
+ //TODO: think about sending appropriate reason for end call
353
+ callingx.endCallWithReason(call_cid, 'remote');
360
354
  }
361
355
  } else {
356
+ const notifeeLib = getNotifeeLibThrowIfNotInstalledForPush();
357
+ const notifee = notifeeLib.default;
358
+ const settings = await notifee.getNotificationSettings();
359
+ if (settings.authorizationStatus !== 1) {
360
+ logger.debug(
361
+ `Notification permission not granted, unable to post ${data.type} notifications`,
362
+ );
363
+ return;
364
+ }
365
+
362
366
  // the other types are call.live_started and call.notification
363
367
  const callChannel = pushConfig.android.callChannel;
364
368
  const callNotificationTextGetters =
365
369
  pushConfig.android.callNotificationTextGetters;
366
370
  if (!callChannel || !callNotificationTextGetters) {
367
- const logger = videoLoggerSystem.getLogger(
368
- 'firebaseMessagingOnMessageHandler',
369
- );
370
371
  logger.debug(
371
372
  "Can't show call notification as either or both callChannel and callNotificationTextGetters is not provided",
372
373
  );
@@ -382,11 +383,9 @@ export const firebaseDataHandler = async (
382
383
  const title = getTitle(type, createdUserName);
383
384
  const body = getBody(type, createdUserName);
384
385
 
385
- videoLoggerSystem
386
- .getLogger('firebaseMessagingOnMessageHandler')
387
- .debug(
388
- `Displaying NonRingingPushEvent ${type} notification with title: ${title} body: ${body}`,
389
- );
386
+ logger.debug(
387
+ `Displaying NonRingingPushEvent ${type} notification with title: ${title} body: ${body}`,
388
+ );
390
389
  await notifee.displayNotification({
391
390
  title: getTitle(type, createdUserName),
392
391
  body: getBody(type, createdUserName),
@@ -409,16 +408,10 @@ export const firebaseDataHandler = async (
409
408
  }
410
409
  };
411
410
 
412
- export const onAndroidNotifeeEvent = async ({
413
- event,
414
- isBackground,
415
- }: {
416
- event: Event;
417
- isBackground: boolean;
418
- }) => {
411
+ export const onAndroidNotifeeEvent = async ({ event }: { event: Event }) => {
419
412
  if (Platform.OS !== 'android') return;
420
413
  const { type, detail } = event;
421
- const { notification, pressAction } = detail;
414
+ const { notification } = detail;
422
415
  const notificationId = notification?.id;
423
416
  const data = notification?.data;
424
417
  const pushConfig = StreamVideoRN.getConfig().push;
@@ -434,92 +427,14 @@ export const onAndroidNotifeeEvent = async ({
434
427
  // we can safely cast to string because the data is from "stream.video"
435
428
  const call_cid = data.call_cid as string;
436
429
 
437
- if (data.type === 'call.ring') {
438
- // check if we have observers for the call cid (this means the app is in the foreground state)
439
- const hasObservers =
440
- pushAcceptedIncomingCallCId$.observed &&
441
- pushRejectedIncomingCallCId$.observed;
442
-
443
- const notifeeLib = getNotifeeLibThrowIfNotInstalledForPush();
444
- const notifee = notifeeLib.default;
445
- // Check if we need to decline the call
446
- const didPressDecline =
447
- type === notifeeLib.EventType.ACTION_PRESS &&
448
- pressAction?.id === DECLINE_CALL_ACTION_ID;
449
- const didDismiss = type === notifeeLib.EventType.DISMISSED;
450
- const mustDecline = didPressDecline || didDismiss;
451
- // Check if we need to accept the call
452
- const mustAccept =
453
- type === notifeeLib.EventType.ACTION_PRESS &&
454
- pressAction?.id === ACCEPT_CALL_ACTION_ID;
455
-
456
- if (
457
- mustAccept ||
458
- mustDecline ||
459
- type === notifeeLib.EventType.ACTION_PRESS
460
- ) {
461
- videoLoggerSystem
462
- .getLogger('onAndroidNotifeeEvent')
463
- .debug(
464
- `clearPushWSEventSubscriptions for callCId: ${call_cid} mustAccept: ${mustAccept} mustDecline: ${mustDecline}`,
465
- );
466
- clearPushWSEventSubscriptions(call_cid);
467
- notifee.stopForegroundService();
468
- }
469
-
470
- if (mustAccept) {
471
- videoLoggerSystem
472
- .getLogger('onAndroidNotifeeEvent')
473
- .debug(`pushAcceptedIncomingCallCId$ added with callCId: ${call_cid}`);
474
- pushAcceptedIncomingCallCId$.next(call_cid);
475
- // NOTE: accept will be handled by the app with rxjs observers as the app will go to foreground always
476
- } else if (mustDecline) {
477
- videoLoggerSystem
478
- .getLogger('onAndroidNotifeeEvent')
479
- .debug(`pushRejectedIncomingCallCId$ added with callCId: ${call_cid}`);
480
- pushRejectedIncomingCallCId$.next(call_cid);
481
- if (hasObservers) {
482
- // if we had observers we can return here as the observers will handle the call as the app is in the foreground state
483
- videoLoggerSystem
484
- .getLogger('onAndroidNotifeeEvent')
485
- .debug(
486
- `Skipped processCallFromPushInBackground for Declining call with callCId: ${call_cid} as the app is in the foreground state`,
487
- );
488
- return;
489
- }
490
- videoLoggerSystem
491
- .getLogger('onAndroidNotifeeEvent')
492
- .debug(
493
- `start processCallFromPushInBackground - Declining call with callCId: ${call_cid}`,
494
- );
495
- await processCallFromPushInBackground(pushConfig, call_cid, 'decline');
496
- } else {
497
- if (type === notifeeLib.EventType.PRESS) {
498
- videoLoggerSystem
499
- .getLogger('onAndroidNotifeeEvent')
500
- .debug(`pushTappedIncomingCallCId$ added with callCId: ${call_cid}`);
501
- pushTappedIncomingCallCId$.next(call_cid);
502
- // pressed state will be handled by the app with rxjs observers as the app will go to foreground always
503
- } else if (isBackground && type === notifeeLib.EventType.DELIVERED) {
504
- videoLoggerSystem
505
- .getLogger('onAndroidNotifeeEvent')
506
- .debug(
507
- `pushAndroidBackgroundDeliveredIncomingCallCId$ added with callCId: ${call_cid}`,
508
- );
509
- pushAndroidBackgroundDeliveredIncomingCallCId$.next(call_cid);
510
- // background delivered state will be handled by the app with rxjs observers as processing needs to happen only when app is opened
511
- }
512
- }
513
- } else {
514
- const notifeeLib = getNotifeeLibThrowIfNotInstalledForPush();
515
- if (type === notifeeLib.EventType.PRESS) {
516
- videoLoggerSystem
517
- .getLogger('onAndroidNotifeeEvent')
518
- .debug(`onTapNonRingingCallNotification with callCId: ${call_cid}`);
519
- pushConfig.onTapNonRingingCallNotification?.(
520
- call_cid,
521
- data.type as NonRingingPushEvent,
522
- );
523
- }
430
+ const notifeeLib = getNotifeeLibThrowIfNotInstalledForPush();
431
+ if (type === notifeeLib.EventType.PRESS) {
432
+ videoLoggerSystem
433
+ .getLogger('onAndroidNotifeeEvent')
434
+ .debug(`onTapNonRingingCallNotification with callCId: ${call_cid}`);
435
+ pushConfig.onTapNonRingingCallNotification?.(
436
+ call_cid,
437
+ data.type as NonRingingPushEvent,
438
+ );
524
439
  }
525
440
  };