All files / src/withPushNotifications withIosPushNotifications.ts

100% Statements 50/50
83.33% Branches 15/18
100% Functions 13/13
100% Lines 50/50

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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213                                                          4x 4x           4x       31x         3x             3x 2x         3x             4x     31x 3x           3x 2x           3x             4x     31x     4x 4x 4x     4x 2x       4x                 4x 12x     4x               4x     31x 6x   6x 1x 1x         5x 5x 5x   5x 5x         5x 5x 5x     5x                     4x 31x                   4x     31x 2x 2x 1x                   2x                   4x 31x                  
import {
  ConfigPlugin,
  withDangerousMod,
  withEntitlementsPlist,
  withInfoPlist,
  withPlugins,
  withPodfile,
  withXcodeProject,
} from 'expo/config-plugins';
 
import { ConfigPluginPropsWithDefaults } from '../withIterable.types';
import {
  NS_ENTITLEMENTS_CONTENT,
  NS_ENTITLEMENTS_FILE_NAME,
  NS_MAIN_FILE_CONTENT,
  NS_MAIN_FILE_NAME,
  NS_PLIST_CONTENT,
  NS_PLIST_FILE_NAME,
  NS_POD,
  NS_TARGET_NAME,
} from './withIosPushNotifications.constants';
import {
  addBuildPhases,
  addNotificationServiceGroup,
  addNotificationServiceTarget,
  createFileIfNoneExists,
  updateBuildSettings,
} from './withIosPushNotifications.utils';
 
const fs = require('fs');
const path = require('path');
 
/**
 * Adds anything that would be added by going to Xcode > Signing & Capabilities
 * and clicking "+ Capability"
 */
const withCapabilities: ConfigPlugin<ConfigPluginPropsWithDefaults> = (
  config,
  props
) => {
  return withEntitlementsPlist(config, (newConfig) => {
    /**
     * Add push notification capabilities to the app.
     * @see Step 3.5.1 of https://support.iterable.com/hc/en-us/articles/360045714132-Installing-Iterable-s-React-Native-SDK#step-3-5-set-up-support-for-push-notifications
     */
    newConfig.modResults['aps-environment'] = props?.appEnvironment;
 
    /**
     * Add the entitlement to allow time-sensitive notifications if
     * `props.enableTimeSensitivePush` not explicitly set to `false`.
     * @see Step 3.5.3 of https://support.iterable.com/hc/en-us/articles/360045714132-Installing-Iterable-s-React-Native-SDK#step-3-5-set-up-support-for-push-notifications
     */
    if (props.enableTimeSensitivePush !== false) {
      newConfig.modResults[
        'com.apple.developer.usernotifications.time-sensitive'
      ] = true;
    }
 
    return newConfig;
  });
};
 
/**
 * Adds any needed background modes to the app.
 */
const withBackgroundModes: ConfigPlugin<ConfigPluginPropsWithDefaults> = (
  config
) => {
  return withInfoPlist(config, (newConfig) => {
    const backgroundModes = newConfig.modResults.UIBackgroundModes || [];
 
    /**
     * Add remote-notification to the background modes if it's not already there
     * @see Step 3.5.2 of https://support.iterable.com/hc/en-us/articles/360045714132-Installing-Iterable-s-React-Native-SDK#step-3-5-set-up-support-for-push-notifications
     */
    if (!backgroundModes.includes('remote-notification')) {
      newConfig.modResults.UIBackgroundModes = [
        ...backgroundModes,
        'remote-notification',
      ];
    }
 
    return newConfig;
  });
};
 
/**
 * Adds the notification service files to the app.
 */
