import * as React from 'react'; import { useEffect, useState } from 'react'; import { Alert, AppState, Image, Linking, NativeEventEmitter, NativeModules, Platform, ScrollView, StatusBar, StyleSheet, Text, View, } from 'react-native'; import Intercom, { Space, type UserAttributes, Visibility, IntercomContent, IntercomEvents, ThemeMode, } from '@intercom/intercom-react-native'; import { CAROUSEL_ID, SURVEY_ID, EVENT_NAME, ARTICLE_ID, USER_NAME, COLLECTION_ID, SEARCH_TERM, TOKEN, CONVERSATION_ID, } from './constants'; import Button from './components/Button'; import Input from './components/Input'; import AsyncStorage from '@react-native-async-storage/async-storage'; const AUTK_KEY = 'auth'; const COLLECTIONS: string[] = []; //Provide help center collections ids export default function App() { const [count, setCount] = useState(0); const [loggedUser, setLoggedUser] = useState(false); const [userName, setUserName] = useState(USER_NAME); const [bottomPadding, setBottomPadding] = useState(0); const [inAppMessageVisibility, setInAppMessageVisibility] = useState(true); const [launcherVisibility, setLauncherVisibility] = useState(false); const [user, setUser] = useState({ email: '' }); const [currentTheme, setCurrentTheme] = useState(ThemeMode.SYSTEM); const [conversationId, setConversationId] = useState( CONVERSATION_ID ); const [articleId, setArticleId] = useState(ARTICLE_ID); const [carouselId, setCarouselId] = useState(CAROUSEL_ID); const [surveyId, setSurveyId] = useState(SURVEY_ID); const [eventName, setEventName] = useState(EVENT_NAME); const [collectionId, setCollectionId] = useState( COLLECTION_ID ); const [searchTerm, setSearchTerm] = useState(SEARCH_TERM); const showErrorAlert = (e: Error) => { Alert.alert('ERROR', JSON.stringify(e)); }; const showResponseAlert = (obj: any) => { Alert.alert('RESPONSE', JSON.stringify(obj)); }; const showEmptyAlertMessage = (field: string) => { Alert.alert(field, `Please provide ${field}`); }; const showIncorrectFieldMessage = (field: string) => { Alert.alert(field, `Provided ${field} is not of correct format`); }; const showLoggedInStatusAlert = () => { Intercom.isUserLoggedIn().then((res) => { Alert.alert(`Logged in status: ${res ? 'Yes' : 'No'}`); }); }; const showLoggedInUserAttributes = () => { Intercom.fetchLoggedInUserAttributes().then((res) => { Alert.alert('User Attributes', JSON.stringify(res)); }); }; const validateEmail = (email: string | undefined) => { return String(email) .toLowerCase() .match( /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|.(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ ); }; const resetAttributes = () => { setCount(0); Intercom.setBottomPadding(0).then(() => setBottomPadding(0)); Intercom.setLauncherVisibility(Visibility.GONE).then(() => setLauncherVisibility(false) ); Intercom.setInAppMessageVisibility(Visibility.VISIBLE).then(() => setInAppMessageVisibility(true) ); setConversationId(CONVERSATION_ID); setArticleId(ARTICLE_ID); setCarouselId(CAROUSEL_ID); setSurveyId(SURVEY_ID); setEventName(EVENT_NAME); setCollectionId(COLLECTION_ID); setSearchTerm(SEARCH_TERM); setUserName(USER_NAME); }; useEffect(() => { /** * Restore user login status */ AsyncStorage.getItem(AUTK_KEY).then((it) => { it === 'true' && setLoggedUser(true); if (it && it !== 'true') { setUser((u) => ({ ...u, email: it })); } }); /** * Handle PushNotification */ const appChangeListener = AppState.addEventListener( 'change', (nextAppState) => nextAppState === 'active' && Intercom.handlePushMessage() ); /** * Handle Push Notification deep links */ const urlListener = Linking.addEventListener('url', (event) => { if (event) { Alert.alert(event.url); } }); Linking.getInitialURL() .then((url) => { if (url) { Alert.alert(url); } }) .catch((e) => console.log(e)); /** * Bootstrap Intercom event listeners */ const cleanupIntercomEventListeners = Intercom.bootstrapEventListeners(); const eventEmitter = new NativeEventEmitter( NativeModules.IntercomEventEmitter ); /** * Unread notification count changed listener */ 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+ // Linking.removeEventListener('url', () => {}); <- for RN < 0.65 // @ts-ignore - type definitions haven't been updated to 0.65 yet appChangeListener.remove(); // <- for RN 0.65+ //AppState.removeEventListener('change', () => {}); <- for RN < 0.65 }; }, []); return ( Intercom Example App {`Logged In: ${loggedUser ? 'Yes' : 'No'}`} Unread messages count: {count}