react-native-sinch-calling 0.2.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 (40) hide show
  1. package/CHANGELOG.md +29 -0
  2. package/LICENSE +20 -0
  3. package/README.md +239 -0
  4. package/SinchCalling.podspec +28 -0
  5. package/android/build.gradle +52 -0
  6. package/android/src/main/AndroidManifest.xml +19 -0
  7. package/android/src/main/java/com/sinchcalling/SinchCallManager.kt +302 -0
  8. package/android/src/main/java/com/sinchcalling/SinchCallingModule.kt +184 -0
  9. package/android/src/main/java/com/sinchcalling/SinchCallingPackage.kt +31 -0
  10. package/android/src/main/java/com/sinchcalling/telecom/SinchConnection.kt +43 -0
  11. package/android/src/main/java/com/sinchcalling/telecom/SinchConnectionService.kt +37 -0
  12. package/android/src/main/java/com/sinchcalling/telecom/SinchTelecomManager.kt +106 -0
  13. package/ios/Sinch/SinchCallKitManager.swift +154 -0
  14. package/ios/Sinch/SinchCallManager.swift +195 -0
  15. package/ios/Sinch/SinchPushManager.swift +81 -0
  16. package/ios/SinchCalling.h +5 -0
  17. package/ios/SinchCalling.mm +242 -0
  18. package/lib/module/NativeSinchCalling.js +5 -0
  19. package/lib/module/NativeSinchCalling.js.map +1 -0
  20. package/lib/module/SinchCalling.js +185 -0
  21. package/lib/module/SinchCalling.js.map +1 -0
  22. package/lib/module/index.js +4 -0
  23. package/lib/module/index.js.map +1 -0
  24. package/lib/module/package.json +1 -0
  25. package/lib/module/types.js +4 -0
  26. package/lib/module/types.js.map +1 -0
  27. package/lib/typescript/package.json +1 -0
  28. package/lib/typescript/src/NativeSinchCalling.d.ts +69 -0
  29. package/lib/typescript/src/NativeSinchCalling.d.ts.map +1 -0
  30. package/lib/typescript/src/SinchCalling.d.ts +98 -0
  31. package/lib/typescript/src/SinchCalling.d.ts.map +1 -0
  32. package/lib/typescript/src/index.d.ts +4 -0
  33. package/lib/typescript/src/index.d.ts.map +1 -0
  34. package/lib/typescript/src/types.d.ts +9 -0
  35. package/lib/typescript/src/types.d.ts.map +1 -0
  36. package/package.json +179 -0
  37. package/src/NativeSinchCalling.ts +148 -0
  38. package/src/SinchCalling.ts +245 -0
  39. package/src/index.tsx +11 -0
  40. package/src/types.ts +9 -0
