onesignal-expo-plugin 1.1.2 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,7 +1,18 @@
1
1
  # Changelog
2
2
  All notable changes to this project will be documented in this file.
3
3
 
4
+ ## November 2022
5
+ ### `1.3.0` - 11/30/22
6
+ #### Changes
7
+ - Add `iosNSEFilePath` configuration options to the plugin, which allows an app to specify a custom iOS Notification Service Extension.
8
+
4
9
  ## October 2022
10
+ ### `1.2.0` - 10/25/22
11
+ #### Changes
12
+ - Add `smallIcons` and `largeIcons` configuration options to the plugin, which allows an app to specify large and small notification icons to package within the Android build.
13
+ - Bump jpeg-js from 0.4.3 to 0.4.4
14
+ - Bump simple-plist from 1.3.0 to 1.3.1
15
+
5
16
  ### `1.1.2` - 10/04/22
6
17
  #### Fixes
7
18
  - Specify `use_frameworks` on `OneSignalNotificationServiceExtension` target in iOS Podfile when applicable (i.e. the app target also specifies `use_frameworks`).
package/README.md CHANGED
@@ -81,9 +81,12 @@ You can pass props to the plugin config object to configure:
81
81
 
82
82
  | Plugin Prop | | |
83
83
  |--------------------------|----------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
