react-native-intercom

repository·master·Indexed 19 days ago

https://github.com/tinycreative/react-native-intercom

A React Native wrapper for the Intercom SDK (version 21.1.0) that provides a JavaScript interface to manage user identity, log events, and control the Intercom Messenger and Help Center UI via the IntercomClient class. Note: This package is marked as deprecated; Intercom recommends using the official intercom-react-native package.

Tokens
2.6K
Snippets
10
Records
13
Agent score
62%

What's inside react-native-intercom

  1. Install react-native-intercom

    master

    To install the package, use yarn or npm to add the dependency and then link the native dependencies.

    Note: This package is marked as DEPRECATED. Intercom recommends using the official package: https://github.com/intercom/intercom-react-native.

    yarn add react-native-intercom  # or npm install react-native-intercom
    
    react-native link react-native-intercom
  2. Configure Intercom for iOS

    master

    Follow these steps to set up Intercom on iOS:

    1. Install Intercom for iOS via CocoaPods or manual installation.
    2. Link the library in Xcode:
      • Add RNIntercom.xcodeproj from node_modules/react-native-intercom/iOS to your project's Libraries folder.
      • In Xcode, go to General Settings -> Build Phases -> Link Binary with Libraries and add libRNIntercom.a.
    3. Initialize in AppDelegate.m:
      • Add #import "Intercom/intercom.h" at the top.
      • Call `[Intercom setApiKey:@
  3. Control Messenger and UI Visibility

    master

    You can programmatically control the visibility and presentation of Intercom UI components:

    • displayMessenger(): Opens the Intercom messenger.
    • hideMessenger(): Closes the Intercom messenger.
    • displayMessageComposer(): Opens the message composer.
    • displayMessageComposerWithInitialMessage(message): Opens the composer with a pre-filled message.
    • displayConversationsList(): Shows the list of conversations.
    • displayHelpCenter(): Opens the Intercom Help Center.
    • presentArticle(articleId): Presents a specific help article.
    • presentCarousel(carousel): Presents a carousel.
    • setLauncherVisibility(visibility): Sets whether the Intercom launcher icon is visible. Use Intercom.Visibility.VISIBLE or Intercom.Visibility.GONE.
    • setInAppMessageVisibility(visibility): Sets whether in-app messages are visible. Use Intercom.Visibility.VISIBLE or Intercom.Visibility.GONE.
    • setBottomPadding(padding): Sets the bottom padding for the Intercom UI.
    import Intercom from 'react-native-intercom';
    
    // Show the messenger
    Intercom.displayMessenger();
    
    // Set launcher visibility
    Intercom.setLauncherVisibility(Intercom.Visibility.GONE);
  4. Manage User Identity with IntercomClient

    master

    Use the following methods to manage the identity of the user currently using your app:

    • registerIdentifiedUser(options): Registers a user with a known identity (e.g., via userId or email).
    • registerUnidentifiedUser(): Registers a user without a known identity.
    • updateUser(options): Updates attributes for the currently registered user.
    • setUserHash(userHash): Sets a user hash for identity validation (optional).
    • logout(): Logs the current user out of Intercom.
    • sendTokenToIntercom(token): Sends an authentication token to Intercom.
    import Intercom from 'react-native-intercom';
    
    // Register an identified user
    Intercom.registerIdentifiedUser({ userId: 'user_id_123' });
    
    // Update user metadata
    Intercom.updateUser({ name: 'John Doe' });
    
    // Log out
    Intercom.logout();
  5. Control the Intercom Messenger and UI

    master

    Use these methods to manage the visibility and presentation of Intercom UI components:

    • displayMessenger(): Opens the Intercom messenger.
    • hideMessenger(): Closes the Intercom messenger.
    • displayMessageComposer(): Opens the message composer.
    • displayMessageComposerWithInitialMessage(message): Opens the composer with a pre-filled message.
    • displayConversationsList(): Shows the list of conversations.
    • displayHelpCenter(): Opens the Intercom Help Center.
    • presentCarousel(carousel): Presents a specific carousel.
    • presentArticle(articleId): Presents a specific help article.
    • setLauncherVisibility(visibility): Sets whether the Intercom launcher is VISIBLE or GONE.
    • setInAppMessageVisibility(visibility): Sets whether in-app messages are VISIBLE or GONE.
    • setBottomPadding(padding): Adjusts the bottom padding for the UI.
  6. Listen to Intercom Events

    master

    The IntercomClient allows you to subscribe to specific Intercom events using addEventListener and removeEventListener.

    Available event types are found in Intercom.Notifications:

    • Intercom.Notifications.UNREAD_COUNT: Triggered when the unread conversation count changes.
    • Intercom.Notifications.WINDOW_DID_HIDE: Triggered when the Intercom window is hidden.
    • Intercom.Notifications.WINDOW_DID_SHOW: Triggered when the Intercom window is shown.
    import Intercom from 'react-native-intercom';
    
    const onUnreadChange = (count) => {
      console.log('Unread count changed:', count);
    };
    
    // Subscribe to event
    Intercom.addEventListener(Intercom.Notifications.UNREAD_COUNT, onUnreadChange);
    
    // Unsubscribe
    Intercom.removeEventListener(Intercom.Notifications.UNREAD_COUNT, onUnreadChange);
  7. Use the IntercomClient API

    master

    The IntercomClient is the primary interface for interacting with Intercom in your React Native application. It provides methods for user registration, event logging, messenger control, and push notification setup. The client is exported as a singleton, so you can import it directly and call its methods.

    import Intercom from 'react-native-intercom';
    
    // Example: Registering an identified user
    Intercom.registerIdentifiedUser({ userId: '123', email: 'user@example.com' });
    
    // Example: Logging an event
    Intercom.logEvent('button_clicked', { buttonId: 'signup' });
    
    // Example: Displaying the messenger
    Intercom.displayMessenger();
  8. Listen to Intercom notifications

    master

    The IntercomClient allows you to subscribe to specific Intercom events using addEventListener(type, handler) and removeEventListener(type, handler).

    Available notification types are found in IntercomClient.Notifications:

    • UNREAD_CHANGE_NOTIFICATION: Triggered when the unread conversation count changes.
    • WINDOW_DID_HIDE_NOTIFICATION: Triggered when the Intercom window is hidden.
    • WINDOW_DID_SHOW_NOTIFICATION: Triggered when the Intercom window is shown.
    import Intercom from 'react-native-intercom';
    
    const onUnreadChange = (count) => {
      console.log('Unread count changed:', count);
    };
    
    // Subscribe to unread count changes
    Intercom.addEventListener(Intercom.Notifications.UNREAD_COUNT, onUnreadChange);
    
    // Unsubscribe later
    Intercom.removeEventListener(Intercom.Notifications.UNREAD_COUNT, onUnreadChange);
  9. Register users with IntercomClient

    master

    You can manage user identity using the following methods:

    • registerIdentifiedUser(options): Registers a user with specific details (e.g., email, name, attributes).
    • registerUnidentifiedUser(): Registers a user without identifying information.
    • updateUser(options): Updates the attributes of the currently registered user.
    • logout(): Logs the current user out of Intercom.
    • setUserHash(userHash): Sets a user hash for identity validation (optional).
    import Intercom from 'react-native-intercom';
    
    // Register an identified user
    Intercom.registerIdentifiedUser({
      email: 'user@example.com',
      name: 'John Doe',
      userAttributes: {
        plan: 'premium'
      }
    });
    
    // Or register an unidentified user
    Intercom.registerUnidentifiedUser();
  10. Use the IntercomClient class

    master

    The IntercomClient is the primary interface for interacting with Intercom services in your React Native application. It provides methods for user registration, event logging, messenger control, and event listening. The module exports a default instance of IntercomClient, allowing you to import and use it directly without manual instantiation.

    import Intercom from 'react-native-intercom';
    
    // Use the default instance
    Intercom.logEvent('button_clicked', { id: 'signup' });