Skip to content

Commit

Permalink
custom service file path
Browse files Browse the repository at this point in the history
  • Loading branch information
LunatiqueCoder committed Oct 17, 2024
1 parent 722626c commit 947a582
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 10 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ const notifeeOptions: TExpoNotifeeRemote = {
* @link https://developer.apple.com/documentation/bundleresources/entitlements/com_apple_security_application-groups
*/
appGroups: string[];
/**
* @description
* Use a custom relative (from project root) path for the NotifeeNotificationService.
* - You can adapt `expo-notifee-plugin/ios/NotifeeNotificationService.swift`
* - Warning! It should be named `NotifeeNotificationService`! Doesn't matter if you use
* Swift or Objective-C!
*
* @examples
* - src/notifications/NotifeeNotificationService.swift
* - src/notifications/NotifeeNotificationService.m
*/
customNotificationServicePath?: string;
developmentTeam: string;
/**
* An array containing the sound file names (including file extensions)
Expand Down
5 changes: 5 additions & 0 deletions ios/NotifeeNSE/NotifeeNotificationService.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#import <UserNotifications/UserNotifications.h>

@interface NotifeeNotificationService : UNNotificationServiceExtension

@end
65 changes: 57 additions & 8 deletions src/helper-plugins/withExtensionViewController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,73 @@ import path from 'path';
import { EXTENSION_CONTROLLER_NAME, EXTENSION_NAME, EXTENSION_PATH } from '../constants';
import { TExpoNotifeeRemote } from '../types';

const withExtensionViewController: ConfigPlugin<TExpoNotifeeRemote> = config => {
const withExtensionViewController: ConfigPlugin<TExpoNotifeeRemote> = (config, pluginParams) => {
return withXcodeProject(config, newConfig => {
const controllerPath = path.join(
newConfig.modRequest.projectRoot,
EXTENSION_PATH,
EXTENSION_NAME,
`${EXTENSION_CONTROLLER_NAME}.swift`
);
const { customNotificationServicePath } = pluginParams;
let controllerPath = '';

if (customNotificationServicePath) {
controllerPath = path.join(newConfig.modRequest.projectRoot, customNotificationServicePath);

if (!fs.existsSync(customNotificationServicePath)) {
throw new Error(
`
### EXPO-NOTIFEE-PLUGIN ERROR: Incorrect path for NotifeeNotificationService file! File does not exist:
### EXPO-NOTIFEE-PLUGIN ERROR: ${controllerPath}`
);
}

if (
!customNotificationServicePath.endsWith(`${EXTENSION_CONTROLLER_NAME}.m`) &&
!customNotificationServicePath.endsWith(`${EXTENSION_CONTROLLER_NAME}.swift`)
) {
throw new Error(
`
### EXPO-NOTIFEE-PLUGIN ERROR: Incorrect path for NotifeeNotificationService file! Should end with:
- NotifeeNotificationService.m
OR
- NotifeeNotificationService.swift
`
);
}
} else {
// No custom file path was found, use default
controllerPath = path.join(
newConfig.modRequest.projectRoot,
EXTENSION_PATH,
EXTENSION_NAME,
`${EXTENSION_CONTROLLER_NAME}.swift`
);
}

const isObjectiveC = controllerPath.endsWith('.m');

const targetPath = path.join(
newConfig.modRequest.platformProjectRoot,
EXTENSION_NAME,
`${EXTENSION_CONTROLLER_NAME}.swift`
`${EXTENSION_CONTROLLER_NAME}.${isObjectiveC ? 'm' : 'swift'}`
);

fs.mkdirSync(path.dirname(targetPath), { recursive: true });
fs.copyFileSync(controllerPath, targetPath);

// Include header file for Objective-C
if (isObjectiveC) {
const headerPath = path.join(
newConfig.modRequest.projectRoot,
EXTENSION_PATH,
EXTENSION_NAME,
`${EXTENSION_CONTROLLER_NAME}.h`
);

const headerTargetPath = path.join(
newConfig.modRequest.platformProjectRoot,
EXTENSION_NAME,
`${EXTENSION_CONTROLLER_NAME}.h`
);
fs.copyFileSync(headerPath, headerTargetPath);
}

return newConfig;
});
};
Expand Down
12 changes: 12 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ export type TExpoNotifeeRemote = {
* @link https://developer.apple.com/documentation/bundleresources/entitlements/com_apple_security_application-groups
*/
appGroups?: string[];
/**
* @description
* Use a custom relative (from project root) path for the NotifeeNotificationService.
* - You can adapt `expo-notifee-plugin/ios/NotifeeNotificationService.swift`
* - Warning! It should be named `NotifeeNotificationService`! Doesn't matter if you use
* Swift or Objective-C!
*
* @examples
* - src/notifications/NotifeeNotificationService.swift
* - src/notifications/NotifeeNotificationService.m
*/
customNotificationServicePath?: string;
developmentTeam: string;
/**
* An array containing the sound file names (including file extensions)
Expand Down
4 changes: 2 additions & 2 deletions src/withNotificationsExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ const initialParams: TExpoNotifeeRemote = {
};

const withNotificationsExtension: ConfigPlugin<TExpoNotifeeRemote> = (config, params = initialParams) => {
const { appGroups, developmentTeam, soundFiles = [], soundFilesPath = 'assets' } = params;
const { developmentTeam, soundFiles = [], soundFilesPath = 'assets', ...paramsWithDefaults } = params;

if (!developmentTeam) {
throw new Error('### NotifeeRemoteExtension Plugin Error: You need to provide Development Team');
}

const guardedParams: TExpoNotifeeRemote = {
appGroups,
developmentTeam,
soundFilesPath,
soundFiles,
...paramsWithDefaults,
};

return withPlugins(config, [
Expand Down

0 comments on commit 947a582

Please sign in to comment.