@talqui-oss/talqui-sdk-reactnative 0.1.1 → 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.
package/README.md CHANGED
@@ -190,6 +190,32 @@ Call it when your user logs out. It forgets the user on this device, clears ever
190
190
  await talqui.logout();
191
191
  ```
192
192
 
193
+ ## Push notifications
194
+
195
+ When your team, a bot or an AI agent answers while the user is away, Talqui can notify them. Pick how in app.talqui.chat → Plugins → WebChat → **Notificações**:
196
+
197
+ - **Through Talqui**: upload a service account of your Firebase project there, then hand the device token to the SDK. Talqui sends the push with your Firebase project.
198
+ - **Webhook**: Talqui posts a signed event to your backend, which notifies the user however you like. Nothing to do in the app. Send your own user id with `setContactMeta({ user_id: … })` to receive it in the event.
199
+
200
+ With [@react-native-firebase/messaging](https://rnfirebase.io/messaging/usage):
201
+
202
+ ```tsx
203
+ const talqui = useTalqui();
204
+
205
+ useEffect(() => {
206
+ messaging().getToken().then(talqui.setPushToken);
207
+ const unsubscribeRefresh = messaging().onTokenRefresh(talqui.setPushToken);
208
+ // Open the chat when the user taps a Talqui notification.
209
+ const unsubscribeTap = messaging().onNotificationOpenedApp((message) => talqui.handlePush(message.data));
210
+ return () => {
211
+ unsubscribeRefresh();
212
+ unsubscribeTap();
213
+ };
214
+ }, []);
215
+ ```
216
+
217
+ `logout()` unlinks the token from the user, so the next person on the phone never gets their replies. The user is only notified when the chat is not on screen.
218
+
193
219
  ## API
194
220
 
195
221
  ### `<TalquiProvider>`
@@ -214,6 +240,8 @@ await talqui.logout();
214
240
  | `setContactMeta(meta)` / `setSessionMeta(meta)` | Custom data, see [Metadata](#metadata). |
215
241
  | `prefill(text)` | Puts text in the message box. The user still decides to send it. |
216
242
  | `logout()` | Forgets the user and starts a fresh visitor. |
243
+ | `setPushToken(token)` | The device's FCM token, see [Push notifications](#push-notifications). |
244
+ | `handlePush(data)` | Opens the chat for a tapped Talqui notification; returns whether it was one. `isTalquiPush(data)` is also exported. |
217
245
  | `unreadCount`, `isReady`, `isOpen` | Current state. |
218
246
 
219
247
  ### `<TalquiChat>`
package/dist/index.d.mts CHANGED
@@ -85,6 +85,14 @@ interface TalquiApi {
85
85
  prefill(text: string): void;
86
86
  /** Call it when your user logs out: forgets this user and starts a fresh anonymous visitor. */
87
87
  logout(): Promise<void>;
88
+ /**
89
+ * The device's Firebase Cloud Messaging token, so a reply reaches the user while the app is
90
+ * closed. Works when the WebChat connection sends notifications "through Talqui" with your
91
+ * Firebase project (Settings → WebChat → Notificações). Call it at startup and on token refresh.
92
+ */
93
+ setPushToken(token: string): void;
94
+ /** Opens the chat for a tapped Talqui notification. Returns whether it was one. */
95
+ handlePush(data: Record<string, unknown> | null | undefined): boolean;
88
96
  /** Messages received while the chat was not on screen. */
89
97
  unreadCount: number;
90
98
  /** Whether the chat is loaded and accepting commands. */
@@ -124,6 +132,9 @@ declare function useTalqui(): TalquiApi;
124
132
  /** Runs `listener` for every `name` event from the chat while the component is mounted. */
125
133
  declare function useTalquiEvent<E extends TalquiEventName>(name: E, listener: TalquiEventListener<E>): void;
126
134
 
135
+ /** Whether a push message was sent by Talqui (check the message's `data`). */
136
+ declare function isTalquiPush(data: Record<string, unknown> | null | undefined): boolean;
137
+
127
138
  interface TalquiChatProps {
128
139
  style?: StyleProp<ViewStyle>;
129
140
  /**
@@ -147,4 +158,4 @@ interface TalquiChatProps {
147
158
  */
148
159
  declare function TalquiChat({ style, visible, onCloseRequested, hideCloseButton }: TalquiChatProps): react_jsx_runtime.JSX.Element;
149
160
 
150
- export { type TalquiApi, TalquiChat, type TalquiChatProps, type TalquiContact, type TalquiEventListener, type TalquiEventMap, type TalquiEventName, type TalquiMeta, TalquiProvider, type TalquiProviderProps, type TalquiUser, useTalqui, useTalquiEvent };
161
+ export { type TalquiApi, TalquiChat, type TalquiChatProps, type TalquiContact, type TalquiEventListener, type TalquiEventMap, type TalquiEventName, type TalquiMeta, TalquiProvider, type TalquiProviderProps, type TalquiUser, isTalquiPush, useTalqui, useTalquiEvent };
package/dist/index.d.ts CHANGED
@@ -85,6 +85,14 @@ interface TalquiApi {
85
85
  prefill(text: string): void;
86
86
  /** Call it when your user logs out: forgets this user and starts a fresh anonymous visitor. */
87
87
  logout(): Promise<void>;
88
+ /**
89
+ * The device's Firebase Cloud Messaging token, so a reply reaches the user while the app is
90
+ * closed. Works when the WebChat connection sends notifications "through Talqui" with your
91
+ * Firebase project (Settings → WebChat → Notificações). Call it at startup and on token refresh.
92
+ */
93
+ setPushToken(token: string): void;
94
+ /** Opens the chat for a tapped Talqui notification. Returns whether it was one. */
95
+ handlePush(data: Record<string, unknown> | null | undefined): boolean;
88
96
  /** Messages received while the chat was not on screen. */
89
97
  unreadCount: number;
90
98
  /** Whether the chat is loaded and accepting commands. */
@@ -124,6 +132,9 @@ declare function useTalqui(): TalquiApi;
124
132
  /** Runs `listener` for every `name` event from the chat while the component is mounted. */
125
133
  declare function useTalquiEvent<E extends TalquiEventName>(name: E, listener: TalquiEventListener<E>): void;
126
134
 
135
+ /** Whether a push message was sent by Talqui (check the message's `data`). */
136
+ declare function isTalquiPush(data: Record<string, unknown> | null | undefined): boolean;
137
+
127
138
  interface TalquiChatProps {
128
139
  style?: StyleProp<ViewStyle>;
129
140
  /**
@@ -147,4 +158,4 @@ interface TalquiChatProps {
147
158
  */
148
159
  declare function TalquiChat({ style, visible, onCloseRequested, hideCloseButton }: TalquiChatProps): react_jsx_runtime.JSX.Element;
149
160
 
150
- export { type TalquiApi, TalquiChat, type TalquiChatProps, type TalquiContact, type TalquiEventListener, type TalquiEventMap, type TalquiEventName, type TalquiMeta, TalquiProvider, type TalquiProviderProps, type TalquiUser, useTalqui, useTalquiEvent };
161
+ export { type TalquiApi, TalquiChat, type TalquiChatProps, type TalquiContact, type TalquiEventListener, type TalquiEventMap, type TalquiEventName, type TalquiMeta, TalquiProvider, type TalquiProviderProps, type TalquiUser, isTalquiPush, useTalqui, useTalquiEvent };
package/dist/index.js CHANGED
@@ -32,6 +32,7 @@ var index_exports = {};
32
32
  __export(index_exports, {
33
33
  TalquiChat: () => TalquiChat,
34
34
  TalquiProvider: () => TalquiProvider,
35
+ isTalquiPush: () => isTalquiPush,
35
36
  useTalqui: () => useTalqui,
36
37
  useTalquiEvent: () => useTalquiEvent
37
38
  });
@@ -47,7 +48,7 @@ var import_async_storage = __toESM(require("@react-native-async-storage/async-st
47
48
  // src/protocol.ts
48
49
  var PROTOCOL = 1;
49
50
  var DEFAULT_BASE_URL = "https://webchat.talqui.chat";
50
- var SDK_LABEL = "react-native@0.1.1";
51
+ var SDK_LABEL = "react-native@0.2.0";
51
52
  var UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
52
53
  var IDENTITY_PATTERN = /^[A-Za-z0-9_-]{8,128}$/;
53
54
  var USER_HASH_PATTERN = /^[A-Za-z0-9_-]{32,128}$/;
@@ -112,6 +113,9 @@ function normalizeMeta(meta, warn) {
112
113
  }
113
114
  return out;
114
115
  }
116
+ function isTalquiPush(data) {
117
+ return data?.talqui === "1";
118
+ }
115
119
 
116
120
  // src/identity.ts
117
121
  var key = (tenantId) => `talqui:identity:${tenantId}`;
@@ -331,19 +335,22 @@ function TalquiProvider(props) {
331
335
  }
332
336
  });
333
337
  }, []);
338
+ const pushToken = (0, import_react2.useRef)(null);
339
+ const platform = import_react_native2.Platform.OS === "ios" || import_react_native2.Platform.OS === "android" ? import_react_native2.Platform.OS : null;
334
340
  const replayAll = (0, import_react2.useCallback)(() => {
335
341
  const state = replay.current;
336
342
  if (state.contact) send("setContact", state.contact);
337
343
  if (Object.keys(state.contactMeta).length) send("setContactMeta", state.contactMeta);
338
344
  if (Object.keys(state.sessionMeta).length) send("setSessionMeta", state.sessionMeta);
339
345
  send("setVisibility", { visible: visibleRef.current });
346
+ if (pushToken.current) send("registerPushToken", { token: pushToken.current, platform });
340
347
  pageCloseButton.current = true;
341
348
  syncCloseButton();
342
349
  if (state.prefill) {
343
350
  send("prefill", { text: state.prefill });
344
351
  state.prefill = null;
345
352
  }
346
- }, [send, syncCloseButton]);
353
+ }, [send, syncCloseButton, platform]);
347
354
  const closeOverlay = (0, import_react2.useCallback)(() => setIsOpen(false), []);
348
355
  const inlineRef = (0, import_react2.useRef)(inline);
349
356
  inlineRef.current = inline;
@@ -433,6 +440,7 @@ function TalquiProvider(props) {
433
440
  unsubscribe();
434
441
  resolve();
435
442
  }
443
+ if (pushToken.current) send("unregisterPushToken", { token: pushToken.current });
436
444
  send("reset", {});
437
445
  });
438
446
  }
@@ -486,6 +494,8 @@ function TalquiProvider(props) {
486
494
  setSessionMeta: unsupported,
487
495
  prefill: unsupported,
488
496
  logout: async () => unsupported(),
497
+ setPushToken: unsupported,
498
+ handlePush: () => false,
489
499
  unreadCount: 0,
490
500
  isReady: false,
491
501
  isOpen: false
@@ -514,11 +524,24 @@ function TalquiProvider(props) {
514
524
  else replay.current.prefill = trimmed;
515
525
  },
516
526
  logout,
527
+ setPushToken: (token) => {
528
+ const trimmed = typeof token === "string" ? token.trim() : "";
529
+ if (!trimmed) return;
530
+ pushToken.current = trimmed;
531
+ send("registerPushToken", { token: trimmed, platform });
532
+ },
533
+ handlePush: (data) => {
534
+ if (!isTalquiPush(data) || data?.tenantId !== tenantId) return false;
535
+ setOwner("overlay");
536
+ setOverlayMounted(true);
537
+ setIsOpen(true);
538
+ return true;
539
+ },
517
540
  unreadCount,
518
541
  isReady,
519
542
  isOpen
520
543
  };
521
- }, [supported, warn, closeOverlay, identify, setContact, setMeta, send, logout, unreadCount, isReady, isOpen]);
544
+ }, [supported, warn, closeOverlay, identify, setContact, setMeta, send, logout, unreadCount, isReady, isOpen, platform, tenantId]);
522
545
  const renderWebView = (0, import_react2.useCallback)(
523
546
  (style) => identityLoaded ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
524
547
  TalquiWebView,
@@ -608,6 +631,7 @@ function TalquiChat({ style, visible = true, onCloseRequested, hideCloseButton =
608
631
  0 && (module.exports = {
609
632
  TalquiChat,
610
633
  TalquiProvider,
634
+ isTalquiPush,
611
635
  useTalqui,
612
636
  useTalquiEvent
613
637
  });
package/dist/index.mjs CHANGED
@@ -16,7 +16,7 @@ import AsyncStorage from "@react-native-async-storage/async-storage";
16
16
  // src/protocol.ts
17
17
  var PROTOCOL = 1;
18
18
  var DEFAULT_BASE_URL = "https://webchat.talqui.chat";
19
- var SDK_LABEL = "react-native@0.1.1";
19
+ var SDK_LABEL = "react-native@0.2.0";
20
20
  var UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
21
21
  var IDENTITY_PATTERN = /^[A-Za-z0-9_-]{8,128}$/;
22
22
  var USER_HASH_PATTERN = /^[A-Za-z0-9_-]{32,128}$/;
@@ -81,6 +81,9 @@ function normalizeMeta(meta, warn) {
81
81
  }
82
82
  return out;
83
83
  }
84
+ function isTalquiPush(data) {
85
+ return data?.talqui === "1";
86
+ }
84
87
 
85
88
  // src/identity.ts
86
89
  var key = (tenantId) => `talqui:identity:${tenantId}`;
@@ -300,19 +303,22 @@ function TalquiProvider(props) {
300
303
  }
301
304
  });
302
305
  }, []);
306
+ const pushToken = useRef2(null);
307
+ const platform = Platform2.OS === "ios" || Platform2.OS === "android" ? Platform2.OS : null;
303
308
  const replayAll = useCallback2(() => {
304
309
  const state = replay.current;
305
310
  if (state.contact) send("setContact", state.contact);
306
311
  if (Object.keys(state.contactMeta).length) send("setContactMeta", state.contactMeta);
307
312
  if (Object.keys(state.sessionMeta).length) send("setSessionMeta", state.sessionMeta);
308
313
  send("setVisibility", { visible: visibleRef.current });
314
+ if (pushToken.current) send("registerPushToken", { token: pushToken.current, platform });
309
315
  pageCloseButton.current = true;
310
316
  syncCloseButton();
311
317
  if (state.prefill) {
312
318
  send("prefill", { text: state.prefill });
313
319
  state.prefill = null;
314
320
  }
315
- }, [send, syncCloseButton]);
321
+ }, [send, syncCloseButton, platform]);
316
322
  const closeOverlay = useCallback2(() => setIsOpen(false), []);
317
323
  const inlineRef = useRef2(inline);
318
324
  inlineRef.current = inline;
@@ -402,6 +408,7 @@ function TalquiProvider(props) {
402
408
  unsubscribe();
403
409
  resolve();
404
410
  }
411
+ if (pushToken.current) send("unregisterPushToken", { token: pushToken.current });
405
412
  send("reset", {});
406
413
  });
407
414
  }
@@ -455,6 +462,8 @@ function TalquiProvider(props) {
455
462
  setSessionMeta: unsupported,
456
463
  prefill: unsupported,
457
464
  logout: async () => unsupported(),
465
+ setPushToken: unsupported,
466
+ handlePush: () => false,
458
467
  unreadCount: 0,
459
468
  isReady: false,
460
469
  isOpen: false
@@ -483,11 +492,24 @@ function TalquiProvider(props) {
483
492
  else replay.current.prefill = trimmed;
484
493
  },
485
494
  logout,
495
+ setPushToken: (token) => {
496
+ const trimmed = typeof token === "string" ? token.trim() : "";
497
+ if (!trimmed) return;
498
+ pushToken.current = trimmed;
499
+ send("registerPushToken", { token: trimmed, platform });
500
+ },
501
+ handlePush: (data) => {
502
+ if (!isTalquiPush(data) || data?.tenantId !== tenantId) return false;
503
+ setOwner("overlay");
504
+ setOverlayMounted(true);
505
+ setIsOpen(true);
506
+ return true;
507
+ },
486
508
  unreadCount,
487
509
  isReady,
488
510
  isOpen
489
511
  };
490
- }, [supported, warn, closeOverlay, identify, setContact, setMeta, send, logout, unreadCount, isReady, isOpen]);
512
+ }, [supported, warn, closeOverlay, identify, setContact, setMeta, send, logout, unreadCount, isReady, isOpen, platform, tenantId]);
491
513
  const renderWebView = useCallback2(
492
514
  (style) => identityLoaded ? /* @__PURE__ */ jsx2(
493
515
  TalquiWebView,
@@ -576,6 +598,7 @@ function TalquiChat({ style, visible = true, onCloseRequested, hideCloseButton =
576
598
  export {
577
599
  TalquiChat,
578
600
  TalquiProvider,
601
+ isTalquiPush,
579
602
  useTalqui,
580
603
  useTalquiEvent
581
604
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@talqui-oss/talqui-sdk-reactnative",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "description": "Live chat SDK for React Native and Expo: in-app customer support chat with your team, bots and AI agents.",
5
5
  "license": "MIT",
6
6
  "author": "Talqui",
@@ -15,6 +15,7 @@ import {
15
15
  buildChatUrl,
16
16
  commandScript,
17
17
  DEFAULT_BASE_URL,
18
+ isTalquiPush,
18
19
  isUuid,
19
20
  normalizeMeta,
20
21
  parseEvent,
@@ -211,12 +212,17 @@ export function TalquiProvider(props: TalquiProviderProps) {
211
212
  });
212
213
  }, []);
213
214
 
215
+ // The device's push token outlives a logout (it is the phone's, not the user's).
216
+ const pushToken = useRef<string | null>(null);
217
+ const platform = Platform.OS === 'ios' || Platform.OS === 'android' ? Platform.OS : null;
218
+
214
219
  const replayAll = useCallback(() => {
215
220
  const state = replay.current;
216
221
  if (state.contact) send('setContact', state.contact);
217
222
  if (Object.keys(state.contactMeta).length) send('setContactMeta', state.contactMeta);
218
223
  if (Object.keys(state.sessionMeta).length) send('setSessionMeta', state.sessionMeta);
219
224
  send('setVisibility', { visible: visibleRef.current });
225
+ if (pushToken.current) send('registerPushToken', { token: pushToken.current, platform });
220
226
  // A freshly loaded page shows the control again.
221
227
  pageCloseButton.current = true;
222
228
  syncCloseButton();
@@ -224,7 +230,7 @@ export function TalquiProvider(props: TalquiProviderProps) {
224
230
  send('prefill', { text: state.prefill });
225
231
  state.prefill = null;
226
232
  }
227
- }, [send, syncCloseButton]);
233
+ }, [send, syncCloseButton, platform]);
228
234
 
229
235
  const closeOverlay = useCallback(() => setIsOpen(false), []);
230
236
  const inlineRef = useRef(inline);
@@ -331,6 +337,8 @@ export function TalquiProvider(props: TalquiProviderProps) {
331
337
  unsubscribe();
332
338
  resolve();
333
339
  }
340
+ // The next person on this phone must not get this user's replies.
341
+ if (pushToken.current) send('unregisterPushToken', { token: pushToken.current });
334
342
  send('reset', {});
335
343
  });
336
344
  }
@@ -389,6 +397,8 @@ export function TalquiProvider(props: TalquiProviderProps) {
389
397
  setSessionMeta: unsupported,
390
398
  prefill: unsupported,
391
399
  logout: async () => unsupported(),
400
+ setPushToken: unsupported,
401
+ handlePush: () => false,
392
402
  unreadCount: 0,
393
403
  isReady: false,
394
404
  isOpen: false,
@@ -417,11 +427,24 @@ export function TalquiProvider(props: TalquiProviderProps) {
417
427
  else replay.current.prefill = trimmed;
418
428
  },
419
429
  logout,
430
+ setPushToken: (token) => {
431
+ const trimmed = typeof token === 'string' ? token.trim() : '';
432
+ if (!trimmed) return;
433
+ pushToken.current = trimmed;
434
+ send('registerPushToken', { token: trimmed, platform });
435
+ },
436
+ handlePush: (data) => {
437
+ if (!isTalquiPush(data) || data?.tenantId !== tenantId) return false;
438
+ setOwner('overlay');
439
+ setOverlayMounted(true);
440
+ setIsOpen(true);
441
+ return true;
442
+ },
420
443
  unreadCount,
421
444
  isReady,
422
445
  isOpen,
423
446
  };
424
- }, [supported, warn, closeOverlay, identify, setContact, setMeta, send, logout, unreadCount, isReady, isOpen]);
447
+ }, [supported, warn, closeOverlay, identify, setContact, setMeta, send, logout, unreadCount, isReady, isOpen, platform, tenantId]);
425
448
 
426
449
  // --- rendering --------------------------------------------------------------------------------
427
450
  const renderWebView = useCallback(
package/src/index.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export { TalquiProvider, useTalqui, useTalquiEvent } from './TalquiProvider';
2
+ export { isTalquiPush } from './protocol';
2
3
  export type { TalquiProviderProps } from './TalquiProvider';
3
4
  export { TalquiChat } from './TalquiChat';
4
5
  export type { TalquiChatProps } from './TalquiChat';
package/src/protocol.ts CHANGED
@@ -7,7 +7,7 @@ import type { TalquiEventMap, TalquiEventName, TalquiMeta } from './types';
7
7
  */
8
8
  export const PROTOCOL = 1;
9
9
  export const DEFAULT_BASE_URL = 'https://webchat.talqui.chat';
10
- export const SDK_LABEL = 'react-native@0.1.1';
10
+ export const SDK_LABEL = 'react-native@0.2.0';
11
11
 
12
12
  const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
13
13
  export const IDENTITY_PATTERN = /^[A-Za-z0-9_-]{8,128}$/;
@@ -99,3 +99,8 @@ export function normalizeMeta(meta: TalquiMeta, warn: (message: string) => void)
99
99
  }
100
100
  return out;
101
101
  }
102
+
103
+ /** Whether a push message was sent by Talqui (check the message's `data`). */
104
+ export function isTalquiPush(data: Record<string, unknown> | null | undefined): boolean {
105
+ return data?.talqui === '1';
106
+ }
package/src/types.ts CHANGED
@@ -62,6 +62,14 @@ export interface TalquiApi {
62
62
  prefill(text: string): void;
63
63
  /** Call it when your user logs out: forgets this user and starts a fresh anonymous visitor. */
64
64
  logout(): Promise<void>;
65
+ /**
66
+ * The device's Firebase Cloud Messaging token, so a reply reaches the user while the app is
67
+ * closed. Works when the WebChat connection sends notifications "through Talqui" with your
68
+ * Firebase project (Settings → WebChat → Notificações). Call it at startup and on token refresh.
69
+ */
70
+ setPushToken(token: string): void;
71
+ /** Opens the chat for a tapped Talqui notification. Returns whether it was one. */
72
+ handlePush(data: Record<string, unknown> | null | undefined): boolean;
65
73
  /** Messages received while the chat was not on screen. */
66
74
  unreadCount: number;
67
75
  /** Whether the chat is loaded and accepting commands. */