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
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,52 @@ See the [example app](https://github.com/intercom/intercom-react-native/blob/mai

___

### Expo

If you are using Expo, you can use the built-in plugin.

After installing this npm package, add the [config plugin](https://docs.expo.io/guides/config-plugins/) to the [`plugins`](https://docs.expo.io/versions/latest/config/app/#plugins) array of your `app.json` or `app.config.js`:

```json
{
"expo": {
"plugins": ["@config-plugins/intercom-react-native"]
}
}
```

The plugin provides props for extra customization. Every time you change the props or plugins, you'll need to rebuild (and `prebuild`) the native app. If no extra properties are added, defaults will be used.

- `appId` (_string_): App ID from Intercom.
- `androidApiKey` (_string_): Android API Key from Intercom.
- `iosApiKey` (_string_): iOS API Key from Intercom.
- `intercomRegion` (_string_): Region for Intercom `US`, `EU`, `AU`. Optional. Defaults to `US`.

```json
{
"expo": {
"plugins": [
[
"@config-plugins/intercom-react-native",
{
"appId": "abc123",
"androidApiKey": "android_sdk-abc123",
"iosApiKey": "ios_sdk-abc123",
"intercomRegion": "EU" // Europe
}
]
]
}
}
```

Next, rebuild your app as described in the ["Adding custom native code"](https://docs.expo.io/workflow/customizing/) guide.

#### Limitations

- **No push notifications support**: Intercom push notifications currently aren't supported by this config plugin extension. This will be added in the future.


## Methods

## Import
Expand Down
1 change: 1 addition & 0 deletions app.plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./lib/commonjs/expo-plugins');
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"access": "public"
},
"devDependencies": {
"@expo/config-plugins": "^7.8.4",
"@react-native-community/eslint-config": "^2.0.0",
"@types/jest": "^26.0.0",
"@types/mocha": "^8.2.2",
Expand Down
8 changes: 8 additions & 0 deletions src/expo-plugins/@types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export type IntercomRegion = 'US' | 'EU' | 'AU';

export type IntercomPluginProps = {
iosApiKey: string;
androidApiKey: string;
appId: string;
intercomRegion?: IntercomRegion;
};
140 changes: 140 additions & 0 deletions src/expo-plugins/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import {
ConfigPlugin,
createRunOncePlugin,
withAppDelegate,
AndroidConfig,
withMainApplication,
withAndroidManifest,
withInfoPlist,
} from '@expo/config-plugins';
import {
addImports,
appendContentsInsideDeclarationBlock,
} from '@expo/config-plugins/build/android/codeMod';
import {
addObjcImports,
insertContentsInsideObjcFunctionBlock,
} from '@expo/config-plugins/build/ios/codeMod';
import type { IntercomPluginProps, IntercomRegion } from './@types';
import packageJson from '../../package.json';

const mainApplication: ConfigPlugin<IntercomPluginProps> = (_config, props) =>
withMainApplication(_config, (config) => {
let stringContents = config.modResults.contents;
stringContents = addImports(
stringContents,
['com.intercom.reactnative.IntercomModule;'],
false
);
stringContents = appendContentsInsideDeclarationBlock(
stringContents,
'onCreate',
`IntercomModule.initialize(this, "${props.androidApiKey}", "${props.appId}");`
);
config.modResults.contents = stringContents;
return config;
});

const androidManifest: ConfigPlugin<IntercomPluginProps> = (
_config,
{ intercomRegion = 'US' }
) => {
let newConfig = AndroidConfig.Permissions.withPermissions(
_config,
[
'android.permission.READ_EXTERNAL_STORAGE',
'android.permission.VIBRATE',
].filter(Boolean)
);

newConfig = withAndroidManifest(newConfig, (config) => {
const currentMainApplication =
AndroidConfig.Manifest.getMainApplicationOrThrow(config.modResults);
const androidRegionMapper: Record<IntercomRegion, string> = {
US: '@integer/intercom_server_region_us',
EU: '@integer/intercom_server_region_eu',
AU: '@integer/intercom_server_region_aus',
};

AndroidConfig.Manifest.addMetaDataItemToMainApplication(
currentMainApplication,
'io.intercom.android.sdk.server.region',
androidRegionMapper[intercomRegion]
);

return config;
});

return newConfig;
};

const withIntercomAndroid: ConfigPlugin<IntercomPluginProps> = (
config,
props
) => {
let newConfig = config;
newConfig = mainApplication(newConfig, props);
newConfig = androidManifest(newConfig, props);
return newConfig;
};

const appDelegate: ConfigPlugin<IntercomPluginProps> = (_config, props) =>
withAppDelegate(_config, (config) => {
let stringContents = config.modResults.contents;
stringContents = addObjcImports(stringContents, ['<IntercomModule.h>']);
stringContents = insertContentsInsideObjcFunctionBlock(
stringContents,
'application didFinishLaunchingWithOptions:',
`[IntercomModule initialize:@"${props.iosApiKey}" withAppId:@"${props.appId}"];`,
{ position: 'tailBeforeLastReturn' }
);
config.modResults.contents = stringContents;
return config;
});

const infoPlist: ConfigPlugin<IntercomPluginProps> = (
_config,
{ intercomRegion }
) => {
const newConfig = withInfoPlist(_config, (config) => {
if (intercomRegion) {
config.modResults.IntercomRegion = intercomRegion;
}

return config;
});

return newConfig;
};
const withIntercomIOS: ConfigPlugin<IntercomPluginProps> = (config, props) => {
let newConfig = appDelegate(config, props);
newConfig = infoPlist(newConfig, props);
return newConfig;
};

const withIntercomReactNative: ConfigPlugin<IntercomPluginProps> = (
config,
props
) => {
let newConfig = config;
newConfig = withIntercomAndroid(newConfig, props);
newConfig = withIntercomIOS(newConfig, props);
return newConfig;
};

const pkg = {
// Prevent this plugin from being run more than once.
// This pattern enables users to safely migrate off of this
// out-of-tree `@config-plugins/intercom-react-native` to a future
// upstream plugin in `intercom-react-native`
name: packageJson.name,
// Indicates that this plugin is dangerously linked to a module,
// and might not work with the latest version of that module.
version: packageJson.version,
};

export default createRunOncePlugin(
withIntercomReactNative,
pkg.name,
pkg.version
);
Loading