forked from invertase/react-native-firebase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetupFirebaseNotifationIcon.ts
More file actions
113 lines (100 loc) · 4.28 KB
/
Copy pathsetupFirebaseNotifationIcon.ts
File metadata and controls
113 lines (100 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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;
});
};
// 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.
*/
// @ts-ignore - config.notification deprecated in Expo 54 / removed in Expo 55+, but still useful for people
if (config.notification) {
return {
// @ts-ignore - config.notification deprecated in Expo 54 / removed in Expo 55+, but still useful for people
icon: config.notification.icon,
// @ts-ignore - config.notification deprecated in Expo 54 / removed in Expo 55+, but still useful for people
color: config.notification.color,
};
}
return { icon: undefined, color: undefined };
};
export function setFireBaseMessagingAndroidManifest(
config: ExpoConfig,
application: ManifestApplication,
) {
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',
);
}
// Defensive code
application['meta-data'] ??= [];
const metaData = application['meta-data'];
if (
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 (
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;
}