-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathnotifications.ts
More file actions
140 lines (112 loc) · 4.16 KB
/
notifications.ts
File metadata and controls
140 lines (112 loc) · 4.16 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { confirm, input } from '@inquirer/prompts';
import ora from 'ora';
import { globals } from '../constants';
import addExpoConfig from '../util/addExpoConfig';
import commit, { handleCommitError } from '../util/commit';
import copyTemplateDirectory from '../util/copyTemplateDirectory';
import exec from '../util/exec';
import injectHooks from '../util/injectHooks';
import print from '../util/print';
import readAppJson from '../util/readAppJson';
type Options = {
bundleId?: string;
interactive?: boolean;
};
export async function addNotifications(options: Options = {}) {
const { interactive = true } = options;
globals.interactive = interactive;
const { bundleId = !interactive ? 'com.myapp' : undefined } = options;
await printIntro();
const spinner = ora().start('Adding React Native Firebase and dependencies');
// Install dependencies
await exec(
'npx expo install @react-native-firebase/app @react-native-firebase/messaging expo-build-properties',
);
spinner.succeed('Added React Native Firebase and dependencies');
spinner.start('Adding notification handlers');
await copyTemplateDirectory({
templateDir: 'notifications',
});
await injectHooks(
'App.tsx',
'useNotifications();',
"import useNotifications from 'src/hooks/useNotifications';\n",
);
spinner.succeed('Added notification handlers');
// Verify for bundle identifier and package name in the AppJsonConfig
const appJson = await readAppJson();
const packageName =
appJson.expo?.android?.package ??
bundleId ??
(await input({
message: 'Define your Android package name:',
default: 'com.myapp',
}));
const bundleIdentifier =
appJson.expo?.ios?.bundleIdentifier ??
bundleId ??
(await input({
message: 'Define your iOS bundle identifier:',
default: packageName,
}));
spinner.start('Configuring app.json');
await addExpoConfig({
android: {
googleServicesFile: './config/google-services.json',
package: packageName,
},
ios: {
googleServicesFile: './config/GoogleService-Info.plist',
bundleIdentifier,
},
plugins: [
'@react-native-firebase/app',
'@react-native-firebase/messaging',
[
'expo-build-properties',
{
ios: {
useFrameworks: 'static',
},
},
],
],
});
await commit('Add push notifications support.').catch(handleCommitError);
spinner.succeed(
`Successfully added notifications support to project.
In order to finish the setup please complete the following steps:
- Add your google-service.json and GoogleService-Info.plist files to your project's config folder
- Run the command "npx expo prebuild --clean" to rebuild the app
- Add the ios/ and android/ folders to your .gitignore file if you don't need to track them
For more details please refer to the official documentation: https://rnfirebase.io/#configure-react-native-firebase-modules.
`,
);
}
async function printIntro() {
print('Let’s get started!');
print(`\nWe will now add notifications support to your app. This will include the following changes:
- Add React Native Firebase, Messaging and Expo Build Properties dependencies
- Add notification handlers to your app
- Configure your app.json file with the necessary information
NOTE: React Native Firebase cannot be used in the pre-compiled Expo Go app because React Native Firebase uses native code that is not compiled into Expo Go. This will switch the app build process to use Continuos Native Generation (CGN), for more details please refer to the documentation (https://docs.expo.dev/workflow/continuous-native-generation).
`);
if (!globals.interactive) {
return;
}
const proceed = await confirm({ message: 'Ready to proceed?' });
if (!proceed) {
process.exit(0);
}
print(''); // add new line
}
/**
* Commander requires this signature to be ...args: unknown[]
* Actual args are:
* ([<Options hash>, <Command>])
*/
export default function addNotificationsAction(...args: unknown[]) {
// if argument ommitted, args[0] is options
const options = (args[0] as unknown[])[0] as Options;
return addNotifications(options);
}