const withAddNSFiles: ConfigPlugin<ConfigPluginPropsWithDefaults> = (
  config
) => {
  return withDangerousMod(config, [
    'ios',
    (newConfig) => {
      const { projectRoot, platformProjectRoot } = newConfig.modRequest;
      const srcPath = path.resolve(projectRoot, platformProjectRoot);
      const newFolderPath = path.resolve(srcPath, NS_TARGET_NAME);
 
      // Create folder if it doesn't exist
      if (!fs.existsSync(newFolderPath)) {
        fs.mkdirSync(newFolderPath);
      }
 
      // Create all required files
      const files = [
        // notification service file
        { name: NS_MAIN_FILE_NAME, content: NS_MAIN_FILE_CONTENT },
        // notification service plist file
        { name: NS_PLIST_FILE_NAME, content: NS_PLIST_CONTENT },
        // notification service entitlements file
        { name: NS_ENTITLEMENTS_FILE_NAME, content: NS_ENTITLEMENTS_CONTENT },
      ];
 
      files.forEach(({ name, content }) => {
        createFileIfNoneExists(path.resolve(newFolderPath, name), content);
      });
 
      return newConfig;
    },
  ]);
};
 
/**
 * Updates the Xcode project to add the notification service target.
 */
const withXcodeUpdates: ConfigPlugin<ConfigPluginPropsWithDefaults> = (
  config
) => {
  return withXcodeProject(config, (newConfig) => {
    const xcodeProject = newConfig.modResults;
 
    if (xcodeProject.pbxTargetByName(NS_TARGET_NAME)) {
      console.log(`${NS_TARGET_NAME} already exists in project. Skipping...`);
      return newConfig;
    }
 
    // Initialize with an empty object if these top-level objects are non-existent.
    // This guarantees that the extension targets will have a destination.
    const objects = xcodeProject.hash.project.objects;
    objects.PBXTargetDependency = objects.PBXTargetDependency || {};
    objects.PBXContainerItemProxy = objects.PBXContainerItemProxy || {};
 
    Eif (!xcodeProject.pbxGroupByName(NS_TARGET_NAME)) {
      const richPushTarget = addNotificationServiceTarget(
        xcodeProject,
        newConfig.ios?.bundleIdentifier as string
      );
 
      addNotificationServiceGroup(xcodeProject);
      updateBuildSettings(xcodeProject);
      addBuildPhases(xcodeProject, richPushTarget.uuid);
    }
 
    return newConfig;
  });
};
 
/**
 * Adds the notification service.
 * This is the equivalent of going to Xcode > File > New > Target... and selecting "Notification Service Extension".
 * @see Step 3.5.7 of https://support.iterable.com/hc/en-us/articles/360045714132-Installing-Iterable-s-React-Native-SDK#step-3-5-set-up-support-for-push-notifications
 */
const withAddNotificationService: ConfigPlugin<
  ConfigPluginPropsWithDefaults
> = (config, props) => {
  return withPlugins(config, [
    [withAddNSFiles, props],
    [withXcodeUpdates, props],
  ]);
};
 
/**
 * Adds the notification service pod to the podfile and ensures it uses our sdk
 * @see Step 3.5.7 of https://support.iterable.com/hc/en-us/articles/360045714132-Installing-Iterable-s-React-Native-SDK#step-3-5-set-up-support-for-push-notifications
 */
const withAddServiceToPodfile: ConfigPlugin<ConfigPluginPropsWithDefaults> = (
  config
) => {
  return withPodfile(config, (newConfig) => {
    const { contents } = newConfig.modResults;
    if (!contents.includes(NS_POD)) {
      newConfig.modResults.contents =
        contents +
        `
target '${NS_TARGET_NAME}' do
    use_frameworks! :linkage => podfile_properties['ios.useFrameworks'].to_sym if podfile_properties['ios.useFrameworks']
    use_frameworks! :linkage => ENV['USE_FRAMEWORKS'].to_sym if ENV['USE_FRAMEWORKS']
    pod '${NS_POD}'
end`;
    }
 
    return newConfig;
  });
};
 
/**
 * Adds a fully configured push notification service to the app unless
 * `props.autoConfigurePushNotifications` is `false`.
 */
export const withIosPushNotifications: ConfigPlugin<
  ConfigPluginPropsWithDefaults
> = (config, props) => {
  return withPlugins(config, [
    [withCapabilities, props],
    [withBackgroundModes, props],
    [withAddNotificationService, props],
    [withAddServiceToPodfile, props],
  ]);
};
 
export default withIosPushNotifications;