Skip to content

Commit d571610

Browse files
committed
Add check for expo-notifications notification icon configurations
1 parent 2af28df commit d571610

File tree

3 files changed

+89
-11
lines changed

3 files changed

+89
-11
lines changed

packages/messaging/plugin/__tests__/androidPlugin.test.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { describe, expect, it } from '@jest/globals';
22
import { setFireBaseMessagingAndroidManifest } from '../src/android/setupFirebaseNotifationIcon';
33
import { ExpoConfig } from '@expo/config-types';
4-
import expoConfigExample from './fixtures/expo-config-example';
4+
import { expoConfigExample, expoConfigExampleWithExpoNotificationsPlugin } from './fixtures/expo-config-example';
55
import manifestApplicationExample from './fixtures/application-example';
66
import { ManifestApplication } from '@expo/config-plugins/build/android/Manifest';
77

@@ -67,4 +67,47 @@ describe('Config Plugin Android Tests', function () {
6767
// eslint-disable-next-line no-console
6868
console.warn = warnOrig;
6969
});
70+
71+
it('applies changes to app/src/main/AndroidManifest.xml with expo-notifications plugin config when app.json notification is undefined', async function () {
72+
const config: ExpoConfig = JSON.parse(JSON.stringify(expoConfigExampleWithExpoNotificationsPlugin));
73+
const manifestApplication: ManifestApplication = JSON.parse(
74+
JSON.stringify(manifestApplicationExample),
75+
);
76+
config.notification = undefined;
77+
setFireBaseMessagingAndroidManifest(config, manifestApplication);
78+
expect(manifestApplication['meta-data']).toContainEqual({
79+
$: {
80+
'android:name': 'com.google.firebase.messaging.default_notification_icon',
81+
'android:resource': '@drawable/notification_icon',
82+
},
83+
});
84+
expect(manifestApplication['meta-data']).toContainEqual({
85+
$: {
86+
'android:name': 'com.google.firebase.messaging.default_notification_color',
87+
'android:resource': '@color/notification_icon_color',
88+
'tools:replace': 'android:resource',
89+
},
90+
});
91+
});
92+
93+
it('applies changes to app/src/main/AndroidManifest.xml with app.json notification config when both configs are defined', async function () {
94+
const config: ExpoConfig = JSON.parse(JSON.stringify(expoConfigExampleWithExpoNotificationsPlugin));
95+
const manifestApplication: ManifestApplication = JSON.parse(
96+
JSON.stringify(manifestApplicationExample),
97+
);
98+
setFireBaseMessagingAndroidManifest(config, manifestApplication);
99+
expect(manifestApplication['meta-data']).toContainEqual({
100+
$: {
101+
'android:name': 'com.google.firebase.messaging.default_notification_icon',
102+
'android:resource': '@drawable/notification_icon',
103+
},
104+
});
105+
expect(manifestApplication['meta-data']).toContainEqual({
106+
$: {
107+
'android:name': 'com.google.firebase.messaging.default_notification_color',
108+
'android:resource': '@color/notification_icon_color',
109+
'tools:replace': 'android:resource',
110+
},
111+
});
112+
});
70113
});

packages/messaging/plugin/__tests__/fixtures/expo-config-example.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
import { ExpoConfig } from '@expo/config-types';
22

3+
const notificationConfig = {
4+
icon: 'IconAsset',
5+
color: '#1D172D',
6+
};
7+
8+
/**
9+
* @type {import('@expo/config-types').ExpoConfig}
10+
*/
11+
export const expoConfigExample: ExpoConfig = {
12+
name: 'FirebaseMessagingTest',
13+
slug: 'fire-base-messaging-test',
14+
notification: notificationConfig,
15+
};
16+
317
/**
418
* @type {import('@expo/config-types').ExpoConfig}
519
*/
6-
const expoConfigExample: ExpoConfig = {
20+
export const expoConfigExampleWithExpoNotificationsPlugin: ExpoConfig = {
721
name: 'FirebaseMessagingTest',
822
slug: 'fire-base-messaging-test',
9-
notification: {
10-
icon: 'IconAsset',
11-
color: '#1D172D',
12-
},
23+
notification: notificationConfig,
24+
plugins: [['expo-notifications', notificationConfig]],
1325
};
1426

15-
export default expoConfigExample;

packages/messaging/plugin/src/android/setupFirebaseNotifationIcon.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,36 @@ export const withExpoPluginFirebaseNotification: ConfigPlugin = config => {
2626
});
2727
};
2828

29+
interface NotificationConfig {
30+
icon: string | null;
31+
color: string | null;
32+
}
33+
2934
export function setFireBaseMessagingAndroidManifest(
3035
config: ExpoConfig,
3136
application: ManifestApplication,
3237
) {
38+
const notificationConfig: NotificationConfig = {
39+
icon: null,
40+
color: null,
41+
};
42+
43+
const notificationConfigFromPlugin = config.plugins?.find(plugin => plugin[0] === 'expo-notifications')?.[1];
44+
45+
// check if the notification config is defined in the expo-notifications plugin first
46+
if (notificationConfigFromPlugin?.icon || notificationConfigFromPlugin?.color) {
47+
notificationConfig.icon = notificationConfigFromPlugin?.icon || null;
48+
notificationConfig.color = notificationConfigFromPlugin?.color || null;
49+
}
50+
51+
// then check if the notification config is defined in the app.json notification object and override the plugin config if it is defined
52+
if (config.notification) {
53+
notificationConfig.icon = config.notification.icon || notificationConfig?.icon || null;
54+
notificationConfig.color = config.notification.color || notificationConfig?.color || null;
55+
}
56+
3357
// If the notification object is not defined, print a friendly warning
34-
if (!config.notification) {
58+
if (!notificationConfig.icon && !notificationConfig.color) {
3559
// This warning is important because the notification icon can only use pure white on Android. By default, the system uses the app icon as the notification icon, but the app icon is usually not pure white, so you need to set the notification icon
3660
// eslint-disable-next-line no-console
3761
console.warn(
@@ -46,10 +70,10 @@ export function setFireBaseMessagingAndroidManifest(
4670
const metaData = application['meta-data'];
4771

4872
if (
49-
config.notification.icon &&
73+
notificationConfig.icon &&
5074
!hasMetaData(application, 'com.google.firebase.messaging.default_notification_icon')
5175
) {
52-
// Expo will automatically create '@drawable/notification_icon' resource if you specify config.notification.icon.
76+
// Expo will automatically create '@drawable/notification_icon' resource if you specify config.notification.icon or expo-notifications plugin.icon.
5377
metaData.push({
5478
$: {
5579
'android:name': 'com.google.firebase.messaging.default_notification_icon',
@@ -59,7 +83,7 @@ export function setFireBaseMessagingAndroidManifest(
5983
}
6084

6185
if (
62-
config.notification.color &&
86+
notificationConfig.color &&
6387
!hasMetaData(application, 'com.google.firebase.messaging.default_notification_color')
6488
) {
6589
metaData.push({

0 commit comments

Comments
 (0)