@@ -0,0 +1,148 @@
1
+ import { TurboModuleRegistry, type TurboModule } from 'react-native';
2
+ import type { CodegenTypes } from 'react-native';
3
+
4
+ export interface ClientStartFailedEvent {
5
+ message: string;
6
+ code: number;
7
+ }
8
+
9
+ export interface IncomingCallEvent {
10
+ callId: string;
11
+ remoteUserId: string;
12
+ }
13
+
14
+ export interface CallEvent {
15
+ callId: string;
16
+ }
17
+
18
+ export interface CallEndedEvent {
19
+ callId: string;
20
+ endCause: string;
21
+ }
22
+
23
+ export interface VoipPushTokenEvent {
24
+ token: string;
25
+ }
26
+
27
+ export interface CallUIAnsweredEvent {
28
+ callId: string;
29
+ }
30
+
31
+ export interface CallUIDeclinedEvent {
32
+ callId: string;
33
+ }
34
+
35
+ export interface IncomingCallUIEvent {
36
+ callId: string;
37
+ displayName: string;
38
+ }
39
+
40
+ export interface Spec extends TurboModule {
41
+ configure(appKey: string, environmentHost: string, userId: string): void;
42
+ start(): void;
43
+ stop(): void;
44
+
45
+ provideRegistrationCredentials(jwt: string): void;
46
+ failRegistration(message: string): void;
47
+
48
+ callUser(userId: string): string;
49
+ // PSTN call. `callerId` is the caller-ID number shown to the recipient
50
+ // (Android only — the iOS SDK derives it from the application's
51
+ // provisioned voice number and ignores this argument).
52
+ callPhoneNumber(phoneNumber: string, callerId: string): string;
53
+ // Joins a Sinch conference room by id (e.g. a backend-bridged PSTN leg).
54
+ // `callerId` is Android-only, same caveat as `callPhoneNumber`.
55
+ callConference(conferenceId: string, callerId: string): string;
56
+ answerCall(callId: string): void;
57
+ hangupCall(callId: string): void;
58
+ setMuted(muted: boolean): void;
59
+ setSpeakerEnabled(enabled: boolean): void;
60
+ // Sends a DTMF tone on an active call. `key` must be one of [0-9, #, *, A-D].
61
+ // Returns false if the call doesn't exist or the tone couldn't be sent.
62
+ sendDTMF(callId: string, key: string): boolean;
63
+
64
+ // Android only. Must be called before `configure()`. No-op on iOS, which
65
+ // uses SINManagedPush (see `enablePushNotifications`) instead of a
66
+ // caller-supplied token.
67
+ registerFcmPush(senderId: string, token: string): void;
68
+
69
+ // iOS only. Sets up SINManagedPush (VoIP push via PushKit) so incoming
70
+ // calls can be relayed while the app is backgrounded. No-op on Android,
71
+ // which relies on `registerFcmPush` + `relayRemotePushNotification`.
72
+ enablePushNotifications(useProductionAps: boolean): void;
73
+
74
+ // Android only. Forward the raw FCM data payload here (e.g. from
75
+ // `messaging().onMessage()` / `setBackgroundMessageHandler()`). By
76
+ // default every payload is treated as a Sinch-relayed push and turned
77
+ // into a normal `onIncomingCall` event. If `configureCustomIncomingCallPush`
78
+ // has been called, payloads containing both configured fields instead call
79
+ // `reportIncomingCallUI` directly and skip the Sinch relay for that push.
80
+ // No-op on iOS, which applies the same logic internally via PushKit.
81
+ relayRemotePushNotification(payload: Object): void;
82
+
83
+ // Opts into recognizing your own backend's incoming-call push payloads.
84
+ // `idField`/`displayField` are the keys your payload uses for the call id
85
+ // and the caller's display name/number — e.g. `configureCustomIncomingCallPush("callId", "callerNumber")`
86
+ // for a payload shaped `{ callId, callerNumber }`. Until this is called,
87
+ // `relayRemotePushNotification` and the iOS VoIP push handler always
88
+ // relay to Sinch (the library's default, backend-agnostic behavior).
89
+ configureCustomIncomingCallPush(idField: string, displayField: string): void;
90
+
91
+ // Reports a system call UI (CallKit / Telecom) for a call your own app
92
+ // logic decided to show — from a custom push, a Socket.IO event, or
93
+ // anything else. Not tied to any particular transport: call this directly
94
+ // whenever you want to ring, independent of `configureCustomIncomingCallPush`.
95
+ reportIncomingCallUI(callId: string, displayName: string): void;
96
+
97
+ // Turns a call shown via `reportIncomingCallUI` (or a configured custom
98
+ // push) into a real call once you know which Sinch conference to join:
99
+ // joins the conference and re-associates the already-visible system call
100
+ // UI with the resulting real callId. Returns the real callId, or "" on
101
+ // failure.
102
+ resolveCallUIToConference(
103
+ callId: string,
104
+ conferenceId: string,
105
+ callerId: string
106
+ ): string;
107
+
108
+ // Cleans up a system call UI reported via `reportIncomingCallUI` that
109
+ // couldn't be resolved into a real call (e.g. your backend rejected it).
110
+ dismissCallUI(callId: string): void;
111
+
112
+ // Overrides the caller name shown on the system call UI (e.g. once a
113
+ // caller-ID lookup resolves) without disturbing the phone number already
114
+ // reported as the handle.
115
+ updateIncomingCallDisplayName(callId: string, displayName: string): void;
116
+
117
+ onClientStarted: CodegenTypes.EventEmitter<void>;
118
+ onClientStartFailed: CodegenTypes.EventEmitter<ClientStartFailedEvent>;
119
+ onRegistrationCredentialsRequired: CodegenTypes.EventEmitter<void>;
120
+
121
+ onIncomingCall: CodegenTypes.EventEmitter<IncomingCallEvent>;
122
+ onCallProgressing: CodegenTypes.EventEmitter<CallEvent>;
123
+ onCallEstablished: CodegenTypes.EventEmitter<CallEvent>;
124
+ onCallEnded: CodegenTypes.EventEmitter<CallEndedEvent>;
125
+
126
+ onPushTokenRegistered: CodegenTypes.EventEmitter<void>;
127
+ onPushTokenRegistrationFailed: CodegenTypes.EventEmitter<ClientStartFailedEvent>;
128
+
129
+ // iOS only: fires with the hex-encoded PushKit VoIP token whenever it's
130
+ // issued or rotated. Forward this to your backend so it can address VoIP
131
+ // pushes to this device.
132
+ onVoipPushTokenUpdated: CodegenTypes.EventEmitter<VoipPushTokenEvent>;
133
+
134
+ // A system call UI was reported — either because you called
135
+ // `reportIncomingCallUI` yourself, or because a configured custom push
136
+ // was auto-detected (see `configureCustomIncomingCallPush`). Useful for
137
+ // e.g. kicking off a caller-ID lookup the moment a call starts ringing.
138
+ onIncomingCallUIShown: CodegenTypes.EventEmitter<IncomingCallUIEvent>;
139
+
140
+ // The system call UI shown via `reportIncomingCallUI` (or a configured
141
+ // custom push) was answered/declined. Handle these however your backend
142
+ // expects — e.g. call your own accept/decline endpoint, then
143
+ // `resolveCallUIToConference` or `dismissCallUI`.
144
+ onCallUIAnswered: CodegenTypes.EventEmitter<CallUIAnsweredEvent>;
145
+ onCallUIDeclined: CodegenTypes.EventEmitter<CallUIDeclinedEvent>;
146
+ }
147
+
148
+ export default TurboModuleRegistry.getEnforcing<Spec>('SinchCalling');
@@ -0,0 +1,245 @@
1
+ import type { EventSubscription } from 'react-native';
2
+ import NativeSinchCalling, {
3
+ type CallEndedEvent,
4
+ type CallEvent,
5
+ type ClientStartFailedEvent,
6
+ type IncomingCallEvent,
7
+ type CallUIAnsweredEvent,
8
+ type CallUIDeclinedEvent,
9
+ type IncomingCallUIEvent,
10
+ type VoipPushTokenEvent,
11
+ } from './NativeSinchCalling';
12
+ import type {
13
+ RegistrationCredentialsProvider,
14
+ SinchClientConfig,
15
+ } from './types';
16
+
17
+ class SinchCalling {
18
+ private credentialsProviderSubscription: EventSubscription | null = null;
19
+
20
+ configure(config: SinchClientConfig): void {
21
+ NativeSinchCalling.configure(
22
+ config.appKey,
23
+ config.environmentHost,
24
+ config.userId
25
+ );
26
+ }
27
+
28
+ start(): void {
29
+ NativeSinchCalling.start();
30
+ }
31
+
32
+ stop(): void {
33
+ NativeSinchCalling.stop();
34
+ }
35
+
36
+ /**
37
+ * Sinch requires a JWT (signed with your app secret on your backend) every
38
+ * time the client (re)registers. Call this once after `configure()` so the
39
+ * bridge can answer that challenge whenever the native SDK raises it.
40
+ */
41
+ setRegistrationCredentialsProvider(
42
+ provider: RegistrationCredentialsProvider
43
+ ): void {
44
+ this.credentialsProviderSubscription?.remove();
45
+ this.credentialsProviderSubscription =
46
+ NativeSinchCalling.onRegistrationCredentialsRequired(async () => {
47
+ try {
48
+ const jwt = await provider();
49
+ NativeSinchCalling.provideRegistrationCredentials(jwt);
50
+ } catch (error) {
51
+ NativeSinchCalling.failRegistration(
52
+ error instanceof Error ? error.message : String(error)
53
+ );
54
+ }
55
+ });
56
+ }
57
+
58
+ onClientStarted(listener: () => void): EventSubscription {
59
+ return NativeSinchCalling.onClientStarted(listener);
60
+ }
61
+
62
+ onClientStartFailed(
63
+ listener: (event: ClientStartFailedEvent) => void
64
+ ): EventSubscription {
65
+ return NativeSinchCalling.onClientStartFailed(listener);
66
+ }
67
+
68
+ callUser(userId: string): string {
69
+ return NativeSinchCalling.callUser(userId);
70
+ }
71
+
72
+ /**
73
+ * Places a PSTN call to a real phone number (E.164 format, e.g. "+14155550101").
74
+ * `callerId` is the caller-ID number shown to the recipient — required on
75
+ * Android, ignored on iOS (derived from the application's provisioned
76
+ * voice number instead).
77
+ */
78
+ callPhoneNumber(phoneNumber: string, callerId: string): string {
79
+ return NativeSinchCalling.callPhoneNumber(phoneNumber, callerId);
80
+ }
81
+
82
+ /**
83
+ * Joins a Sinch conference room by id — e.g. to answer a call your
84
+ * backend parked and bridged into a conference (see `reportIncomingCallUI`
85
+ * / `resolveCallUIToConference`). `callerId` is Android-only, same
86
+ * caveat as `callPhoneNumber`.
87
+ */
88
+ callConference(conferenceId: string, callerId: string): string {
89
+ return NativeSinchCalling.callConference(conferenceId, callerId);
90
+ }
91
+
92
+ answerCall(callId: string): void {
93
+ NativeSinchCalling.answerCall(callId);
94
+ }
95
+
96
+ hangupCall(callId: string): void {
97
+ NativeSinchCalling.hangupCall(callId);
98
+ }
99
+
100
+ setMuted(muted: boolean): void {
101
+ NativeSinchCalling.setMuted(muted);
102
+ }
103
+
104
+ setSpeakerEnabled(enabled: boolean): void {
105
+ NativeSinchCalling.setSpeakerEnabled(enabled);
106
+ }
107
+
108
+ /** `key` must be one of [0-9, #, *, A-D]. Returns false if it couldn't be sent. */
109
+ sendDTMF(callId: string, key: string): boolean {
110
+ return NativeSinchCalling.sendDTMF(callId, key);
111
+ }
112
+
113
+ onIncomingCall(
114
+ listener: (event: IncomingCallEvent) => void
115
+ ): EventSubscription {
116
+ return NativeSinchCalling.onIncomingCall(listener);
117
+ }
118
+
119
+ onCallProgressing(listener: (event: CallEvent) => void): EventSubscription {
120
+ return NativeSinchCalling.onCallProgressing(listener);
121
+ }
122
+
123
+ onCallEstablished(listener: (event: CallEvent) => void): EventSubscription {
124
+ return NativeSinchCalling.onCallEstablished(listener);
125
+ }
126
+
127
+ onCallEnded(listener: (event: CallEndedEvent) => void): EventSubscription {
128
+ return NativeSinchCalling.onCallEnded(listener);
129
+ }
130
+
131
+ /** Android only. Call before `configure()`. No-op on iOS. */
132
+ registerFcmPush(senderId: string, token: string): void {
133
+ NativeSinchCalling.registerFcmPush(senderId, token);
134
+ }
135
+
136
+ /** iOS only. Sets up SINManagedPush (VoIP push). No-op on Android. */
137
+ enablePushNotifications(useProductionAps: boolean): void {
138
+ NativeSinchCalling.enablePushNotifications(useProductionAps);
139
+ }
140
+
141
+ /**
142
+ * Android only. Forward the raw FCM data payload here (e.g. from
143
+ * `messaging().onMessage()` / `setBackgroundMessageHandler()`). By
144
+ * default this always relays to Sinch; call
145
+ * `configureCustomIncomingCallPush` first to also recognize your own
146
+ * backend's payload shape. No-op on iOS, which applies the same logic
147
+ * internally via PushKit.
148
+ */
149
+ relayRemotePushNotification(payload: Record<string, string>): void {
150
+ NativeSinchCalling.relayRemotePushNotification(payload);
151
+ }
152
+
153
+ /**
154
+ * Opts into recognizing your own backend's incoming-call push payloads,
155
+ * on both platforms. `idField`/`displayField` are the keys your payload
156
+ * uses for the call id and the caller's display name/number — e.g.
157
+ * `configureCustomIncomingCallPush('callId', 'callerNumber')` for a
158
+ * payload shaped `{ callId, callerNumber }`. Until this is called, pushes
159
+ * always relay straight to Sinch (see `relayRemotePushNotification`).
160
+ */
161
+ configureCustomIncomingCallPush(idField: string, displayField: string): void {
162
+ NativeSinchCalling.configureCustomIncomingCallPush(idField, displayField);
163
+ }
164
+
165
+ /**
166
+ * Reports a system call UI (CallKit / Telecom) for a call your own app
167
+ * logic decided to show — from a custom push, a Socket.IO event, or
168
+ * anything else. Independent of `configureCustomIncomingCallPush`; call
169
+ * this directly whenever you want to ring.
170
+ */
171
+ reportIncomingCallUI(callId: string, displayName: string): void {
172
+ NativeSinchCalling.reportIncomingCallUI(callId, displayName);
173
+ }
174
+
175
+ onPushTokenRegistered(listener: () => void): EventSubscription {
176
+ return NativeSinchCalling.onPushTokenRegistered(listener);
177
+ }
178
+
179
+ onPushTokenRegistrationFailed(
180
+ listener: (event: ClientStartFailedEvent) => void
181
+ ): EventSubscription {
182
+ return NativeSinchCalling.onPushTokenRegistrationFailed(listener);
183
+ }
184
+
185
+ /**
186
+ * Turns a call shown via `reportIncomingCallUI` (or a configured custom
187
+ * push) into a real call once you know which Sinch conference to join.
188
+ * Returns the real callId, or "" on failure.
189
+ */
190
+ resolveCallUIToConference(
191
+ callId: string,
192
+ conferenceId: string,
193
+ callerId: string
194
+ ): string {
195
+ return NativeSinchCalling.resolveCallUIToConference(
196
+ callId,
197
+ conferenceId,
198
+ callerId
199
+ );
200
+ }
201
+
202
+ /** Cleans up a system call UI that couldn't be resolved into a real call. */
203
+ dismissCallUI(callId: string): void {
204
+ NativeSinchCalling.dismissCallUI(callId);
205
+ }
206
+
207
+ /** Overrides the caller name shown on the system call UI (e.g. after a caller-ID lookup resolves). */
208
+ updateIncomingCallDisplayName(callId: string, displayName: string): void {
209
+ NativeSinchCalling.updateIncomingCallDisplayName(callId, displayName);
210
+ }
211
+
212
+ /** iOS only: hex-encoded PushKit VoIP token, issued on start and on rotation. */
213
+ onVoipPushTokenUpdated(
214
+ listener: (event: VoipPushTokenEvent) => void
215
+ ): EventSubscription {
216
+ return NativeSinchCalling.onVoipPushTokenUpdated(listener);
217
+ }
218
+
219
+ /**
220
+ * A system call UI was reported — either you called `reportIncomingCallUI`
221
+ * yourself, or a configured custom push was auto-detected. Useful for
222
+ * kicking off a caller-ID lookup the moment a call starts ringing.
223
+ */
224
+ onIncomingCallUIShown(
225
+ listener: (event: IncomingCallUIEvent) => void
226
+ ): EventSubscription {
227
+ return NativeSinchCalling.onIncomingCallUIShown(listener);
228
+ }
229
+
230
+ /** The system call UI shown via `reportIncomingCallUI` was answered. */
231
+ onCallUIAnswered(
232
+ listener: (event: CallUIAnsweredEvent) => void
233
+ ): EventSubscription {
234
+ return NativeSinchCalling.onCallUIAnswered(listener);
235
+ }
236
+
237
+ /** The system call UI shown via `reportIncomingCallUI` was declined. */
238
+ onCallUIDeclined(
239
+ listener: (event: CallUIDeclinedEvent) => void
240
+ ): EventSubscription {
241
+ return NativeSinchCalling.onCallUIDeclined(listener);
242
+ }
243
+ }
244
+
245
+ export default new SinchCalling();
package/src/index.tsx ADDED
@@ -0,0 +1,11 @@
1
+ export { default as SinchCalling } from './SinchCalling';
2
+ export type {
3
+ RegistrationCredentialsProvider,
4
+ SinchClientConfig,
5
+ } from './types';
6
+ export type {
7
+ CallEndedEvent,
8
+ CallEvent,
9
+ ClientStartFailedEvent,
10
+ IncomingCallEvent,
11
+ } from './NativeSinchCalling';
package/src/types.ts ADDED
@@ -0,0 +1,9 @@
1
+ export interface SinchClientConfig {
2
+ appKey: string;
3
+ environmentHost: string;
4
+ userId: string;
5
+ }
6
+
7
+ export type RegistrationCredentialsProvider = () => Promise<string>;
8
+
9
+ export class SinchRegistrationError extends Error {}