Skip to content

Commit

Permalink
Merge pull request #196 from OneSignal/small-icon-color
Browse files Browse the repository at this point in the history
Add ability to set the small icon accent color
  • Loading branch information
rgomezp authored Sep 26, 2023
2 parents 27da6e8 + 8a49f8b commit 87ff596
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ You can pass props to the plugin config object to configure:
| `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"`. |
| `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. |
| `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. |
| `smallIconAccentColor` | optional | The accent color to use for notification icons on Android. Must be a valid hex value, e.g: `"#FF0000"` |
| `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"`. |

### OneSignal App ID
Expand Down
32 changes: 31 additions & 1 deletion onesignal/withOneSignalAndroid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @see https://documentation.onesignal.com/docs/react-native-sdk-setup#step-4-install-for-ios-using-cocoapods-for-ios-apps
*/

import { ConfigPlugin, withDangerousMod } from '@expo/config-plugins';
import { ConfigPlugin, withDangerousMod, withStringsXml } from '@expo/config-plugins';
import { generateImageAsync } from '@expo/image-utils';
import { OneSignalLog } from '../support/OneSignalLog';
import { OneSignalPluginProps } from '../types/types';
Expand Down Expand Up @@ -72,6 +72,35 @@ const withLargeIcons: ConfigPlugin<OneSignalPluginProps> = (
]);
};

const withSmallIconAccentColor: ConfigPlugin<OneSignalPluginProps> = (config, onesignalProps) => {
if(!onesignalProps.smallIconAccentColor) {
return config
}

return withStringsXml(config, (config) => {
const colorInARGB = `FF${onesignalProps.smallIconAccentColor?.replace('#', '')}`;
const strings = config.modResults.resources.string ?? [];

// Check if the accent color entry already exists
const hasAccentColor = strings.some(
(stringEntry) =>
stringEntry.$?.name === 'onesignal_notification_accent_color' &&
stringEntry._ === colorInARGB
);

if (!hasAccentColor) {
const accentColorEntry = {
$: { name: 'onesignal_notification_accent_color' },
_: colorInARGB,
};

config.modResults.resources.string = [...strings, accentColorEntry];
}

return config;
});
}

async function saveIconsArrayAsync(projectRoot: string, icons: string[], dirsToSize: { [name: string]: number }) {
for(const icon of icons) {
await saveIconAsync(icon, projectRoot, dirsToSize);
Expand Down Expand Up @@ -113,5 +142,6 @@ export const withOneSignalAndroid: ConfigPlugin<OneSignalPluginProps> = (
) => {
config = withSmallIcons(config, props);
config = withLargeIcons(config, props);
config = withSmallIconAccentColor(config, props);
return config;
};
5 changes: 5 additions & 0 deletions types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
*/
smallIcons?: string[];

/**
* (optional) The accent color to use for notification icons on Android. Must be a valid Android color resource, for example: "#FF0000"
*/
smallIconAccentColor?: string;

/**
* (optional) The large notification icons for Android. Images will be automatically scaled up/down to 256x256.
*/
Expand Down

0 comments on commit 87ff596

Please sign in to comment.