Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 33 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1022,37 +1022,48 @@ Set the level of the native logger
`Promise<boolean>`
___

### `Intercom.addEventListener(event,callback)`
### `Intercom.bootstrapEventListeners()`

Sets a listener for listed events:
Bootstrap event listeners (Only for Android). This handles platform-specific setup and returns a cleanup function.
This must be called before setting up your own NativeEventEmitter.

| Event | Platform |
| ------- | -------- |
| IntercomUnreadConversationCountDidChangeNotification| IOS, Android |
| IntercomHelpCenterDidShowNotification| IOS |
| IntercomHelpCenterDidHideNotification| IOS |
| IntercomWindowDidShowNotification| IOS | |
| IntercomWindowDidHideNotification| IOS |
### Returns

`() => void` - Cleanup function

### Usage

```javascript
import { NativeEventEmitter, NativeModules } from 'react-native';
import Intercom, { IntercomEvents } from '@intercom/intercom-react-native';

useEffect(() => {
const listener = Intercom.addEventListener('IntercomUnreadConversationCountDidChangeNotification', ({count}) => alert(count));
return () => {
listener.remove();
}
}, [])
```
const cleanupIntercomEventListeners = Intercom.bootstrapEventListeners();

### Options
const eventEmitter = new NativeEventEmitter(NativeModules.IntercomEventEmitter);

| Type | Type | Required |
| ------- | -------- | -------- |
| event| string (`IntercomEvents`) |yes |
| callback| function `({count?: number, visible?: boolean}) => void` |yes |
// Listen to unread conversation count changes
const unreadCountEventName = IntercomEvents.IntercomUnreadCountDidChange;
const countListener = eventEmitter.addListener(unreadCountEventName, (response) => {
console.log('Unread count:', response.count);
});

### Returns
return () => {
countListener.remove();
cleanupIntercomEventListeners();
};
}, []);
```

### Available Events

`EmitterSubscription`
| Event | Platform |
| ------- | -------- |
| IntercomUnreadConversationCountDidChangeNotification| iOS, Android |
| IntercomHelpCenterDidShowNotification| iOS |
| IntercomHelpCenterDidHideNotification| iOS |
| IntercomWindowDidShowNotification| iOS |
| IntercomWindowDidHideNotification| iOS |

___
### Types
Expand Down
21 changes: 17 additions & 4 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
AppState,
Image,
Linking,
NativeEventEmitter,
NativeModules,
Platform,
ScrollView,
StatusBar,
Expand All @@ -13,11 +15,11 @@ import {
View,
} from 'react-native';
import Intercom, {
IntercomEvents,
Space,
type UserAttributes,
Visibility,
IntercomContent,
IntercomEvents,
ThemeMode,
} from '@intercom/intercom-react-native';

Expand Down Expand Up @@ -156,17 +158,28 @@ export default function App() {
.catch((e) => console.log(e));

/**
* Handle message count changed
* Bootstrap Intercom event listeners
*/
const cleanupIntercomEventListeners = Intercom.bootstrapEventListeners();

const eventEmitter = new NativeEventEmitter(
NativeModules.IntercomEventEmitter
);

/**
* Unread notification count changed listener
*/
const countListener = Intercom.addEventListener(
IntercomEvents.IntercomUnreadCountDidChange,
const unreadCountEventName = IntercomEvents.IntercomUnreadCountDidChange;
const countListener = eventEmitter.addListener(
unreadCountEventName,
(response) => {
setCount(response.count as number);
}
);

return () => {
countListener.remove();
cleanupIntercomEventListeners();

// @ts-ignore - type definitions haven't been updated to 0.65 yet
urlListener.remove(); // <- for RN 0.65+
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@intercom/intercom-react-native",
"version": "8.8.0",
"version": "9.0.0-beta.1",
"description": "React Native wrapper to bridge our iOS and Android SDK",
"main": "lib/commonjs/index",
"module": "lib/module/index",
Expand Down
40 changes: 13 additions & 27 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import {
NativeModules,
NativeEventEmitter,
Platform,
type EmitterSubscription,
} from 'react-native';
import { NativeModules, Platform } from 'react-native';

const { IntercomModule, IntercomEventEmitter } = NativeModules;

Expand Down Expand Up @@ -45,11 +40,6 @@ export const IntercomEvents = {
IntercomEventEmitter.WINDOW_DID_HIDE_NOTIFICATION,
};

type EventType =
| 'IntercomUnreadConversationCountDidChangeNotification'
| 'IntercomWindowDidHideNotification'
| 'IntercomWindowDidShowNotification';

export type CustomAttributes = {
[key: string]: boolean | string | number;
};
Expand Down Expand Up @@ -324,12 +314,11 @@ export type IntercomType = {
setUserJwt(JWT: String): Promise<boolean>;

/**
* Add an event listener for the supported event types.
* [Android Only] Bootstrap event listeners for Android. Call this before setting up your own NativeEventEmitter
*
* @returns cleanup function for Android
*/
addEventListener: (
event: EventType,
callback: (response: { count?: number; visible: boolean }) => void
) => EmitterSubscription;
bootstrapEventListeners: () => () => void;
};

const Intercom: IntercomType = {
Expand Down Expand Up @@ -379,20 +368,17 @@ const Intercom: IntercomType = {
setLogLevel: (logLevel) => IntercomModule.setLogLevel(logLevel),
setThemeMode: (themeMode) => IntercomModule.setThemeMode(themeMode),
setUserJwt: (jwt) => IntercomModule.setUserJwt(jwt),
addEventListener: (event, callback) => {
event === IntercomEvents.IntercomUnreadCountDidChange &&
Platform.OS === 'android' &&

bootstrapEventListeners: () => {
if (Platform.OS === 'android') {
IntercomEventEmitter.startEventListener();
const eventEmitter = new NativeEventEmitter(IntercomEventEmitter);
const listener = eventEmitter.addListener(event, callback);
const originalRemove = listener.remove;
listener.remove = () => {
event === IntercomEvents.IntercomUnreadCountDidChange &&
Platform.OS === 'android' &&
}

return () => {
if (Platform.OS === 'android') {
IntercomEventEmitter.removeEventListener();
originalRemove();
}
};
return listener;
},
};

Expand Down