onesignal-expo-plugin 2.0.0 → 2.0.1

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/README.md CHANGED
@@ -87,10 +87,11 @@ You can pass props to the plugin config object to configure:
87
87
  | `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"`. |
88
88
  | `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. |
89
89
  | `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. |
90
+ | `smallIconAccentColor` | optional | The accent color to use for notification icons on Android. Must be a valid hex value, e.g: `"#FF0000"` |
90
91
  | `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"`. |
91
92
 
92
93
  ### OneSignal App ID
93
- Add your OneSignal App ID to your [Expo constants via the `extra` param](https://docs.expo.dev/versions/latest/config/app/):
94
+ Add your OneSignal App ID to your [Expo constants via the `extra` param](https://docs.expo.dev/versions/latest/config/app/#extra):
94
95
 
95
96
  **Example:**
96
97
  ```json
@@ -101,18 +102,18 @@ Add your OneSignal App ID to your [Expo constants via the `extra` param](https:/
101
102
  }
102
103
  ```
103
104
 
104
- You can then access the value to pass to the `setAppId` function:
105
+ You can then access the value to pass to the `initialize` function:
105
106
 
106
107
  ```js
107
108
  import OneSignal from 'react-native-onesignal';
108
109
  import Constants from "expo-constants";
109
- OneSignal.setAppId(Constants.manifest.extra.oneSignalAppId);
110
+ OneSignal.initialize(Constants.expoConfig.extra.oneSignalAppId);
110
111
  ```
111
112
 
112
113
  Alternatively, pass the app ID directly to the function:
113
114
 
114
115
  ```js
115
- OneSignal.setAppId("YOUR-ONESIGNAL-APP-ID");
116
+ OneSignal.initialize("YOUR-ONESIGNAL-APP-ID");
116
117
  ```
117
118
 
118
119
  ### Versioning
@@ -6,7 +6,13 @@
6
6
  Object.defineProperty(exports, "__esModule", { value: true });
7
7
  const withOneSignalAndroid_1 = require("./withOneSignalAndroid");
8
8
  const withOneSignalIos_1 = require("./withOneSignalIos");
9
+ const helpers_1 = require("../support/helpers");
9
10
  const withOneSignal = (config, props) => {
11
+ // if props are undefined, throw error
12
+ if (!props) {
13
+ throw new Error('You are trying to use the OneSignal plugin without any props. Property "mode" is required. Please see https://github.com/OneSignal/onesignal-expo-plugin for more info.');
14
+ }
15
+ (0, helpers_1.validatePluginProps)(props);
10
16
  config = (0, withOneSignalIos_1.withOneSignalIos)(config, props);
11
17
  config = (0, withOneSignalAndroid_1.withOneSignalAndroid)(config, props);
12
18
  return config;
@@ -58,6 +58,30 @@ const withLargeIcons = (config, onesignalProps) => {
58
58
  },
59
59
  ]);
60
60
  };
