diff --git a/packages/messaging/app.plugin.js b/packages/messaging/app.plugin.js new file mode 100644 index 0000000000..3c7d11b615 --- /dev/null +++ b/packages/messaging/app.plugin.js @@ -0,0 +1 @@ +module.exports = require('./plugin/build'); diff --git a/packages/messaging/package.json b/packages/messaging/package.json index 77b286312e..e0acd6e67f 100644 --- a/packages/messaging/package.json +++ b/packages/messaging/package.json @@ -8,7 +8,9 @@ "scripts": { "build": "genversion --semi lib/version.js", "build:clean": "rimraf android/build && rimraf ios/build", - "prepare": "yarn run build" + "build:plugin": "rimraf plugin/build && tsc --build plugin", + "lint:plugin": "eslint plugin/src/*", + "prepare": "yarn run build && yarn run build:plugin" }, "repository": { "type": "git", @@ -22,7 +24,16 @@ "messaging" ], "peerDependencies": { - "@react-native-firebase/app": "18.6.2" + "@react-native-firebase/app": "18.6.2", + "expo": ">=47.0.0" + }, + "devDependencies": { + "expo": "^49.0.20" + }, + "peerDependenciesMeta": { + "expo": { + "optional": true + } }, "publishConfig": { "access": "public" diff --git a/packages/messaging/plugin/__tests__/androidPlugin.test.ts b/packages/messaging/plugin/__tests__/androidPlugin.test.ts new file mode 100644 index 0000000000..01d2500d9b --- /dev/null +++ b/packages/messaging/plugin/__tests__/androidPlugin.test.ts @@ -0,0 +1,63 @@ +import { describe, expect, it, jest } from '@jest/globals'; +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 () { + 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 = 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(); + }); +}); diff --git a/packages/messaging/plugin/__tests__/fixtures/application-example.ts b/packages/messaging/plugin/__tests__/fixtures/application-example.ts new file mode 100644 index 0000000000..c60f1936c7 --- /dev/null +++ b/packages/messaging/plugin/__tests__/fixtures/application-example.ts @@ -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; diff --git a/packages/messaging/plugin/__tests__/fixtures/expo-config-example.ts b/packages/messaging/plugin/__tests__/fixtures/expo-config-example.ts new file mode 100644 index 0000000000..c9aa9e1e27 --- /dev/null +++ b/packages/messaging/plugin/__tests__/fixtures/expo-config-example.ts @@ -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; diff --git a/packages/messaging/plugin/src/android/index.ts b/packages/messaging/plugin/src/android/index.ts new file mode 100644 index 0000000000..c2329e2f3b --- /dev/null +++ b/packages/messaging/plugin/src/android/index.ts @@ -0,0 +1,3 @@ +import { withExpoPluginFirebaseNotification } from './setupFirebaseNotifationIcon'; + +export { withExpoPluginFirebaseNotification }; diff --git a/packages/messaging/plugin/src/android/setupFirebaseNotifationIcon.ts b/packages/messaging/plugin/src/android/setupFirebaseNotifationIcon.ts new file mode 100644 index 0000000000..7a226e7a53 --- /dev/null +++ b/packages/messaging/plugin/src/android/setupFirebaseNotifationIcon.ts @@ -0,0 +1,77 @@ +import { ConfigPlugin, withAndroidManifest } from '@expo/config-plugins'; +import { ManifestApplication } from '@expo/config-plugins/build/android/Manifest'; +import { ExpoConfig } from '@expo/config-types'; + +/** + * Determine whether a ManifestApplication has an attribute. + */ +const hasMetaData = (application: ManifestApplication, metaData: string) => { + return application['meta-data']?.some(item => item['$']['android:name'] === metaData); +}; + +/** + * 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 => { + // Add NS `xmlns:tools to handle boundary conditions. + config.modResults.manifest.$ = { + ...config.modResults.manifest.$, + 'xmlns:tools': 'http://schemas.android.com/tools', + }; + + 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 && + !hasMetaData(application, 'com.google.firebase.messaging.default_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 && + !hasMetaData(application, 'com.google.firebase.messaging.default_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; +} diff --git a/packages/messaging/plugin/src/index.ts b/packages/messaging/plugin/src/index.ts new file mode 100644 index 0000000000..7b46e739da --- /dev/null +++ b/packages/messaging/plugin/src/index.ts @@ -0,0 +1,17 @@ +import { ConfigPlugin, withPlugins, createRunOncePlugin } from '@expo/config-plugins'; +import { withExpoPluginFirebaseNotification } from './android'; + +/** + * A config plugin for configuring `@react-native-firebase/app` + */ +const withRnFirebaseApp: ConfigPlugin = config => { + return withPlugins(config, [ + // iOS + + // Android + withExpoPluginFirebaseNotification, + ]); +}; + +const pak = require('@react-native-firebase/messaging/package.json'); +export default createRunOncePlugin(withRnFirebaseApp, pak.name, pak.version); diff --git a/packages/messaging/plugin/tsconfig.json b/packages/messaging/plugin/tsconfig.json new file mode 100644 index 0000000000..c2e8788648 --- /dev/null +++ b/packages/messaging/plugin/tsconfig.json @@ -0,0 +1,9 @@ +{ + "extends": "@tsconfig/node-lts/tsconfig", + "compilerOptions": { + "outDir": "build", + "rootDir": "src", + "declaration": true + }, + "include": ["./src"] +} diff --git a/yarn.lock b/yarn.lock index 953c6b7ee7..f2456a3189 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5054,8 +5054,14 @@ __metadata: "@react-native-firebase/messaging@npm:18.6.2, @react-native-firebase/messaging@workspace:packages/messaging": version: 0.0.0-use.local resolution: "@react-native-firebase/messaging@workspace:packages/messaging" + dependencies: + expo: "npm:^49.0.20" peerDependencies: "@react-native-firebase/app": 18.6.2 + expo: ">=47.0.0" + peerDependenciesMeta: + expo: + optional: true languageName: unknown linkType: soft