All files / src/withPushNotifications withAndroidPushNotifications.ts

100% Statements 42/42
100% Branches 10/10
100% Functions 12/12
100% Lines 42/42

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184                                                            4x 29x 2x 1x               1x           2x           4x 29x 2x 1x       1x           1x                 1x         2x                 4x       29x                   4x     31x 5x 5x 4x   5x 5x 1x       5x 4x       5x               4x 29x     2x       2x         2x 2x   1x       1x         4x       31x 2x       2x     29x               4x 31x              
import {
  ConfigPlugin,
  WarningAggregator,
  withAndroidManifest,
  withAppBuildGradle,
  withDangerousMod,
  withPlugins,
  withProjectBuildGradle,
} from 'expo/config-plugins';
import fs from 'fs';
import path from 'path';
 
import { ConfigPluginPropsWithDefaults } from '../withIterable.types';
import {
  DEFAULT_GOOGLE_SERVICES_PATH,
  FIREBASE_BOM_CLASS_PATH,
  FIREBASE_BOM_VERSION,
  FIREBASE_MESSAGING_CLASS_PATH,
  GOOGLE_SERVICES_CLASS_PATH,
  GOOGLE_SERVICES_PLUGIN,
  GOOGLE_SERVICES_VERSION,
} from './withAndroidPushNotifications.constants';
import {
  addProjectDependency,
  addApplyPlugin,
  addAppDependency,
} from './withAndroidPushNotifications.utils';
 
const withFirebaseInProjectBuildGradle: ConfigPlugin<
  ConfigPluginPropsWithDefaults
> = (config) => {
  return withProjectBuildGradle(config, async (newConfig) => {
    if (newConfig.modResults.language === 'groovy') {
      newConfig.modResults.contents = addProjectDependency(
        newConfig.modResults.contents,
        {
          classpath: GOOGLE_SERVICES_CLASS_PATH,
          version: GOOGLE_SERVICES_VERSION,
        }
      );
    } else {
      WarningAggregator.addWarningAndroid(
        '@iterable/expo-plugin',
        "Cannot automatically configure project build.gradle if it's not groovy"
      );
    }
 
    return newConfig;
  });
};
 
const withFirebaseInAppBuildGradle: ConfigPlugin<
  ConfigPluginPropsWithDefaults
> = (config) => {
  return withAppBuildGradle(config, (newConfig) => {
    if (newConfig.modResults.language === 'groovy') {
      newConfig.modResults.contents = addApplyPlugin(
        newConfig.modResults.contents,
        GOOGLE_SERVICES_PLUGIN
      );
      newConfig.modResults.contents = addAppDependency(
        newConfig.modResults.contents,
        {
          classpath: FIREBASE_MESSAGING_CLASS_PATH,
        }
      );
      newConfig.modResults.contents = addAppDependency(
        newConfig.modResults.contents,
        {
          classpath: FIREBASE_BOM_CLASS_PATH,
          version: FIREBASE_BOM_VERSION,
          implementation: `platform('${FIREBASE_BOM_CLASS_PATH}:${FIREBASE_BOM_VERSION}')`,
        }
      );
    } else {
      WarningAggregator.addWarningAndroid(
        '@iterable/expo-plugin',
        "Cannot automatically configure app build.gradle if it's not groovy"
      );
    }
    return newConfig;
  });
};
 
/**
 * Add the Google Services dependencies to the project and build.gradle file if
 * they don't exist.
 * @see Step 4.1 https://support.iterable.com/hc/en-us/articles/360045714132-Installing-Iterable-s-React-Native-SDK#step-4-1-set-up-firebase
 */
const withFirebase: ConfigPlugin<ConfigPluginPropsWithDefaults> = (
  config,
  props
) => {
  return withPlugins(config, [
    [withFirebaseInProjectBuildGradle, props],
    [withFirebaseInAppBuildGradle, props],
  ]);
};
 
/**
 * Add the POST_NOTIFICATIONS permission to the AndroidManifest.xml file if it
 * doesn't exist.
 */
const withAppPermissions: ConfigPlugin<ConfigPluginPropsWithDefaults> = (
  config
) => {
  return withAndroidManifest(config, (newConfig) => {
    const androidManifest = newConfig.modResults.manifest;
    if (!androidManifest['uses-permission']) {
      androidManifest['uses-permission'] = [];
    }
    const postPermission = 'android.permission.POST_NOTIFICATIONS';
    const currPostPermission = androidManifest['uses-permission'].find(
      (p) => p.$['android:name'] === postPermission
    );
 
    // Only add the permission if it doesn't exist
    if (!currPostPermission) {
      androidManifest['uses-permission'].push({
        $: { 'android:name': postPermission },
      });
    }
    return newConfig;
  });
};
 
/**
 * Copy `google-services.json`
 * TODO: Add this step to the docs
 */
const withCopyAndroidGoogleServices: ConfigPlugin = (config) => {
  return withDangerousMod(config, [
    'android',
    async (newConfig) => {
      const srcPath = path.resolve(
        newConfig.modRequest.projectRoot,
        newConfig?.android?.googleServicesFile as string
      );
      const destPath = path.resolve(
        newConfig.modRequest.platformProjectRoot,
        DEFAULT_GOOGLE_SERVICES_PATH
      );
 
      try {
        await fs.promises.copyFile(srcPath, destPath);
      } catch {
        throw new Error(
          `Cannot copy google-services.json, because the file ${srcPath} doesn't exist. Please provide a valid path in \`app.json\`.`
        );
      }
      return newConfig;
    },
  ]);
};
 
const withGoogleServices: ConfigPlugin<ConfigPluginPropsWithDefaults> = (
  config,
  props
) => {
  if (!config.android?.googleServicesFile) {
    WarningAggregator.addWarningAndroid(
      '@iterable/expo-plugin',
      'Path to google-services.json is not defined, so push notifications will not be enabled.  To enable push notifications, please specify the `expo.android.googleServicesFile` field in app.json.'
    );
    return config;
  }
 
  return withPlugins(config, [
    [withFirebase, props],
    [withCopyAndroidGoogleServices, props],
  ]);
};
 
export const withAndroidPushNotifications: ConfigPlugin<
  ConfigPluginPropsWithDefaults
> = (config, props) => {
  return withPlugins(config, [
    [withAppPermissions, props],
    [withGoogleServices, props],
  ]);
};
 
export default withAndroidPushNotifications;