61
+ const withSmallIconAccentColor = (config, onesignalProps) => {
62
+ if (!onesignalProps.smallIconAccentColor) {
63
+ return config;
64
+ }
65
+ return (0, config_plugins_1.withStringsXml)(config, (config) => {
66
+ var _a, _b;
67
+ const colorInARGB = `FF${(_a = onesignalProps.smallIconAccentColor) === null || _a === void 0 ? void 0 : _a.replace('#', '')}`;
68
+ const strings = (_b = config.modResults.resources.string) !== null && _b !== void 0 ? _b : [];
69
+ // Check if the accent color entry already exists
70
+ const hasAccentColor = strings.some((stringEntry) => {
71
+ var _a;
72
+ return ((_a = stringEntry.$) === null || _a === void 0 ? void 0 : _a.name) === 'onesignal_notification_accent_color' &&
73
+ stringEntry._ === colorInARGB;
74
+ });
75
+ if (!hasAccentColor) {
76
+ const accentColorEntry = {
77
+ $: { name: 'onesignal_notification_accent_color' },
78
+ _: colorInARGB,
79
+ };
80
+ config.modResults.resources.string = [...strings, accentColorEntry];
81
+ }
82
+ return config;
83
+ });
84
+ };
61
85
  async function saveIconsArrayAsync(projectRoot, icons, dirsToSize) {
62
86
  for (const icon of icons) {
63
87
  await saveIconAsync(icon, projectRoot, dirsToSize);
@@ -84,6 +108,7 @@ async function saveIconAsync(icon, projectRoot, dirsToSize) {
84
108
  const withOneSignalAndroid = (config, props) => {
85
109
  config = withSmallIcons(config, props);
86
110
  config = withLargeIcons(config, props);
111
+ config = withSmallIconAccentColor(config, props);
87
112
  return config;
88
113
  };
89
114
  exports.withOneSignalAndroid = withOneSignalAndroid;
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.validatePluginProps = void 0;
4
+ const types_1 = require("../types/types");
5
+ function validatePluginProps(props) {
6
+ // check the type of each property
7
+ if (typeof props.mode !== "string") {
8
+ throw new Error("OneSignal Expo Plugin: 'mode' must be a string.");
9
+ }
10
+ if (props.devTeam && typeof props.devTeam !== "string") {
11
+ throw new Error("OneSignal Expo Plugin: 'devTeam' must be a string.");
12
+ }
13
+ if (props.iPhoneDeploymentTarget && typeof props.iPhoneDeploymentTarget !== "string") {
14
+ throw new Error("OneSignal Expo Plugin: 'iPhoneDeploymentTarget' must be a string.");
15
+ }
16
+ if (props.smallIcons && !Array.isArray(props.smallIcons)) {
17
+ throw new Error("OneSignal Expo Plugin: 'smallIcons' must be an array.");
18
+ }
19
+ if (props.smallIconAccentColor && typeof props.smallIconAccentColor !== "string") {
20
+ throw new Error("OneSignal Expo Plugin: 'smallIconAccentColor' must be a string.");
21
+ }
22
+ if (props.largeIcons && !Array.isArray(props.largeIcons)) {
23
+ throw new Error("OneSignal Expo Plugin: 'largeIcons' must be an array.");
24
+ }
25
+ if (props.iosNSEFilePath && typeof props.iosNSEFilePath !== "string") {
26
+ throw new Error("OneSignal Expo Plugin: 'iosNSEFilePath' must be a string.");
27
+ }
28
+ // check for extra properties
29
+ const inputProps = Object.keys(props);
30
+ for (const prop of inputProps) {
31
+ if (!types_1.ONESIGNAL_PLUGIN_PROPS.includes(prop)) {
32
+ throw new Error(`OneSignal Expo Plugin: You have provided an invalid property "${prop}" to the OneSignal plugin.`);
33
+ }
34
+ }
35
+ }
36
+ exports.validatePluginProps = validatePluginProps;
@@ -1,6 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Mode = void 0;
3
+ exports.Mode = exports.ONESIGNAL_PLUGIN_PROPS = void 0;
4
+ exports.ONESIGNAL_PLUGIN_PROPS = [
5
+ "mode",
6
+ "devTeam",
7
+ "iPhoneDeploymentTarget",
8
+ "smallIcons",
9
+ "largeIcons",
10
+ "iosNSEFilePath",
11
+ "smallIconAccentColor"
12
+ ];
4
13
  var Mode;
5
14
  (function (Mode) {
6
15
  Mode["Dev"] = "development";
@@ -7,8 +7,18 @@ import { ConfigPlugin } from '@expo/config-plugins';
7
7
  import { OneSignalPluginProps } from '../types/types';
8
8
  import { withOneSignalAndroid } from './withOneSignalAndroid';
9
9
  import { withOneSignalIos } from './withOneSignalIos';
10
+ import { validatePluginProps } from '../support/helpers';
10
11
 
11
12
  const withOneSignal: ConfigPlugin<OneSignalPluginProps> = (config, props) => {
13
+ // if props are undefined, throw error
14
+ if (!props) {
15
+ throw new Error(
16
+ 'You are trying to use the OneSignal plugin without any props. Property "mode" is required. Please see https://github.com/OneSignal/onesignal-expo-plugin for more info.'
17
+ );
18
+ }
19
+
20
+ validatePluginProps(props);
21
+
12
22
  config = withOneSignalIos(config, props);
13
23
  config = withOneSignalAndroid(config, props);
14
24
 
@@ -3,7 +3,7 @@
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, withDangerousMod } from '@expo/config-plugins';
6
+ import { ConfigPlugin, withDangerousMod, withStringsXml } from '@expo/config-plugins';
7
7
  import { generateImageAsync } from '@expo/image-utils';
8
8
  import { OneSignalLog } from '../support/OneSignalLog';
9
9
  import { OneSignalPluginProps } from '../types/types';
@@ -72,6 +72,35 @@ const withLargeIcons: ConfigPlugin<OneSignalPluginProps> = (
72
72
  ]);
73
73
  };
74
74
 
75
+ const withSmallIconAccentColor: ConfigPlugin<OneSignalPluginProps> = (config, onesignalProps) => {
76
+ if(!onesignalProps.smallIconAccentColor) {
77
+ return config
78
+ }
79
+
80
+ return withStringsXml(config, (config) => {
81
+ const colorInARGB = `FF${onesignalProps.smallIconAccentColor?.replace('#', '')}`;
82
+ const strings = config.modResults.resources.string ?? [];
83
+
84
+ // Check if the accent color entry already exists
85
+ const hasAccentColor = strings.some(
86
+ (stringEntry) =>
87
+ stringEntry.$?.name === 'onesignal_notification_accent_color' &&
88
+ stringEntry._ === colorInARGB
89
+ );
90
+
91
+ if (!hasAccentColor) {
92
+ const accentColorEntry = {
93
+ $: { name: 'onesignal_notification_accent_color' },
94
+ _: colorInARGB,
95
+ };
96
+
97
+ config.modResults.resources.string = [...strings, accentColorEntry];
98
+ }
99
+
100
+ return config;
101
+ });
102
+ }
103
+
75
104
  async function saveIconsArrayAsync(projectRoot: string, icons: string[], dirsToSize: { [name: string]: number }) {
76
105
  for(const icon of icons) {
77
106
  await saveIconAsync(icon, projectRoot, dirsToSize);
@@ -113,5 +142,6 @@ export const withOneSignalAndroid: ConfigPlugin<OneSignalPluginProps> = (
113
142
  ) => {
114
143
  config = withSmallIcons(config, props);
115
144
  config = withLargeIcons(config, props);
145
+ config = withSmallIconAccentColor(config, props);
116
146
  return config;
117
147
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "onesignal-expo-plugin",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
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": {
@@ -0,0 +1,41 @@
1
+ import { ONESIGNAL_PLUGIN_PROPS } from "../types/types";
2
+
3
+ export function validatePluginProps(props: any): void {
4
+ // check the type of each property
5
+ if (typeof props.mode !== "string") {
6
+ throw new Error("OneSignal Expo Plugin: 'mode' must be a string.");
7
+ }
8
+
9
+ if (props.devTeam && typeof props.devTeam !== "string") {
10
+ throw new Error("OneSignal Expo Plugin: 'devTeam' must be a string.");
11
+ }
12
+
13
+ if (props.iPhoneDeploymentTarget && typeof props.iPhoneDeploymentTarget !== "string") {
14
+ throw new Error("OneSignal Expo Plugin: 'iPhoneDeploymentTarget' must be a string.");
15
+ }
16
+
17
+ if (props.smallIcons && !Array.isArray(props.smallIcons)) {
18
+ throw new Error("OneSignal Expo Plugin: 'smallIcons' must be an array.");
19
+ }
20
+
21
+ if (props.smallIconAccentColor && typeof props.smallIconAccentColor !== "string") {
22
+ throw new Error("OneSignal Expo Plugin: 'smallIconAccentColor' must be a string.");
23
+ }
24
+
25
+ if (props.largeIcons && !Array.isArray(props.largeIcons)) {
26
+ throw new Error("OneSignal Expo Plugin: 'largeIcons' must be an array.");
27
+ }
28
+
29
+ if (props.iosNSEFilePath && typeof props.iosNSEFilePath !== "string") {
30
+ throw new Error("OneSignal Expo Plugin: 'iosNSEFilePath' must be a string.");
31
+ }
32
+
33
+ // check for extra properties
34
+ const inputProps = Object.keys(props);
35
+
36
+ for (const prop of inputProps) {
37
+ if (!ONESIGNAL_PLUGIN_PROPS.includes(prop)) {
38
+ throw new Error(`OneSignal Expo Plugin: You have provided an invalid property "${prop}" to the OneSignal plugin.`);
39
+ }
40
+ }
41
+ }
package/types/types.ts CHANGED
@@ -8,15 +8,15 @@
8
8
  mode: Mode;
9
9
 
10
10
  /**
11
- * (optional) Used to configure Apple Team ID. You can find your Apple Team ID by running expo credentials:manager e.g: "91SW8A37CR"
11
+ * (optional) Used to configure Apple Team ID. You can find your Apple Team ID by running expo credentials:manager e.g: "91SW8A37CR"
12
12
  */
13
- devTeam: string;
13
+ devTeam?: string;
14
14
 
15
15
  /**
16
16
  * (optional) Target IPHONEOS_DEPLOYMENT_TARGET value to be used when adding the iOS NSE. A deployment target is nothing more than
17
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
18
  */
19
- iPhoneDeploymentTarget: string;
19
+ iPhoneDeploymentTarget?: string;
20
20
 
21
21
  /**
22
22
  * (optional) The small notification icons for Android. Images will be automatically scaled up/down, the recommended size
@@ -24,6 +24,11 @@
24
24
  */
25
25
  smallIcons?: string[];
26
26
 
27
+ /**
28
+ * (optional) The accent color to use for notification icons on Android. Must be a valid Android color resource, for example: "#FF0000"
29
+ */
30
+ smallIconAccentColor?: string;
31
+
27
32
  /**
28
33
  * (optional) The large notification icons for Android. Images will be automatically scaled up/down to 256x256.
29
34
  */
@@ -37,6 +42,16 @@
37
42
  iosNSEFilePath?: string;
38
43
  };
39
44
 
45
+ export const ONESIGNAL_PLUGIN_PROPS: string[] = [
46
+ "mode",
47
+ "devTeam",
48
+ "iPhoneDeploymentTarget",
49
+ "smallIcons",
50
+ "largeIcons",
51
+ "iosNSEFilePath",
52
+ "smallIconAccentColor"
53
+ ];
54
+
40
55
  export enum Mode {
41
56
  Dev = "development",
42
57
  Prod = "production"