Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 61 additions & 1 deletion packages/app/plugin/__tests__/androidPlugin.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import fs from 'fs/promises';
import path from 'path';
import { beforeAll, describe, expect, it } from '@jest/globals';
import { beforeAll, describe, expect, it, jest } from '@jest/globals';

import { applyPlugin } from '../src/android/applyPlugin';
import { setBuildscriptDependency } from '../src/android/buildscriptDependency';
import { setFireBaseMessagingAndroidManifest } from '../src/android/setupFirebaseNotifationIcon';
import { ExpoConfig } from '@expo/config-types';
import expoConfigExample from './fixtures/expo-config-example';
import manifestApplicationExample from './fixtures/application-example';
import { ManifestApplication } from '@expo/config-plugins/build/android/Manifest';

describe('Config Plugin Android Tests', function () {
let appBuildGradle: string;
Expand All @@ -29,4 +34,59 @@ describe('Config Plugin Android Tests', function () {
const result = applyPlugin(appBuildGradle);
expect(result).toMatchSnapshot();
});

it('applies changes to app/src/main/AndroidManifest.xml with color', async function () {
const config: ExpoConfig = JSON.parse(JSON.stringify(expoConfigExample));
const manifestApplication: ManifestApplication = JSON.parse(
JSON.stringify(manifestApplicationExample),
);
setFireBaseMessagingAndroidManifest(config, manifestApplication);
expect(manifestApplication['meta-data']).toContainEqual({
$: {
'android:name': 'com.google.firebase.messaging.default_notification_icon',
'android:resource': '@drawable/notification_icon',
},
});
expect(manifestApplication['meta-data']).toContainEqual({
$: {
'android:name': 'com.google.firebase.messaging.default_notification_color',
'android:resource': '@color/notification_icon_color',
'tools:replace': 'android:resource',
},
});
});

it('applies changes to app/src/main/AndroidManifest.xml without color', async function () {
const config: ExpoConfig = JSON.parse(JSON.stringify(expoConfigExample));
const manifestApplication: ManifestApplication = JSON.parse(
JSON.stringify(manifestApplicationExample),
);
config.notification!.color = undefined;
setFireBaseMessagingAndroidManifest(config, manifestApplication);
expect(manifestApplication['meta-data']).toContainEqual({
$: {
'android:name': 'com.google.firebase.messaging.default_notification_icon',
'android:resource': '@drawable/notification_icon',
},
});
expect(manifestApplication['meta-data']).not.toContainEqual({
$: {
'android:name': 'com.google.firebase.messaging.default_notification_icon',
'android:resource': '@drawable/notification_icon_color',
'tools:replace': 'android:resource',
},
});
});

it('applies changes to app/src/main/AndroidManifest.xml without notification', async function () {
const warnSpy = jest.spyOn(console, 'warn');
const config: ExpoConfig = JSON.parse(JSON.stringify(expoConfigExample));
const manifestApplication: ManifestApplication = JSON.parse(
JSON.stringify(manifestApplicationExample),
);
config.notification = undefined;
setFireBaseMessagingAndroidManifest(config, manifestApplication);
expect(warnSpy).toHaveBeenCalled();
warnSpy.mockRestore();
});
});
12 changes: 12 additions & 0 deletions packages/app/plugin/__tests__/fixtures/application-example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ManifestApplication } from '@expo/config-plugins/build/android/Manifest';

/**
* @type {import('"@expo/config-plugins/build/android/Manifest"').ManifestApplication}
*/
const manifestApplicationExample: ManifestApplication = {
$: {
'android:name': '',
},
};

export default manifestApplicationExample;
15 changes: 15 additions & 0 deletions packages/app/plugin/__tests__/fixtures/expo-config-example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ExpoConfig } from '@expo/config-types';

/**
* @type {import('@expo/config-types').ExpoConfig}
*/
const expoConfigExample: ExpoConfig = {
name: 'FirebaseMessagingTest',
slug: 'fire-base-messaging-test',
notification: {
icon: 'IconAsset',
color: '#1D172D',
},
};

export default expoConfigExample;
8 changes: 7 additions & 1 deletion packages/app/plugin/src/android/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { withApplyGoogleServicesPlugin } from './applyPlugin';
import { withBuildscriptDependency } from './buildscriptDependency';
import { withCopyAndroidGoogleServices } from './copyGoogleServices';
import { withExpoPluginFirebaseNotification } from './setupFirebaseNotifationIcon';

export { withBuildscriptDependency, withApplyGoogleServicesPlugin, withCopyAndroidGoogleServices };
export {
withBuildscriptDependency,
withApplyGoogleServicesPlugin,
withCopyAndroidGoogleServices,
withExpoPluginFirebaseNotification,
};
81 changes: 81 additions & 0 deletions packages/app/plugin/src/android/setupFirebaseNotifationIcon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { ConfigPlugin, withAndroidManifest } from '@expo/config-plugins';
import { ManifestApplication } from '@expo/config-plugins/build/android/Manifest';
import { ExpoConfig } from '@expo/config-types';

/**
* Check if the developer has installed '@react-native-firebase/messaging'
*
* @return {boolean}
*/
function hasMessagingDependency(): boolean {
try {
require.resolve('@react-native-firebase/messaging');
} catch (error) {
return false;
}
return true;
}

/**
* Create `com.google.firebase.messaging.default_notification_icon` and `com.google.firebase.messaging.default_notification_color`
*/
export const withExpoPluginFirebaseNotification: ConfigPlugin = config => {
return withAndroidManifest(config, async config => {
const messagingInstalled = hasMessagingDependency();
// If the developer is not using '@react-native-firebase/messaging', there is no need to do anything
if (!messagingInstalled) {
return config;
}
// If there is no need to build an Android program, there is no need to do anything
if (!config.android) {
return config;
}
const application = config.modResults.manifest.application![0];
setFireBaseMessagingAndroidManifest(config, application);
return config;
});
};

export function setFireBaseMessagingAndroidManifest(
config: ExpoConfig,
application: ManifestApplication,
) {
// If the notification object is not defined, print a friendly warning
if (!config.notification) {
// 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
application['meta-data'] ??= [];

const metaData = application['meta-data'];

if (config.notification.icon) {
// Expo will automatically create '@drawable/notification_icon' resource if you specify config.notification.icon.
metaData.push({
$: {
'android:name': 'com.google.firebase.messaging.default_notification_icon',
'android:resource': '@drawable/notification_icon',
},
});
}

if (config.notification.color) {
metaData.push({
$: {
'android:name': 'com.google.firebase.messaging.default_notification_color',
'android:resource': '@color/notification_icon_color',
// @react-native-firebase/messaging will automatically configure the notification color from the 'firebase.json' file, setting 'tools:replace' = 'android:resource' to overwrite it.
// @ts-ignore
'tools:replace': 'android:resource',
},
});
}

return application;
}
2 changes: 2 additions & 0 deletions packages/app/plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
withApplyGoogleServicesPlugin,
withBuildscriptDependency,
withCopyAndroidGoogleServices,
withExpoPluginFirebaseNotification,
} from './android';
import { withFirebaseAppDelegate, withIosGoogleServicesFile } from './ios';

Expand All @@ -20,6 +21,7 @@ const withRnFirebaseApp: ConfigPlugin = config => {
withBuildscriptDependency,
withApplyGoogleServicesPlugin,
withCopyAndroidGoogleServices,
withExpoPluginFirebaseNotification,
]);
};

Expand Down