From ac96a9678164bc20bb5edbd9ed0ba52634280e7d Mon Sep 17 00:00:00 2001 From: Hugo EXTRAT Date: Thu, 21 Aug 2025 13:52:01 +0200 Subject: [PATCH] feat(expo): enhances notification config retrieval Improves notification configuration by first checking `expo-notifications` plugin for icon and color. Falls back to deprecated `config.notification` settings. Warns about missing notification icon for Android. --- .../android/setupFirebaseNotifationIcon.ts | 43 ++++++++++++++++--- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/packages/messaging/plugin/src/android/setupFirebaseNotifationIcon.ts b/packages/messaging/plugin/src/android/setupFirebaseNotifationIcon.ts index 7a226e7a53..b707ce3abd 100644 --- a/packages/messaging/plugin/src/android/setupFirebaseNotifationIcon.ts +++ b/packages/messaging/plugin/src/android/setupFirebaseNotifationIcon.ts @@ -26,18 +26,51 @@ export const withExpoPluginFirebaseNotification: ConfigPlugin = config => { }); }; +// Helper function to get notification icon and color from either config.notification or expo-notifications plugin +const getNotificationConfig = (config: ExpoConfig) => { + // Check expo-notifications plugin + if (config.plugins) { + const expoNotificationsPlugin = config.plugins.find(plugin => + Array.isArray(plugin) && plugin[0] === 'expo-notifications' + ); + + if (expoNotificationsPlugin && Array.isArray(expoNotificationsPlugin)) { + const pluginConfig = expoNotificationsPlugin[1]; + if (pluginConfig && typeof pluginConfig === 'object') { + return { + icon: pluginConfig.icon, + color: pluginConfig.color, + }; + } + } + } + + /** + * @deprecated + * Get notification config from config.notification + * Will be removed in the future as `expo-notifications` plugin is the recommended way to configure notifications. + */ + if (config.notification) { + return { + icon: config.notification.icon, + color: config.notification.color, + }; + } + + return { icon: undefined, color: undefined }; +}; + export function setFireBaseMessagingAndroidManifest( config: ExpoConfig, application: ManifestApplication, ) { - // If the notification object is not defined, print a friendly warning - if (!config.notification) { + const { icon, color } = getNotificationConfig(config); + if (!icon) { // 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 // eslint-disable-next-line no-console console.warn( 'For Android 8.0 and above, it is necessary to set the notification icon to ensure correct display. Otherwise, the notification will not show the correct icon. For more information, visit https://docs.expo.dev/versions/latest/config/app/#notification', ); - return config; } // Defensive code @@ -46,7 +79,7 @@ export function setFireBaseMessagingAndroidManifest( const metaData = application['meta-data']; if ( - config.notification.icon && + icon && !hasMetaData(application, 'com.google.firebase.messaging.default_notification_icon') ) { // Expo will automatically create '@drawable/notification_icon' resource if you specify config.notification.icon. @@ -59,7 +92,7 @@ export function setFireBaseMessagingAndroidManifest( } if ( - config.notification.color && + color && !hasMetaData(application, 'com.google.firebase.messaging.default_notification_color') ) { metaData.push({