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
103 lines (89 loc) · 4.03 KB
/
Copy pathsetupFirebaseNotifationIcon.ts
File metadata and controls
103 lines (89 loc) · 4.03 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
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;
});
};
interface NotificationConfig {
icon: string | null;
color: string | null;
}
export function setFireBaseMessagingAndroidManifest(
config: ExpoConfig,
application: ManifestApplication,
) {
const notificationConfig: NotificationConfig = {
icon: null,
color: null,
};
const notificationConfigFromPlugin = config.plugins?.find(
plugin => Array.isArray(plugin) && plugin[0] === 'expo-notifications',
)?.[1];
// check if the notification config is defined in the expo-notifications plugin first
if (notificationConfigFromPlugin?.icon || notificationConfigFromPlugin?.color) {
notificationConfig.icon = notificationConfigFromPlugin?.icon || null;
notificationConfig.color = notificationConfigFromPlugin?.color || null;
}
// then check if the notification config is defined in the app.json notification object and override the plugin config if it is defined
if (config.notification) {
notificationConfig.icon = config.notification.icon || notificationConfig?.icon || null;
notificationConfig.color = config.notification.color || notificationConfig?.color || null;
}
// If the notification object is not defined, print a friendly warning
if (!notificationConfig.icon && !notificationConfig.color) {
// 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 (
notificationConfig.icon &&
!hasMetaData(application, 'com.google.firebase.messaging.default_notification_icon')
) {
// Expo will automatically create '@drawable/notification_icon' resource if you specify config.notification.icon or expo-notifications plugin.icon.
metaData.push({
$: {
'android:name': 'com.google.firebase.messaging.default_notification_icon',
'android:resource': '@drawable/notification_icon',
},
});
}
if (
notificationConfig.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;
}