Skip to content

Commit

Permalink
feat(ios):) add notification service extension target for image suppo…
Browse files Browse the repository at this point in the history
…rt in notifications
  • Loading branch information
emirmuminoglu committed Dec 15, 2024
1 parent 3623a50 commit 95e52ed
Show file tree
Hide file tree
Showing 9 changed files with 433 additions and 7 deletions.
25 changes: 18 additions & 7 deletions packages/messaging/plugin/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
import { ConfigPlugin, withPlugins, createRunOncePlugin } from '@expo/config-plugins';
import { ConfigPlugin, createRunOncePlugin } from '@expo/config-plugins';
import { withExpoPluginFirebaseNotification } from './android';
import {
withAppEnvironment,
withNotificationServiceExtension,
withRNFirebaseXcodeProject,
} from './ios';
import { PluginProps, withEasManagedCredentials } from './ios/setupNotificationServiceExtension';

/**
* A config plugin for configuring `@react-native-firebase/app`
*/
const withRnFirebaseApp: ConfigPlugin = config => {
return withPlugins(config, [
// iOS
const withRnFirebaseApp: ConfigPlugin<PluginProps> = (config, props) => {
// iOS
if (props.installNSE) {
config = withAppEnvironment(config, props);
config = withNotificationServiceExtension(config, props);
config = withRNFirebaseXcodeProject(config, props);
config = withEasManagedCredentials(config, props);
}

// Android
withExpoPluginFirebaseNotification,
]);
// Android
config = withExpoPluginFirebaseNotification(config);
return config;
};

const pak = require('@react-native-firebase/messaging/package.json');
Expand Down
27 changes: 27 additions & 0 deletions packages/messaging/plugin/src/ios/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export const IPHONEOS_DEPLOYMENT_TARGET = '11.0';
export const TARGETED_DEVICE_FAMILY = `"1,2"`;

export const NSE_PODFILE_SNIPPET = `
target 'RNFirebaseNotificationServiceExtension' do
use_frameworks! :linkage => podfile_properties['ios.useFrameworks'].to_sym if podfile_properties['ios.useFrameworks']
pod 'GoogleUtilities'
pod 'Firebase/Messaging'
end
`;

export const NSE_PODFILE_REGEX = /target 'RNFirebaseNotificationServiceExtension'/;

export const GROUP_IDENTIFIER_TEMPLATE_REGEX = /{{GROUP_IDENTIFIER}}/gm;
export const BUNDLE_SHORT_VERSION_TEMPLATE_REGEX = /{{BUNDLE_SHORT_VERSION}}/gm;
export const BUNDLE_VERSION_TEMPLATE_REGEX = /{{BUNDLE_VERSION}}/gm;

export const DEFAULT_BUNDLE_VERSION = '1';
export const DEFAULT_BUNDLE_SHORT_VERSION = '1.0';

export const NSE_TARGET_NAME = 'RNFirebaseNotificationServiceExtension';
export const NSE_SOURCE_FILE = 'NotificationService.m';
export const NSE_EXT_FILES = [
'NotificationService.h',
`${NSE_TARGET_NAME}.entitlements`,
`${NSE_TARGET_NAME}-Info.plist`,
];
38 changes: 38 additions & 0 deletions packages/messaging/plugin/src/ios/fsutils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import * as fs from 'fs';

export async function readFile(path: string): Promise<string> {
return new Promise<string>((resolve, reject) => {
fs.readFile(path, 'utf8', (err, data) => {
if (err || !data) {
// eslint-disable-next-line no-console
console.error("Couldn't read file:" + path);
reject(err);
return;
}
resolve(data);
});
});
}

export async function writeFile(path: string, contents: string): Promise<void> {
return new Promise<void>((resolve, reject) => {
fs.writeFile(path, contents, 'utf8', err => {
if (err) {
// eslint-disable-next-line no-console
console.error("Couldn't write file:" + path);
reject(err);
return;
}
resolve();
});
});
}

export async function copyFile(path1: string, path2: string): Promise<void> {
const fileContents = await readFile(path1);
await writeFile(path2, fileContents);
}

export function dirExists(path: string): boolean {
return fs.existsSync(path);
}
13 changes: 13 additions & 0 deletions packages/messaging/plugin/src/ios/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {
withNotificationServiceExtension,
withRNFirebaseXcodeProject,
withAppEnvironment,
withEasManagedCredentials,
} from './setupNotificationServiceExtension';

export {
withNotificationServiceExtension,
withRNFirebaseXcodeProject,
withAppEnvironment,
withEasManagedCredentials,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#import <UserNotifications/UserNotifications.h>

@interface NotificationService : UNNotificationServiceExtension

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#import "NotificationService.h"
#import "FirebaseMessaging.h"

@interface NotificationService ()

@property (nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver);
@property (nonatomic, strong) UNNotificationRequest *receivedRequest;
@property (nonatomic, strong) UNMutableNotificationContent *bestAttemptContent;

@end

@implementation NotificationService

- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
self.contentHandler = contentHandler;
self.bestAttemptContent = [request.content mutableCopy];

[[FIRMessaging extensionHelper] populateNotificationContent:self.bestAttemptContent withContentHandler:contentHandler];
}

- (void)serviceExtensionTimeWillExpire {
self.contentHandler(self.bestAttemptContent);
}

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleDisplayName</key>
<string>RNFirebaseNotificationServiceExtension</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
<key>CFBundleShortVersionString</key>
<string>{{BUNDLE_SHORT_VERSION}}</string>
<key>CFBundleVersion</key>
<string>{{BUNDLE_VERSION}}</string>
<key>NSExtension</key>
<dict>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.usernotifications.service</string>
<key>NSExtensionPrincipalClass</key>
<string>NotificationService</string>
</dict>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.application-groups</key>
<array>
<string>{{GROUP_IDENTIFIER}}</string>
</array>
</dict>
</plist>
Loading

0 comments on commit 95e52ed

Please sign in to comment.