diff --git a/packages/react-native/plugin/src/withBreezIOS.ts b/packages/react-native/plugin/src/withBreezIOS.ts index b87c2ec34..13121720f 100644 --- a/packages/react-native/plugin/src/withBreezIOS.ts +++ b/packages/react-native/plugin/src/withBreezIOS.ts @@ -11,6 +11,7 @@ import { import { addExtension } from "./addExtension"; import { withTargetPlist } from "./withExtensionInfoPlist"; import { warnOnce } from "./utils"; +import { withTargetEntitlementsPlist } from "./withExtensionEntitlementsPlist"; const NOTIFICATION_SERVICE_PODS = (targetName: string) => ` target '${targetName}' do @@ -19,40 +20,12 @@ target '${targetName}' do end `; -const NOTIFICATION_SERVICE_ENTITLEMENT = ` - - - - - - -`; - type EASAppExtension = { targetName: string; bundleIdentifier: string; entitlements: Record>; }; -function generateEntitlementsXML( - entitlements: Record>, -): string { - const dictEntries = Object.entries(entitlements) - .map( - ([key, values]) => ` - ${key} - - ${values.map((value) => `${value}`).join("\n ")} - `, - ) - .join("\n"); - - return NOTIFICATION_SERVICE_ENTITLEMENT.replace( - "", - `\n${dictEntries}`, - ); -} - export type NotificationServiceExtensionProps = { apiKey: string; keyService: string; @@ -90,17 +63,6 @@ export function withNotificationServiceExtension(config: ExpoConfig, props: Noti fs.mkdirSync(extensionDir); } - const entitlementsPath = path.join( - extensionDir, - `${targetName}.entitlements`, - ); - if (!fs.existsSync(entitlementsPath)) { - fs.writeFileSync( - entitlementsPath, - generateEntitlementsXML(entitlements), - ); - } - // TODO: Add multiple sources const swiftSource = path.join( __dirname, @@ -120,6 +82,10 @@ export function withNotificationServiceExtension(config: ExpoConfig, props: Noti // Create Target Info Plist config = withTargetPlist(config, { targetName, appGroup, ...props }); + // Create Target Entitlements + config = withTargetEntitlementsPlist(config, { targetName, entitlements }); + + // Add the same entitlements to the main target config = withEntitlementsPlist(config, (config) => { config.modResults = { ...config.modResults, ...entitlements }; return config; @@ -134,7 +100,6 @@ export function withNotificationServiceExtension(config: ExpoConfig, props: Noti }); // Add required Pods for the extension - // TODO: Support dynamic Pods config = withPodfile(config, (config) => { const podFile = config.modResults; if (podFile.contents.includes(targetName)) { diff --git a/packages/react-native/plugin/src/withExtensionEntitlementsPlist.ts b/packages/react-native/plugin/src/withExtensionEntitlementsPlist.ts new file mode 100644 index 000000000..10a9b3050 --- /dev/null +++ b/packages/react-native/plugin/src/withExtensionEntitlementsPlist.ts @@ -0,0 +1,35 @@ +import * as fs from "fs"; +import * as path from "path"; +import type { ExpoConfig } from "@expo/config"; +import type { InfoPlist } from "expo/config-plugins"; +import { withDangerousMod } from "expo/config-plugins"; +import plist from "@expo/plist"; + +type EntitlementsProps = { + targetName: string; + entitlements: Record>; +}; + +export function withTargetEntitlementsPlist(config: ExpoConfig, {targetName, entitlements}: EntitlementsProps): ExpoConfig { + return withDangerousMod(config, [ + "ios", + (config) => { + const extensionDir = path.join( + config.modRequest.platformProjectRoot, + targetName, + ); + if (!fs.existsSync(extensionDir)) { + fs.mkdirSync(extensionDir); + } + const entitlementsPath = path.join(extensionDir, `${targetName}.entitlements`); + + const entitlementsPlist: InfoPlist = { + ...entitlements + }; + + fs.writeFileSync(entitlementsPath, plist.build(entitlementsPlist)); + + return config; + }, + ]); +}