84
- | `mode` | **required** | Used to configure [APNs environment](https://developer.apple.com/documentation/bundleresources/entitlements/aps-environment) entitlement. `"development"` or `"production"` |
84
+ | `mode` | **required** | Used to configure [APNs environment](https://developer.apple.com/documentation/bundleresources/entitlements/aps-environment) entitlement. `"development"` or `"production"` |
85
85
  | `devTeam` | optional | Used to configure Apple Team ID. You can find your Apple Team ID by running `expo credentials:manager` e.g: `"91SW8A37CR"` |
86
86
  | `iPhoneDeploymentTarget` | optional | Target `IPHONEOS_DEPLOYMENT_TARGET` value to be used when adding the iOS [NSE](https://documentation.onesignal.com/docs/service-extensions). A deployment target is nothing more than the minimum version of the operating system the application can run on. This value should match the value in your Podfile e.g: `"12.0"`. |
87
+ | `smallIcons` | optional | An array of local paths to small notification icons for Android. Image should be white, transparent, and 96x96 in size. Input images will be automatically scaled down and placed in the appropriate resource folders. e.g: `["./assets/ic_stat_onesignal_default.png"]`. See https://documentation.onesignal.com/docs/customize-notification-icons#small-notification-icons. |
88
+ | `largeIcons` | optional | An array of local paths to large notification icons for Android. Image should be white, transparent, and 256x256 in size. e.g: `["./assets/ic_onesignal_large_icon_default.png"]`. See https://documentation.onesignal.com/docs/customize-notification-icons#large-notification-icons. |
89
+ | `iosNSEFilePath` | optional | The local path to a custom Notification Service Extension (NSE), written in Objective-C. The NSE will typically start as a copy of the [default NSE](https://github.com/OneSignal/onesignal-expo-plugin/blob/main/support/serviceExtensionFiles/NotificationService.m), then altered to support any custom logic required. e.g: `"./assets/NotificationService.m"`. |
87
90
 
88
91
  ### OneSignal App ID
89
92
  Add your OneSignal App ID to your [Expo constants via the `extra` param](https://docs.expo.dev/versions/latest/config/app/):
@@ -115,9 +118,10 @@ OneSignal.setAppId("YOUR-ONESIGNAL-APP-ID");
115
118
  In your configuration file, make sure you set:
116
119
 
117
120
  | Property | Details |
118
- |-------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
121
+ |-------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
119
122
  | `version` | Your app version. Corresponds to `CFBundleShortVersionString` on iOS. This value will be used in your NSE* target's plist file. |
120
123
  | `ios.buildNumber` | Build number for your iOS standalone app. Corresponds to `CFBundleVersion` and must match Apple's specified format. This value will be used in your NSE* target's plist file. |
124
+ | `ios.bundleIdentifier` | Bundle identifier for your iOS standalone app. Corresponds to `CFBundleIdentifier`. This value will be used in your NSE* target's plist and entitlements file. |
121
125
 
122
126
  \* NSE = Notification Service Extension. Learn more about the NSE [here](https://documentation.onesignal.com/docs/service-extensions).
123
127
 
@@ -5,9 +5,85 @@
5
5
  */
6
6
  Object.defineProperty(exports, "__esModule", { value: true });
7
7
  exports.withOneSignalAndroid = void 0;
8
+ const config_plugins_1 = require("@expo/config-plugins");
9
+ const image_utils_1 = require("@expo/image-utils");
10
+ const OneSignalLog_1 = require("../support/OneSignalLog");
11
+ const path_1 = require("path");
12
+ const fs_1 = require("fs");
13
+ const RESOURCE_ROOT_PATH = 'android/app/src/main/res/';
14
+ // The name of each small icon folder resource, and the icon size for that folder.
15
+ const SMALL_ICON_DIRS_TO_SIZE = {
16
+ 'drawable-mdpi': 24,
17
+ 'drawable-hdpi': 36,
18
+ 'drawable-xhdpi': 48,
19
+ 'drawable-xxhdpi': 72,
20
+ 'drawable-xxxhdpi': 96
21
+ };
22
+ // The name of each large icon folder resource, and the icon size for that folder.
23
+ const LARGE_ICON_DIRS_TO_SIZE = {
24
+ 'drawable-xxxhdpi': 256
25
+ };
26
+ const withSmallIcons = (config, onesignalProps) => {
27
+ var _a;
28
+ if (!onesignalProps.smallIcons && !((_a = config.notification) === null || _a === void 0 ? void 0 : _a.icon)) {
29
+ return config;
30
+ }
31
+ // we are modifying the android build (adding files) without a base mod
32
+ return (0, config_plugins_1.withDangerousMod)(config, [
33
+ 'android',
34
+ async (config) => {
35
+ var _a;
36
+ if ((_a = config.notification) === null || _a === void 0 ? void 0 : _a.icon) {
37
+ await saveIconAsync(config.notification.icon, config.modRequest.projectRoot, SMALL_ICON_DIRS_TO_SIZE);
38
+ }
39
+ if (onesignalProps.smallIcons) {
40
+ await saveIconsArrayAsync(config.modRequest.projectRoot, onesignalProps.smallIcons, SMALL_ICON_DIRS_TO_SIZE);
41
+ }
42
+ return config;
43
+ },
44
+ ]);
45
+ };
46
+ const withLargeIcons = (config, onesignalProps) => {
47
+ if (!onesignalProps.largeIcons) {
48
+ return config;
49
+ }
50
+ // we are modifying the android build (adding files) without a base mod
51
+ return (0, config_plugins_1.withDangerousMod)(config, [
52
+ 'android',
53
+ async (config) => {
54
+ if (onesignalProps.largeIcons) {
55
+ await saveIconsArrayAsync(config.modRequest.projectRoot, onesignalProps.largeIcons, LARGE_ICON_DIRS_TO_SIZE);
56
+ }
57
+ return config;
58
+ },
59
+ ]);
60
+ };
61
+ async function saveIconsArrayAsync(projectRoot, icons, dirsToSize) {
62
+ for (const icon of icons) {
63
+ await saveIconAsync(icon, projectRoot, dirsToSize);
64
+ }
65
+ }
66
+ async function saveIconAsync(icon, projectRoot, dirsToSize) {
67
+ const name = (0, path_1.parse)(icon).name;
68
+ OneSignalLog_1.OneSignalLog.log("Saving icon " + icon + " as drawable resource " + name);
69
+ for (const iconResourceDir in dirsToSize) {
70
+ const path = (0, path_1.resolve)(projectRoot, RESOURCE_ROOT_PATH, iconResourceDir);
71
+ if (!(0, fs_1.existsSync)(path)) {
72
+ (0, fs_1.mkdirSync)(path, { recursive: true });
73
+ }
74
+ const resizedIcon = (await (0, image_utils_1.generateImageAsync)({ projectRoot, cacheType: 'onesignal-icon' }, {
75
+ src: icon,
76
+ width: dirsToSize[iconResourceDir],
77
+ height: dirsToSize[iconResourceDir],
78
+ resizeMode: 'cover',
79
+ backgroundColor: 'transparent',
80
+ })).source;
81
+ (0, fs_1.writeFileSync)((0, path_1.resolve)(path, name + '.png'), resizedIcon);
82
+ }
83
+ }
8
84
  const withOneSignalAndroid = (config, props) => {
9
- //commented out until https://github.com/expo/eas-cli/issues/1226 is confirmed fixed
10
- //OneSignalLog.log("No specific actions required for OneSignal on Android...");
85
+ config = withSmallIcons(config, props);
86
+ config = withLargeIcons(config, props);
11
87
  return config;
12
88
  };
13
89
  exports.withOneSignalAndroid = withOneSignalAndroid;
@@ -105,7 +105,8 @@ const withOneSignalNSE = (config, onesignalProps) => {
105
105
  bundleVersion: (_b = props.ios) === null || _b === void 0 ? void 0 : _b.buildNumber,
106
106
  bundleShortVersion: props === null || props === void 0 ? void 0 : props.version,
107
107
  mode: onesignalProps === null || onesignalProps === void 0 ? void 0 : onesignalProps.mode,
108
- iPhoneDeploymentTarget: onesignalProps === null || onesignalProps === void 0 ? void 0 : onesignalProps.iPhoneDeploymentTarget
108
+ iPhoneDeploymentTarget: onesignalProps === null || onesignalProps === void 0 ? void 0 : onesignalProps.iPhoneDeploymentTarget,
109
+ iosNSEFilePath: onesignalProps.iosNSEFilePath
109
110
  };
110
111
  // support for monorepos where node_modules can be up to 5 parents
111
112
  // above the project directory.
@@ -133,13 +134,13 @@ const withOneSignalIos = (config, props) => {
133
134
  };
134
135
  exports.withOneSignalIos = withOneSignalIos;
135
136
  function xcodeProjectAddNse(appName, options, sourceDir) {
136
- const { iosPath, devTeam, bundleIdentifier, bundleVersion, bundleShortVersion, iPhoneDeploymentTarget } = options;
137
+ const { iosPath, devTeam, bundleIdentifier, bundleVersion, bundleShortVersion, iPhoneDeploymentTarget, iosNSEFilePath } = options;
137
138
  // not awaiting in order to not block main thread
138
139
  (0, updatePodfile_1.updatePodfile)(iosPath).catch(err => { OneSignalLog_1.OneSignalLog.error(err); });
139
140
  const projPath = `${iosPath}/${appName}.xcodeproj/project.pbxproj`;
141
+ const sourceFile = "NotificationService.m";
140
142
  const extFiles = [
141
143
  "NotificationService.h",
142
- "NotificationService.m",
143
144
  `${iosConstants_1.NSE_TARGET_NAME}.entitlements`,
144
145
  `${iosConstants_1.NSE_TARGET_NAME}-Info.plist`
145
146
  ];
@@ -156,13 +157,17 @@ function xcodeProjectAddNse(appName, options, sourceDir) {
156
157
  const targetFile = `${iosPath}/${iosConstants_1.NSE_TARGET_NAME}/${extFile}`;
157
158
  await FileManager_1.FileManager.copyFile(`${sourceDir}${extFile}`, targetFile);
158
159
  }
160
+ // Copy NSE source file either from configuration-provided location, falling back to the default one.
161
+ const sourcePath = iosNSEFilePath !== null && iosNSEFilePath !== void 0 ? iosNSEFilePath : `${sourceDir}${sourceFile}`;
162
+ const targetFile = `${iosPath}/${iosConstants_1.NSE_TARGET_NAME}/${sourceFile}`;
163
+ await FileManager_1.FileManager.copyFile(`${sourcePath}`, targetFile);
159
164
  /* MODIFY COPIED EXTENSION FILES */
160
165
  const nseUpdater = new NseUpdaterManager_1.default(iosPath);
161
166
  await nseUpdater.updateNSEEntitlements(`group.${bundleIdentifier}.onesignal`);
162
167
  await nseUpdater.updateNSEBundleVersion(bundleVersion !== null && bundleVersion !== void 0 ? bundleVersion : iosConstants_1.DEFAULT_BUNDLE_VERSION);
163
168
  await nseUpdater.updateNSEBundleShortVersion(bundleShortVersion !== null && bundleShortVersion !== void 0 ? bundleShortVersion : iosConstants_1.DEFAULT_BUNDLE_SHORT_VERSION);
164
169
  // Create new PBXGroup for the extension
165
- const extGroup = xcodeProject.addPbxGroup(extFiles, iosConstants_1.NSE_TARGET_NAME, iosConstants_1.NSE_TARGET_NAME);
170
+ const extGroup = xcodeProject.addPbxGroup([...extFiles, sourceFile], iosConstants_1.NSE_TARGET_NAME, iosConstants_1.NSE_TARGET_NAME);
166
171
  // Add the new PBXGroup to the top level group. This makes the
167
172
  // files / folder appear in the file explorer in Xcode.
168
173
  const groups = xcodeProject.hash.project.objects["PBXGroup"];
@@ -3,15 +3,115 @@
3
3
  * @see https://documentation.onesignal.com/docs/react-native-sdk-setup#step-4-install-for-ios-using-cocoapods-for-ios-apps
4
4
  */
5
5
 
6
- import { ConfigPlugin } from '@expo/config-plugins';
6
+ import { ConfigPlugin, withDangerousMod } from '@expo/config-plugins';
7
+ import { generateImageAsync } from '@expo/image-utils';
7
8
  import { OneSignalLog } from '../support/OneSignalLog';
8
9
  import { OneSignalPluginProps } from '../types/types';
10
+ import { resolve, parse } from 'path';
11
+ import { existsSync, mkdirSync, writeFileSync } from 'fs';
12
+
13
+ const RESOURCE_ROOT_PATH = 'android/app/src/main/res/';
14
+
15
+ // The name of each small icon folder resource, and the icon size for that folder.
16
+ const SMALL_ICON_DIRS_TO_SIZE: { [name: string]: number } = {
17
+ 'drawable-mdpi': 24,
18
+ 'drawable-hdpi': 36,
19
+ 'drawable-xhdpi': 48,
20
+ 'drawable-xxhdpi': 72,
21
+ 'drawable-xxxhdpi': 96
22
+ };
23
+
24
+ // The name of each large icon folder resource, and the icon size for that folder.
25
+ const LARGE_ICON_DIRS_TO_SIZE: { [name: string]: number } = {
26
+ 'drawable-xxxhdpi': 256
27
+ };
28
+
29
+ const withSmallIcons: ConfigPlugin<OneSignalPluginProps> = (
30
+ config,
31
+ onesignalProps
32
+ ) => {
33
+
34
+ if(!onesignalProps.smallIcons && !config.notification?.icon) {
35
+ return config
36
+ }
37
+
38
+ // we are modifying the android build (adding files) without a base mod
39
+ return withDangerousMod(config, [
40
+ 'android',
41
+ async (config) => {
42
+ if(config.notification?.icon) {
43
+ await saveIconAsync(config.notification.icon, config.modRequest.projectRoot, SMALL_ICON_DIRS_TO_SIZE)
44
+ }
45
+
46
+ if(onesignalProps.smallIcons) {
47
+ await saveIconsArrayAsync(config.modRequest.projectRoot, onesignalProps.smallIcons, SMALL_ICON_DIRS_TO_SIZE);
48
+ }
49
+ return config;
50
+ },
51
+ ]);
52
+ };
53
+
54
+ const withLargeIcons: ConfigPlugin<OneSignalPluginProps> = (
55
+ config,
56
+ onesignalProps
57
+ ) => {
58
+
59
+ if(!onesignalProps.largeIcons) {
60
+ return config
61
+ }
62
+
63
+ // we are modifying the android build (adding files) without a base mod
64
+ return withDangerousMod(config, [
65
+ 'android',
66
+ async (config) => {
67
+ if(onesignalProps.largeIcons) {
68
+ await saveIconsArrayAsync(config.modRequest.projectRoot, onesignalProps.largeIcons, LARGE_ICON_DIRS_TO_SIZE);
69
+ }
70
+ return config;
71
+ },
72
+ ]);
73
+ };
74
+
75
+ async function saveIconsArrayAsync(projectRoot: string, icons: string[], dirsToSize: { [name: string]: number }) {
76
+ for(const icon of icons) {
77
+ await saveIconAsync(icon, projectRoot, dirsToSize);
78
+ }
79
+ }
80
+
81
+ async function saveIconAsync(icon: string, projectRoot: string, dirsToSize: { [name: string]: number }) {
82
+ const name = parse(icon).name;
83
+
84
+ OneSignalLog.log("Saving icon " + icon + " as drawable resource " + name);
85
+
86
+ for(const iconResourceDir in dirsToSize) {
87
+ const path = resolve(projectRoot, RESOURCE_ROOT_PATH, iconResourceDir);
88
+
89
+ if(!existsSync(path)) {
90
+ mkdirSync(path, { recursive: true });
91
+ }
92
+
93
+ const resizedIcon = (
94
+ await generateImageAsync(
95
+ { projectRoot, cacheType: 'onesignal-icon' },
96
+ {
97
+ src: icon,
98
+ width: dirsToSize[iconResourceDir],
99
+ height: dirsToSize[iconResourceDir],
100
+ resizeMode: 'cover',
101
+ backgroundColor: 'transparent',
102
+ }
103
+ )
104
+ ).source;
105
+
106
+ writeFileSync(resolve(path, name + '.png'), resizedIcon);
107
+ }
108
+ }
9
109
 
10
110
  export const withOneSignalAndroid: ConfigPlugin<OneSignalPluginProps> = (
11
111
  config,
12
112
  props,
13
113
  ) => {
14
- //commented out until https://github.com/expo/eas-cli/issues/1226 is confirmed fixed
15
- //OneSignalLog.log("No specific actions required for OneSignal on Android...");
114
+ config = withSmallIcons(config, props);
115
+ config = withLargeIcons(config, props);
16
116
  return config;
17
117
  };
@@ -102,7 +102,8 @@ const withOneSignalNSE: ConfigPlugin<OneSignalPluginProps> = (config, onesignalP
102
102
  bundleVersion: props.ios?.buildNumber,
103
103
  bundleShortVersion: props?.version,
104
104
  mode: onesignalProps?.mode,
105
- iPhoneDeploymentTarget: onesignalProps?.iPhoneDeploymentTarget
105
+ iPhoneDeploymentTarget: onesignalProps?.iPhoneDeploymentTarget,
106
+ iosNSEFilePath: onesignalProps.iosNSEFilePath
106
107
  };
107
108
 
108
109
  // support for monorepos where node_modules can be up to 5 parents
@@ -145,16 +146,16 @@ export function xcodeProjectAddNse(
145
146
  options: PluginOptions,
146
147
  sourceDir: string
147
148
  ): void {
148
- const { iosPath, devTeam, bundleIdentifier, bundleVersion, bundleShortVersion, iPhoneDeploymentTarget } = options;
149
+ const { iosPath, devTeam, bundleIdentifier, bundleVersion, bundleShortVersion, iPhoneDeploymentTarget, iosNSEFilePath } = options;
149
150
 
150
151
  // not awaiting in order to not block main thread
151
152
  updatePodfile(iosPath).catch(err => { OneSignalLog.error(err) });
152
153
 
153
154
  const projPath = `${iosPath}/${appName}.xcodeproj/project.pbxproj`;
154
155
 
156
+ const sourceFile = "NotificationService.m"
155
157
  const extFiles = [
156
158
  "NotificationService.h",
157
- "NotificationService.m",
158
159
  `${NSE_TARGET_NAME}.entitlements`,
159
160
  `${NSE_TARGET_NAME}-Info.plist`
160
161
  ];
@@ -176,6 +177,11 @@ export function xcodeProjectAddNse(
176
177
  await FileManager.copyFile(`${sourceDir}${extFile}`, targetFile);
177
178
  }
178
179
 
180
+ // Copy NSE source file either from configuration-provided location, falling back to the default one.
181
+ const sourcePath = iosNSEFilePath ?? `${sourceDir}${sourceFile}`
182
+ const targetFile = `${iosPath}/${NSE_TARGET_NAME}/${sourceFile}`;
183
+ await FileManager.copyFile(`${sourcePath}`, targetFile);
184
+
179
185
  /* MODIFY COPIED EXTENSION FILES */
180
186
  const nseUpdater = new NseUpdaterManager(iosPath);
181
187
  await nseUpdater.updateNSEEntitlements(`group.${bundleIdentifier}.onesignal`)
@@ -183,7 +189,7 @@ export function xcodeProjectAddNse(
183
189
  await nseUpdater.updateNSEBundleShortVersion(bundleShortVersion ?? DEFAULT_BUNDLE_SHORT_VERSION);
184
190
 
185
191
  // Create new PBXGroup for the extension
186
- const extGroup = xcodeProject.addPbxGroup(extFiles, NSE_TARGET_NAME, NSE_TARGET_NAME);
192
+ const extGroup = xcodeProject.addPbxGroup([...extFiles, sourceFile], NSE_TARGET_NAME, NSE_TARGET_NAME);
187
193
 
188
194
  // Add the new PBXGroup to the top level group. This makes the
189
195
  // files / folder appear in the file explorer in Xcode.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "onesignal-expo-plugin",
3
- "version": "1.1.2",
3
+ "version": "1.3.0",
4
4
  "description": "The OneSignal Expo plugin allows you to use OneSignal without leaving the managed workflow. Developed in collaboration with SweetGreen.",
5
5
  "main": "./app.plugin.js",
6
6
  "scripts": {
@@ -41,6 +41,7 @@
41
41
  "typescript": "^4.5.5"
42
42
  },
43
43
  "dependencies": {
44
- "xcode": "^3.0.1"
44
+ "xcode": "^3.0.1",
45
+ "@expo/image-utils": "^0.3.22"
45
46
  }
46
47
  }
package/types/types.ts CHANGED
@@ -3,11 +3,38 @@
3
3
  */
4
4
  export type OneSignalPluginProps = {
5
5
  /**
6
- * (iOS only) Environment name and bundle identifier
6
+ * (required) Used to configure APNs environment entitlement. "development" or "production"
7
7
  */
8
8
  mode: Mode;
9
+
10
+ /**
11
+ * (optional) Used to configure Apple Team ID. You can find your Apple Team ID by running expo credentials:manager e.g: "91SW8A37CR"
12
+ */
9
13
  devTeam: string;
14
+
15
+ /**
16
+ * (optional) Target IPHONEOS_DEPLOYMENT_TARGET value to be used when adding the iOS NSE. A deployment target is nothing more than
17
+ * the minimum version of the operating system the application can run on. This value should match the value in your Podfile e.g: "12.0".
18
+ */
10
19
  iPhoneDeploymentTarget: string;
20
+
21
+ /**
22
+ * (optional) The small notification icons for Android. Images will be automatically scaled up/down, the recommended size
23
+ * is 96x96 to always scale down.
24
+ */
25
+ smallIcons?: string[];
26
+
27
+ /**
28
+ * (optional) The large notification icons for Android. Images will be automatically scaled up/down to 256x256.
29
+ */
30
+ largeIcons?: string[];
31
+
32
+ /**
33
+ * (optional) The local path to a custom Notification Service Extension (NSE), written in Objective-C. The NSE will typically start as a copy
34
+ * of the default NSE found at (support/serviceExtensionFiles/NotificationService.m, then altered to support any custom
35
+ * logic required.
36
+ */
37
+ iosNSEFilePath?: string;
11
38
  };
12
39
 
13
40
  /**
@@ -21,7 +48,8 @@ export type PluginOptions = {
21
48
  bundleVersion?: string,
22
49
  bundleShortVersion?: string,
23
50
  bundleIdentifier?: string,
24
- iPhoneDeploymentTarget?: string
51
+ iPhoneDeploymentTarget?: string,
52
+ iosNSEFilePath?: string
25
53
  }
26
54
 
27
55
  export enum Mode {